in benchmarks/tpcc/src/main/java/com/google/cloud/pgadapter/tpcc/AbstractBenchmarkRunner.java [286:450]
private void payment() throws SQLException {
LOG.debug("Executing payment");
long warehouseId = Long.reverse(random.nextInt(tpccConfiguration.getWarehouses()));
long districtId = Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse()));
long customerId = Long.reverse(random.nextInt(tpccConfiguration.getCustomersPerDistrict()));
BigDecimal amount = BigDecimal.valueOf(random.nextInt(1, 5000));
long customerWarehouseId;
long customerDistrictId;
String lastName = LastNameGenerator.generateLastName(this.random, Long.MAX_VALUE);
boolean byName;
Object[] row;
if (random.nextInt(100) < 60) {
byName = true;
} else {
byName = false;
}
if (random.nextInt(100) < 85) {
customerWarehouseId = warehouseId;
customerDistrictId = districtId;
} else {
customerWarehouseId = getOtherWarehouseId(warehouseId);
customerDistrictId =
Long.reverse(random.nextInt(tpccConfiguration.getDistrictsPerWarehouse()));
}
executeStatement("begin transaction");
executeParamStatement(
"UPDATE warehouse " + "SET w_ytd = w_ytd + ? " + "WHERE w_id = ?",
new Object[] {amount, warehouseId});
row =
paramQueryRow(
"SELECT w_street_1, w_street_2, w_city, w_state, w_zip, w_name "
+ "FROM warehouse "
+ "WHERE w_id = ?",
new Object[] {warehouseId});
String warehouseStreet1 = (String) row[0];
String warehouseStreet2 = (String) row[1];
String warehouseCity = (String) row[2];
String warehouseState = (String) row[3];
String warehouseZip = (String) row[4];
String warehouseName = (String) row[5];
executeParamStatement(
"UPDATE district " + "SET d_ytd = d_ytd + ? " + "WHERE w_id = ? AND d_id= ?",
new Object[] {amount, warehouseId, districtId});
row =
paramQueryRow(
"SELECT d_street_1, d_street_2, d_city, d_state, d_zip, d_name "
+ "FROM district "
+ "WHERE w_id = ? AND d_id = ?",
new Object[] {warehouseId, districtId});
String districtStreet1 = (String) row[0];
String districtStreet2 = (String) row[1];
String districtCity = (String) row[2];
String districtState = (String) row[3];
String districtZip = (String) row[4];
String districtName = (String) row[5];
if (byName) {
row =
paramQueryRow(
"SELECT count(c_id) namecnt "
+ "FROM customer "
+ "WHERE w_id = ? AND d_id= ? AND c_last=?",
new Object[] {customerWarehouseId, customerDistrictId, lastName});
int nameCount = (int) (long) row[0];
if (nameCount % 2 == 0) {
nameCount++;
}
List<Object[]> resultSet =
executeParamQuery(
"SELECT c_id "
+ "FROM customer "
+ "WHERE w_id = ? AND d_id= ? AND c_last=? "
+ "ORDER BY c_first",
new Object[] {customerWarehouseId, customerDistrictId, lastName});
for (int counter = 0; counter < Math.min(nameCount, resultSet.size()); counter++) {
customerId = (long) resultSet.get(counter)[0];
}
}
row =
paramQueryRow(
"SELECT c_first, c_middle, c_last, c_street_1, c_street_2, c_city, c_state, c_zip, c_phone, c_credit, c_credit_lim, c_discount, c_balance, c_ytd_payment, c_since "
+ "FROM customer "
+ "WHERE w_id = ? AND d_id= ? AND c_id=? FOR UPDATE",
new Object[] {customerWarehouseId, customerDistrictId, customerId});
String firstName = (String) row[0];
String middleName = (String) row[1];
lastName = (String) row[2];
String street1 = (String) row[3];
String street2 = (String) row[4];
String city = (String) row[5];
String state = (String) row[6];
String zip = (String) row[7];
String phone = (String) row[8];
String credit = (String) row[9];
long creditLimit = (long) row[10];
BigDecimal discount = (BigDecimal) row[11];
BigDecimal balance = (BigDecimal) row[12];
BigDecimal ytdPayment = (BigDecimal) row[13];
Timestamp since = (Timestamp) row[14];
balance = balance.subtract(amount);
ytdPayment = ytdPayment.add(amount);
if ("BC".equals(credit)) {
row =
paramQueryRow(
"SELECT c_data " + "FROM customer " + "WHERE w_id = ? AND d_id=? AND c_id= ?",
new Object[] {customerWarehouseId, customerDistrictId, customerId});
String customerData = (String) row[0];
String newCustomerData =
String.format(
"| %4d %2d %4d %2d %4d $%7.2f %12s %24s",
customerId,
customerDistrictId,
customerWarehouseId,
districtId,
warehouseId,
amount,
LocalDateTime.now(),
customerData);
if (newCustomerData.length() > 500) {
newCustomerData = newCustomerData.substring(0, 500);
}
executeParamStatement(
"UPDATE customer "
+ "SET c_balance=?, c_ytd_payment=?, c_data=? "
+ "WHERE w_id = ? AND d_id=? AND c_id=?",
new Object[] {
balance,
ytdPayment,
newCustomerData,
customerWarehouseId,
customerDistrictId,
customerId
});
} else {
executeParamStatement(
"UPDATE customer "
+ "SET c_balance=?, c_ytd_payment=? "
+ "WHERE w_id = ? AND d_id=? AND c_id=?",
new Object[] {balance, ytdPayment, customerWarehouseId, customerDistrictId, customerId});
}
executeParamStatement(
"INSERT INTO history (d_id, w_id, c_id, h_d_id, h_w_id, h_date, h_amount, h_data) "
+ "VALUES (?,?,?,?,?,CURRENT_TIMESTAMP,?,?)",
new Object[] {
customerDistrictId,
customerWarehouseId,
customerId,
districtId,
warehouseId,
amount,
String.format("%10s %10s", warehouseName, districtName)
});
LOG.debug("Committing payment");
executeStatement("commit");
}