add method isEmpty to PersistentMap

This commit is contained in:
2021-10-16 16:59:58 +02:00
parent 3d72231415
commit cce2c052f2
2 changed files with 38 additions and 0 deletions

View File

@@ -188,6 +188,13 @@ public class PersistentMap<K, V> implements AutoCloseable {
private long version;
/**
*
* @param path file relative to {@code storageBasePath}
* @param storageBasePath base path
* @param keyEncoder encoder for keys
* @param valueEncoder encoder for values
*/
public PersistentMap(final Path path, final Path storageBasePath, final EncoderDecoder<K> keyEncoder,
final EncoderDecoder<V> valueEncoder) {
this.path = path;
@@ -633,6 +640,14 @@ public class PersistentMap<K, V> implements AutoCloseable {
return stats;
}
public synchronized boolean isEmpty() {
final long rootNodeOffset = readNodeOffsetOfRootNode();
final PersistentMapDiskNode node = getNode(rootNodeOffset);
final List<NodeEntry> entries = node.getEntries();
return entries.size() == 1; // the empty map has a single NodeEntry for the PersistentMapDiskNode.MAX_KEY
}
private void swapFiles(final Path newFile) throws IOException {
final Path backupFile = path.getParent().resolve(path.getFileName() + "."
+ DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss").format(OffsetDateTime.now()) + ".backup");