Insert messages individually.

This commit is contained in:
Jon Chambers 2020-10-03 13:00:22 -04:00 committed by Jon Chambers
parent fb0941bbe9
commit 158bfe4816
1 changed files with 17 additions and 19 deletions

View File

@ -67,32 +67,30 @@ public class Messages {
public void store(final List<Envelope> messages, final String destination, final long destinationDevice) { public void store(final List<Envelope> messages, final String destination, final long destinationDevice) {
database.use(jdbi -> jdbi.useHandle(handle -> { database.use(jdbi -> jdbi.useHandle(handle -> {
try (final Timer.Context ignored = storeTimer.time()) { try (final Timer.Context ignored = storeTimer.time()) {
final PreparedBatch batch = handle.prepareBatch("INSERT INTO messages (" + GUID + ", " + TYPE + ", " + RELAY + ", " + TIMESTAMP + ", " + SERVER_TIMESTAMP + ", " + SOURCE + ", " + SOURCE_UUID + ", " + SOURCE_DEVICE + ", " + DESTINATION + ", " + DESTINATION_DEVICE + ", " + MESSAGE + ", " + CONTENT + ") " +
"VALUES (:guid, :type, :relay, :timestamp, :server_timestamp, :source, :source_uuid, :source_device, :destination, :destination_device, :message, :content)");
for (final Envelope message : messages) { for (final Envelope message : messages) {
if (message.getServerGuid() == null) { if (message.getServerGuid() == null) {
insertNullGuidMeter.mark(); insertNullGuidMeter.mark();
} }
batch.bind("guid", UUID.fromString(message.getServerGuid())) handle.createUpdate("INSERT INTO messages (" + GUID + ", " + TYPE + ", " + RELAY + ", " + TIMESTAMP + ", " + SERVER_TIMESTAMP + ", " + SOURCE + ", " + SOURCE_UUID + ", " + SOURCE_DEVICE + ", " + DESTINATION + ", " + DESTINATION_DEVICE + ", " + MESSAGE + ", " + CONTENT + ") " +
.bind("destination", destination) "VALUES (:guid, :type, :relay, :timestamp, :server_timestamp, :source, :source_uuid, :source_device, :destination, :destination_device, :message, :content)")
.bind("destination_device", destinationDevice) .bind("guid", UUID.fromString(message.getServerGuid()))
.bind("type", message.getType().getNumber()) .bind("destination", destination)
.bind("relay", message.getRelay()) .bind("destination_device", destinationDevice)
.bind("timestamp", message.getTimestamp()) .bind("type", message.getType().getNumber())
.bind("server_timestamp", message.getServerTimestamp()) .bind("relay", message.getRelay())
.bind("source", message.hasSource() ? message.getSource() : null) .bind("timestamp", message.getTimestamp())
.bind("source_uuid", message.hasSourceUuid() ? UUID.fromString(message.getSourceUuid()) : null) .bind("server_timestamp", message.getServerTimestamp())
.bind("source_device", message.hasSourceDevice() ? message.getSourceDevice() : null) .bind("source", message.hasSource() ? message.getSource() : null)
.bind("message", message.hasLegacyMessage() ? message.getLegacyMessage().toByteArray() : null) .bind("source_uuid", message.hasSourceUuid() ? UUID.fromString(message.getSourceUuid()) : null)
.bind("content", message.hasContent() ? message.getContent().toByteArray() : null) .bind("source_device", message.hasSourceDevice() ? message.getSourceDevice() : null)
.add(); .bind("message", message.hasLegacyMessage() ? message.getLegacyMessage().toByteArray() : null)
.bind("content", message.hasContent() ? message.getContent().toByteArray() : null)
.execute();
} }
batch.execute();
storeSizeHistogram.update(messages.size());
} }
storeSizeHistogram.update(messages.size());
})); }));
} }