Skip to content

Commit fe8a923

Browse files
authored
Merge pull request #33 from scalableminds/fix-listkeys-omissions
Fix listKeys omissions
2 parents 273a251 + ff79f6a commit fe8a923

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/main/scala/com/scalableminds/fossildb/db/RocksDBStore.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class RocksDBKeyIterator(it: RocksIterator, prefix: Option[String]) extends Iter
112112
key
113113
}
114114

115+
def peek: String = {
116+
new String(it.key().map(_.toChar))
117+
}
118+
115119
}
116120

117121
class RocksDBIterator(it: RocksIterator, prefix: Option[String]) extends Iterator[KeyValuePair[Array[Byte]]] {
@@ -138,7 +142,7 @@ class RocksDBStore(db: RocksDB, handle: ColumnFamilyHandle) {
138142
new RocksDBIterator(it, prefix)
139143
}
140144

141-
def scanKeysOnly(key: String, prefix: Option[String]): Iterator[String] = {
145+
def scanKeysOnly(key: String, prefix: Option[String]): RocksDBKeyIterator = {
142146
val it = db.newIterator(handle)
143147
it.seek(key.getBytes())
144148
new RocksDBKeyIterator(it, prefix)

src/main/scala/com/scalableminds/fossildb/db/StoreManager.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,12 @@ class StoreManager(dataDir: Path, backupDir: Path, columnFamilies: List[String],
6969
def compactAllData() = {
7070
failDuringBackup
7171
failDuringRestore
72-
try {
73-
rocksDBManager.get.compactAllData()
74-
}
72+
rocksDBManager.get.compactAllData()
7573
}
7674

7775
def exportDB(newDataDir: String, newOptionsFilePathOpt: Option[String]) = {
7876
failDuringRestore
79-
try {
80-
rocksDBManager.get.exportToNewDB(Paths.get(newDataDir), newOptionsFilePathOpt)
81-
}
77+
rocksDBManager.get.exportToNewDB(Paths.get(newDataDir), newOptionsFilePathOpt)
8278
}
8379

8480
def close = {

src/main/scala/com/scalableminds/fossildb/db/VersionedKeyValueStore.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class VersionFilterIterator[T](it: Iterator[KeyValuePair[T]], version: Option[Lo
5858

5959
class KeyOnlyIterator[T](underlying: RocksDBStore, startAfterKey: Option[String]) extends Iterator[String] {
6060

61+
/*
62+
Note that seek in the underlying iterators either hits precisely or goes to the
63+
lexicographically *next* key. To achieve correct behavior with startAfterKey,
64+
we have to advance once in case of the exact hit.
65+
*/
66+
6167
private var currentKey: Option[String] = startAfterKey
6268

6369
private def compositeKeyFor(keyOpt: Option[String]) = keyOpt match {
@@ -67,13 +73,13 @@ class KeyOnlyIterator[T](underlying: RocksDBStore, startAfterKey: Option[String]
6773

6874
override def hasNext: Boolean = {
6975
val it = underlying.scanKeysOnly(compositeKeyFor(currentKey), None)
70-
if (it.hasNext && currentKey.isDefined) it.next
76+
if (it.hasNext && currentKey.isDefined && currentKey.contains(VersionedKey(it.peek).get.key)) it.next
7177
it.hasNext
7278
}
7379

7480
override def next(): String = {
7581
val it = underlying.scanKeysOnly(compositeKeyFor(currentKey), None)
76-
if (currentKey.isDefined) it.next
82+
if (it.hasNext && currentKey.isDefined && currentKey.contains(VersionedKey(it.peek).get.key)) it.next
7783
val nextKey = VersionedKey(it.next).get.key
7884
currentKey = Some(nextKey)
7985
nextKey

src/test/scala/com/scalableminds/fossildb/FossilDBSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ class FossilDBSuite extends FlatSpec with BeforeAndAfterEach with TestHelpers {
162162
assert(reply2.keys.length == 1)
163163
}
164164

165+
it should "return all keys despite lexicographic similarity" in {
166+
client.put(PutRequest(collectionA, "abb/1/1-[1,1,1]", Some(1), testData1))
167+
client.put(PutRequest(collectionA, "abc/1/1481800838-[3600,2717,121]", Some(123), testData2))
168+
client.put(PutRequest(collectionA, "abc/1/1481800839-[3601,2717,121]", Some(123), testData3))
169+
client.put(PutRequest(collectionA, "abc/1/1481800839-[3601,2717,121]", Some(125), testData3))
170+
client.put(PutRequest(collectionA, "abc/1/1481800839-[3601,2717,121]", Some(128), testData3))
171+
client.put(PutRequest(collectionA, "abc/1/1481800846-[3602,2717,121]", Some(123), testData2))
172+
173+
val reply = client.listKeys(ListKeysRequest(collectionA, None, Some("abb")))
174+
assert(reply.keys.length == 3)
175+
}
176+
165177
"GetMultipleVersions" should "return all versions in decending order if called without limits" in {
166178
client.put(PutRequest(collectionA, aKey, Some(0), testData1))
167179
client.put(PutRequest(collectionA, aKey, Some(1), testData2))

0 commit comments

Comments
 (0)