diff --git a/core/src/main/java/org/apache/accumulo/core/metadata/CompactionMetadata.java b/core/src/main/java/org/apache/accumulo/core/metadata/CompactionMetadata.java index 491bad70187..e01d4fd1e14 100644 --- a/core/src/main/java/org/apache/accumulo/core/metadata/CompactionMetadata.java +++ b/core/src/main/java/org/apache/accumulo/core/metadata/CompactionMetadata.java @@ -113,7 +113,7 @@ public String toJson() { GSonData jData = new GSonData(); jData.inputs = jobFiles.stream().map(stf -> new TabletFileCqMetadataGson(stf)).collect(toList()); - jData.tmp = new TabletFileCqMetadataGson(compactTmpName); + jData.tmp = new TabletFileCqMetadataGson(compactTmpName.insert()); jData.compactor = compactorId; jData.kind = kind.name(); jData.groupId = cgid.toString(); diff --git a/core/src/main/java/org/apache/accumulo/core/metadata/StoredTabletFile.java b/core/src/main/java/org/apache/accumulo/core/metadata/StoredTabletFile.java index d8f575d041c..bcce048b212 100644 --- a/core/src/main/java/org/apache/accumulo/core/metadata/StoredTabletFile.java +++ b/core/src/main/java/org/apache/accumulo/core/metadata/StoredTabletFile.java @@ -236,7 +236,7 @@ private static String serialize(TabletFileCq tabletFileCq) { * returned as an empty byte array **/ - private static byte[] encodeRow(final Key key) { + protected static byte[] encodeRow(final Key key) { final Text row = key != null ? key.getRow() : null; if (row != null) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -252,7 +252,7 @@ private static byte[] encodeRow(final Key key) { return new byte[0]; } - private static Text decodeRow(byte[] serialized) { + protected static Text decodeRow(byte[] serialized) { // Empty byte array means null row if (serialized.length == 0) { return null; @@ -292,26 +292,26 @@ public TabletFileCq(Path path, Range range) { } static class TabletFileCqMetadataGson { - private String path; - private byte[] startRow; - private byte[] endRow; + protected String metadataEntry; + protected String path; + protected byte[] startRow; + protected byte[] endRow; TabletFileCqMetadataGson() {} - TabletFileCqMetadataGson(AbstractTabletFile atf) { - path = Objects.requireNonNull(atf.path.toString()); - startRow = encodeRow(atf.range.getStartKey()); - endRow = encodeRow(atf.range.getEndKey()); + TabletFileCqMetadataGson(StoredTabletFile stf) { + metadataEntry = Objects.requireNonNull(stf.getMetadata()); + path = Objects.requireNonNull(stf.getMetadataPath()); + startRow = encodeRow(stf.range.getStartKey()); + endRow = encodeRow(stf.range.getEndKey()); } ReferencedTabletFile toReferencedTabletFile() { - return new ReferencedTabletFile(new Path(URI.create(path)), - new Range(decodeRow(startRow), true, decodeRow(endRow), false)); + return new StoredTabletFile(metadataEntry).getTabletFile(); } StoredTabletFile toStoredTabletFile() { - return StoredTabletFile.of(new Path(URI.create(path)), - new Range(decodeRow(startRow), true, decodeRow(endRow), false)); + return new StoredTabletFile(metadataEntry); } } diff --git a/core/src/test/java/org/apache/accumulo/core/metadata/StoredTabletFileTest.java b/core/src/test/java/org/apache/accumulo/core/metadata/StoredTabletFileTest.java index 998c9eab112..660b7bbe431 100644 --- a/core/src/test/java/org/apache/accumulo/core/metadata/StoredTabletFileTest.java +++ b/core/src/test/java/org/apache/accumulo/core/metadata/StoredTabletFileTest.java @@ -18,11 +18,23 @@ */ package org.apache.accumulo.core.metadata; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.net.URI; + +import org.apache.accumulo.core.data.Range; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.dataImpl.KeyExtent; +import org.apache.accumulo.core.metadata.StoredTabletFile.TabletFileCqMetadataGson; +import org.apache.accumulo.core.util.json.ByteArrayToBase64TypeAdapter; +import org.apache.hadoop.io.Text; import org.junit.jupiter.api.Test; +import com.google.gson.Gson; + public class StoredTabletFileTest { @Test @@ -37,4 +49,28 @@ public void fileConversionTest() { assertFalse(StoredTabletFile.fileNeedsConversion(s31)); assertFalse(StoredTabletFile.fileNeedsConversion(s31_untrimmed)); } + + @Test + public void testSerDe() { + Gson gson = ByteArrayToBase64TypeAdapter.createBase64Gson(); + String metadataEntry = + "{ \"path\":\"hdfs://localhost:8020/accumulo//tables//1/t-0000000/A000003v.rf\",\"startRow\":\"AmEA\",\"endRow\":\"AnoA\" }"; + URI normalizedPath = + URI.create("hdfs://localhost:8020/accumulo/tables/1/t-0000000/A000003v.rf"); + KeyExtent ke = new KeyExtent(TableId.of("t"), new Text("z"), new Text("a")); + Range r = ke.toDataRange(); + StoredTabletFile expected = new StoredTabletFile(metadataEntry); + TabletFileCqMetadataGson meta = new TabletFileCqMetadataGson(expected); + assertEquals(metadataEntry, meta.metadataEntry); + assertEquals(normalizedPath.toString(), meta.path); + assertArrayEquals(StoredTabletFile.encodeRow(r.getStartKey()), meta.startRow); + assertArrayEquals(StoredTabletFile.encodeRow(r.getEndKey()), meta.endRow); + String json = gson.toJson(meta); + System.out.println(json); + TabletFileCqMetadataGson des = gson.fromJson(json, TabletFileCqMetadataGson.class); + assertEquals(metadataEntry, des.metadataEntry); + assertEquals(normalizedPath.toString(), des.path); + assertArrayEquals(StoredTabletFile.encodeRow(r.getStartKey()), des.startRow); + assertArrayEquals(StoredTabletFile.encodeRow(r.getEndKey()), des.endRow); + } }