in jdbc/src/main/java/site/ycsb/db/JdbcDBClient.java [432:497]
public Status insert(String tableName, String key, Map<String, ByteIterator> values) {
try {
int numFields = values.size();
OrderedFieldInfo fieldInfo = getFieldInfo(values);
StatementType type = new StatementType(StatementType.Type.INSERT, tableName,
numFields, fieldInfo.getFieldKeys(), getShardIndexByKey(key));
PreparedStatement insertStatement = cachedStatements.get(type);
if (insertStatement == null) {
insertStatement = createAndCacheInsertStatement(type, key);
}
insertStatement.setString(1, key);
int index = 2;
for (String value: fieldInfo.getFieldValues()) {
insertStatement.setString(index++, value);
}
// Using the batch insert API
if (batchUpdates) {
insertStatement.addBatch();
// Check for a sane batch size
if (batchSize > 0) {
// Commit the batch after it grows beyond the configured size
if (++numRowsInBatch % batchSize == 0) {
int[] results = insertStatement.executeBatch();
for (int r : results) {
// Acceptable values are 1 and SUCCESS_NO_INFO (-2) from reWriteBatchedInserts=true
if (r != 1 && r != -2) {
return Status.ERROR;
}
}
// If autoCommit is off, make sure we commit the batch
if (!autoCommit) {
getShardConnectionByKey(key).commit();
}
return Status.OK;
} // else, the default value of -1 or a nonsense. Treat it as an infinitely large batch.
} // else, we let the batch accumulate
// Added element to the batch, potentially committing the batch too.
return Status.BATCHED_OK;
} else {
// Normal update
int result = insertStatement.executeUpdate();
// If we are not autoCommit, we might have to commit now
if (!autoCommit) {
// Let updates be batcher locally
if (batchSize > 0) {
if (++numRowsInBatch % batchSize == 0) {
// Send the batch of updates
getShardConnectionByKey(key).commit();
}
// uhh
return Status.OK;
} else {
// Commit each update
getShardConnectionByKey(key).commit();
}
}
if (result == 1) {
return Status.OK;
}
}
return Status.UNEXPECTED_STATE;
} catch (SQLException e) {
System.err.println("Error in processing insert to table: " + tableName + e);
return Status.ERROR;
}
}