Skip to content

Commit

Permalink
rwlock usage in getNearest and more javadoc errata
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Sep 19, 2023
1 parent 8e78bcb commit 13da7d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public void remove(final SegmentIdentifier segmentId, final byte[] key) {
*
* <p>be sure to close this iterator, like in a try-with-resources block, otherwise a native
* memory leak might occur.
*
* @param segmentId id for the segment to iterate over.
* @return RocksIterator
*/
public RocksIterator getIterator(final SegmentIdentifier segmentId) {
return snapTx.getIterator(readOptions, columnFamilyMapper.apply(segmentId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,24 @@ public Optional<byte[]> get(final SegmentIdentifier segmentIdentifier, final byt
public Optional<NearestKeyValue> getNearestTo(
final SegmentIdentifier segmentIdentifier, final Bytes key) throws StorageException {

// TODO: revisit this for sort performance
Comparator<Map.Entry<Bytes, Optional<byte[]>>> comparing =
Comparator.comparing(
(Map.Entry<Bytes, Optional<byte[]>> a) -> a.getKey().commonPrefixLength(key))
.thenComparing((a, b) -> a.getKey().compareTo(b.getKey()));
return this.hashValueStore
.computeIfAbsent(segmentIdentifier, s -> new HashMap<>())
.entrySet()
.stream()
.sorted(comparing.reversed())
.findFirst()
.map(z -> new NearestKeyValue(z.getKey(), z.getValue()));
final Lock lock = rwLock.readLock();
lock.lock();
try {
// TODO: revisit this for sort performance
Comparator<Map.Entry<Bytes, Optional<byte[]>>> comparing =
Comparator.comparing(
(Map.Entry<Bytes, Optional<byte[]>> a) -> a.getKey().commonPrefixLength(key))
.thenComparing((a, b) -> a.getKey().compareTo(b.getKey()));
return this.hashValueStore
.computeIfAbsent(segmentIdentifier, s -> new HashMap<>())
.entrySet()
.stream()
.sorted(comparing.reversed())
.findFirst()
.map(z -> new NearestKeyValue(z.getKey(), z.getValue()));
} finally {
lock.unlock();
}
}

@Override
Expand Down

0 comments on commit 13da7d2

Please sign in to comment.