diff --git a/leveldb/src/main/java/org/iq80/leveldb/impl/Filename.java b/leveldb/src/main/java/org/iq80/leveldb/impl/Filename.java index b12ec99a..4350f73b 100755 --- a/leveldb/src/main/java/org/iq80/leveldb/impl/Filename.java +++ b/leveldb/src/main/java/org/iq80/leveldb/impl/Filename.java @@ -57,6 +57,11 @@ public static String logFileName(long number) * Return the name of the sstable with the specified number. */ public static String tableFileName(long number) + { + return makeFileName(number, "ldb"); + } + + public static String sstTableFileName(long number) { return makeFileName(number, "sst"); } @@ -149,6 +154,10 @@ else if (fileName.endsWith(".sst")) { long fileNumber = Long.parseLong(removeSuffix(fileName, ".sst")); return new FileInfo(FileType.TABLE, fileNumber); } + else if (fileName.endsWith(".ldb")) { + long fileNumber = Long.parseLong(removeSuffix(fileName, ".ldb")); + return new FileInfo(FileType.TABLE, fileNumber); + } else if (fileName.endsWith(".dbtmp")) { long fileNumber = Long.parseLong(removeSuffix(fileName, ".dbtmp")); return new FileInfo(FileType.TEMP, fileNumber); diff --git a/leveldb/src/main/java/org/iq80/leveldb/impl/TableCache.java b/leveldb/src/main/java/org/iq80/leveldb/impl/TableCache.java index ce2fb0e8..8363e434 100755 --- a/leveldb/src/main/java/org/iq80/leveldb/impl/TableCache.java +++ b/leveldb/src/main/java/org/iq80/leveldb/impl/TableCache.java @@ -121,6 +121,10 @@ private TableAndFile(File databaseDir, long fileNumber, UserComparator userCompa { String tableFileName = Filename.tableFileName(fileNumber); File tableFile = new File(databaseDir, tableFileName); + if (!tableFile.exists()) { + tableFileName = Filename.sstTableFileName(fileNumber); + tableFile = new File(databaseDir, tableFileName); + } FileInputStream fis = null; try { fis = new FileInputStream(tableFile); diff --git a/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java b/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java index cc833cbe..3321be38 100644 --- a/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java +++ b/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java @@ -56,6 +56,7 @@ import static org.iq80.leveldb.table.BlockHelper.beforeString; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -188,6 +189,35 @@ public void testGetFromVersions() assertEquals(db.get("foo"), "v1"); } + @Test + public void testFileExtensionCompatible() + throws Exception + { + DbStringWrapper db = new DbStringWrapper(new Options(), databaseDir); + db.put("foo", "v1"); + db.compactMemTable(); + db.put("bar", "v1"); + db.compactMemTable(); + db.put("xvf", "v1"); + db.compactMemTable(); + ImmutableList files = FileUtils.listFiles(databaseDir); + for (File file : files) { + Filename.FileInfo fileInfo = Filename.parseFileName(file); + assertNotNull(fileInfo); + + if (fileInfo.getFileType().equals(Filename.FileType.TABLE)) { + assertTrue(file.getName().endsWith(".ldb")); + long fileNumber = fileInfo.getFileNumber(); + File newFile = new File(databaseDir, Filename.sstTableFileName(fileNumber)); + file.renameTo(newFile); + } + } + db.reopen(); + assertEquals(db.get("foo"), "v1"); + assertEquals(db.get("bar"), "v1"); + assertEquals(db.get("xvf"), "v1"); + } + @Test public void testGetSnapshot() throws Exception