Skip to content

Commit

Permalink
feat: implement method to get size of table in IndexManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed Jun 5, 2024
1 parent 36b1e30 commit 4acb940
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.sepgh.internal.index;

import com.github.sepgh.internal.index.tree.node.BaseTreeNode;
import com.github.sepgh.internal.storage.IndexStorageManager;

import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

public class BloomIndexManagerDecorator extends IndexManagerDecorator {

private final IndexStorageManager indexStorageManager;

public BloomIndexManagerDecorator(IndexManager indexManager, int size, double tolerance, IndexStorageManager indexStorageManager) {
super(indexManager);
this.indexStorageManager = indexStorageManager;

}

@Override
public BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws ExecutionException, InterruptedException, IOException {
return super.addIndex(table, identifier, pointer);
}

@Override
public Optional<Pointer> getIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException {
return super.getIndex(table, identifier);
}

@Override
public boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException {
return super.removeIndex(table, identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@
import com.google.common.cache.CacheBuilder;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;

public class CachedIndexManagerDecorator extends IndexManagerDecorator {
private final Cache<TableIdentifier, Pointer> cache;
private final Map<Integer, Integer> sizeCache;

public CachedIndexManagerDecorator(IndexManager indexManager, int maxSize) {
this(indexManager, CacheBuilder.newBuilder().maximumSize(maxSize).initialCapacity(10).build());
}
public CachedIndexManagerDecorator(IndexManager indexManager, Cache<TableIdentifier, Pointer> cache) {
super(indexManager);
this.cache = cache;
this.sizeCache = new ConcurrentHashMap<>();
}

@Override
public BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws ExecutionException, InterruptedException, IOException {
BaseTreeNode baseTreeNode = super.addIndex(table, identifier, pointer);
cache.put(new TableIdentifier(table, identifier), pointer);
sizeCache.computeIfPresent(table, (k, v) -> v + 1);
return baseTreeNode;
}

Expand All @@ -42,11 +47,23 @@ public Optional<Pointer> getIndex(int table, long identifier) throws ExecutionEx
public boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException {
if (super.removeIndex(table, identifier)) {
cache.invalidate(new TableIdentifier(table, identifier));
sizeCache.computeIfPresent(table, (k, v) -> v - 1);
return true;
}
return false;
}

@Override
public int size(int table) {
return sizeCache.computeIfAbsent(table, key -> {
try {
return super.size(table);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
});
}

public record TableIdentifier(int table, long identifier){
@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IndexManager {
BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws ExecutionException, InterruptedException, IOException;
Optional<Pointer> getIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException;
boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException;
int size(int table) throws InterruptedException, ExecutionException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public Optional<Pointer> getIndex(int table, long identifier) throws ExecutionEx
public boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException, IOException {
return this.indexManager.removeIndex(table, identifier);
}

@Override
public int size(int table) throws ExecutionException, InterruptedException {
return this.indexManager.size(table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.github.sepgh.internal.index.IndexManager;
import com.github.sepgh.internal.index.Pointer;
import com.github.sepgh.internal.index.tree.node.BaseTreeNode;
import com.github.sepgh.internal.index.tree.node.InternalTreeNode;
import com.github.sepgh.internal.index.tree.node.LeafTreeNode;
import com.github.sepgh.internal.storage.IndexStorageManager;
import com.github.sepgh.internal.storage.IndexTreeNodeIO;
import com.github.sepgh.internal.storage.session.ImmediateCommitIndexIOSession;
import com.github.sepgh.internal.storage.session.IndexIOSession;
import com.github.sepgh.internal.storage.session.IndexIOSessionFactory;
Expand Down Expand Up @@ -55,6 +57,33 @@ public boolean removeIndex(int table, long identifier) throws ExecutionException
return new BPlusTreeIndexDeleteOperation(degree, table, indexIOSession).removeIndex(root, identifier);
}

@Override
public int size(int table) throws InterruptedException, ExecutionException {
Optional<IndexStorageManager.NodeData> optionalNodeData = this.indexStorageManager.getRoot(table).get();
if (optionalNodeData.isEmpty())
return 0;

BaseTreeNode root = BaseTreeNode.fromNodeData(optionalNodeData.get());
if (root.isLeaf()){
return root.getKeyList(degree).size();
}

BaseTreeNode curr = root;
while (!curr.isLeaf()) {
curr = IndexTreeNodeIO.read(indexStorageManager, table, ((InternalTreeNode) curr).getChildrenList().getFirst());
}

int size = curr.getKeyList(degree).size();
Optional<Pointer> optionalNext = ((LeafTreeNode) curr).getNextSiblingPointer(degree);
while (optionalNext.isPresent()){
BaseTreeNode nextNode = IndexTreeNodeIO.read(indexStorageManager, table, optionalNext.get());
size += nextNode.getKeyList(degree).size();
optionalNext = ((LeafTreeNode) nextNode).getNextSiblingPointer(degree);
}

return size;
}

private BaseTreeNode getRoot(IndexIOSession indexIOSession) throws ExecutionException, InterruptedException, IOException {
Optional<BaseTreeNode> optionalRoot = indexIOSession.getRoot();
if (optionalRoot.isPresent()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.github.sepgh.internal.storage.CompactFileIndexStorageManager.INDEX_FILE_NAME;

Expand Down Expand Up @@ -98,6 +101,34 @@ public void findIndexSuccessfully() throws IOException, ExecutionException, Inte
Assertions.assertEquals(dataPointer, optionalPointer.get());
}

@Test
@Timeout(value = 2)
public void getTableSize() throws IOException, ExecutionException, InterruptedException {
HeaderManager headerManager = new InMemoryHeaderManager(header);
CompactFileIndexStorageManager compactFileIndexStorageManager = new CompactFileIndexStorageManager(dbPath, headerManager, engineConfig);

IndexManager indexManager = new TableLevelAsyncIndexManagerDecorator(new BPlusTreeIndexManager(degree, compactFileIndexStorageManager));
Pointer dataPointer = new Pointer(Pointer.TYPE_DATA, 100, 0);

ExecutorService executorService = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch = new CountDownLatch(100);
for(long i = 1; i <= 100; i++){
long finalI = i;
executorService.submit(() -> {
try {
indexManager.addIndex(1, finalI, dataPointer);
} catch (ExecutionException | InterruptedException | IOException e) {
throw new RuntimeException(e);
} finally {
countDownLatch.countDown();
}
});
}

countDownLatch.await();
Assertions.assertEquals(100, indexManager.size(1));
}

@Test
@Timeout(value = 2)
public void findIndexFailure() throws IOException, ExecutionException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ public void findIndexSuccessfully() throws IOException, ExecutionException, Inte
Assertions.assertEquals(dataPointer, optionalPointer.get());
}

@Test
@Timeout(value = 2)
public void getTableSize() throws IOException, ExecutionException, InterruptedException {
HeaderManager headerManager = new InMemoryHeaderManager(header);
CompactFileIndexStorageManager compactFileIndexStorageManager = new CompactFileIndexStorageManager(dbPath, headerManager, engineConfig);

IndexManager indexManager = new BPlusTreeIndexManager(degree, compactFileIndexStorageManager);
Pointer dataPointer = new Pointer(Pointer.TYPE_DATA, 100, 0);

for(long i = 1; i <= 100; i++)
indexManager.addIndex(1, i, dataPointer);

Assertions.assertEquals(100, indexManager.size(1));
}


@Test
@Timeout(value = 2)
public void findIndexFailure() throws IOException, ExecutionException, InterruptedException {
Expand Down

0 comments on commit 4acb940

Please sign in to comment.