Apache Lucene open-source search software
SOLR-13347: Add support for reading/writing UUID from/to TransactionLog (#681)
Thomas Wöckinger828960927f50
diff --git a/solr/core/src/java/org/apache/solr/update/TransactionLog.java b/solr/core/src/java/org/apache/solr/update/TransactionLog.javaindex a5f55c2033a..b4db6ef0cb0 100644--- a/solr/core/src/java/org/apache/solr/update/TransactionLog.java+++ b/solr/core/src/java/org/apache/solr/update/TransactionLog.java@@ -102,6 +102,7 @@ public class TransactionLog implements Closeable {};public class LogCodec extends JavaBinCodec {+public LogCodec(JavaBinCodec.ObjectResolver resolver) {super(resolver);}@@ -128,7 +129,7 @@ public class TransactionLog implements Closeable {public CharSequence readExternString(DataInputInputStream fis) throws IOException {int idx = readSize(fis);if (idx != 0) {// idx != 0 is the index of the extern string- // no need to synchronize globalStringList - it's only updated before the first record is written to the log+ // no need to synchronize globalStringList - it's only updated before the first record is written to the logreturn globalStringList.get(idx - 1);} else {// idx == 0 means it has a string value// this shouldn't happen with this codec subclass.@@ -136,6 +137,25 @@ public class TransactionLog implements Closeable {}}+ @Override+ protected Object readObject(DataInputInputStream dis) throws IOException {+ if (UUID == tagByte) {+ return new java.util.UUID(dis.readLong(), dis.readLong());+ }+ return super.readObject(dis);+ }++ @Override+ public boolean writePrimitive(Object val) throws IOException {+ if (val instanceof java.util.UUID) {+ java.util.UUID uuid = (java.util.UUID) val;+ daos.writeByte(UUID);+ daos.writeLong(uuid.getMostSignificantBits());+ daos.writeLong(uuid.getLeastSignificantBits());+ return true;+ }+ return super.writePrimitive(val);+ }}TransactionLog(File tlogFile, Collection<String> globalStrings) {diff --git a/solr/core/src/test/org/apache/solr/update/TransactionLogTest.java b/solr/core/src/test/org/apache/solr/update/TransactionLogTest.javaindex 167b3b469b5..594bc898c35 100644--- a/solr/core/src/test/org/apache/solr/update/TransactionLogTest.java+++ b/solr/core/src/test/org/apache/solr/update/TransactionLogTest.java@@ -18,19 +18,24 @@package org.apache.solr.update;import java.io.File;+import java.io.IOException;import java.nio.file.Path;import java.util.ArrayList;+import java.util.List;import java.util.Locale;+import java.util.UUID;import org.apache.solr.SolrTestCase;import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.update.TransactionLog.LogReader;import org.junit.Test;public class TransactionLogTest extends SolrTestCase {@Testpublic void testBigLastAddSize() {- String tlogFileName = String.format(Locale.ROOT, UpdateLog.LOG_FILENAME_PATTERN, UpdateLog.TLOG_NAME, Long.MAX_VALUE);+ String tlogFileName = String.format(Locale.ROOT, UpdateLog.LOG_FILENAME_PATTERN, UpdateLog.TLOG_NAME,+ Long.MAX_VALUE);Path path = createTempDir();File logFile = new File(path.toFile(), tlogFileName);try (TransactionLog transactionLog = new TransactionLog(logFile, new ArrayList<>())) {@@ -41,4 +46,30 @@ public class TransactionLogTest extends SolrTestCase {}}+ @Test+ public void testUUID() throws IOException, InterruptedException {+ String tlogFileName = String.format(Locale.ROOT, UpdateLog.LOG_FILENAME_PATTERN, UpdateLog.TLOG_NAME,+ Long.MAX_VALUE);+ Path path = createTempDir();+ File logFile = new File(path.toFile(), tlogFileName);+ UUID uuid = UUID.randomUUID();+ try (TransactionLog tlog = new TransactionLog(logFile, new ArrayList<>())) {+ tlog.deleteOnClose = false;+ AddUpdateCommand updateCommand = new AddUpdateCommand(null);++ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("uuid", uuid);+ updateCommand.solrDoc = doc;++ tlog.write(updateCommand);+ }++ try (TransactionLog tlog = new TransactionLog(logFile, new ArrayList<>(), true)) {+ LogReader reader = tlog.getReader(0);+ Object entry = reader.next();+ assertNotNull(entry);+ SolrInputDocument doc = (SolrInputDocument) ((List<?>) entry).get(2);+ assertEquals(uuid, (UUID) doc.getFieldValue("uuid"));+ }+ }}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.javaindex 434e2f90d9f..7d6f2e48352 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java@@ -100,6 +100,7 @@ public class JavaBinCodec implements PushWriter {MAP_ENTRY_ITER = 17,ENUM_FIELD_VALUE = 18,MAP_ENTRY = 19,+ UUID = 20, // This is reserved to be used only in LogCodec// types that combine tag + length (or other info) in a single byteTAG_AND_LEN = (byte) (1 << 5),STR = (byte) (1 << 5),