strangedb/src/main/java/org/warp/jcwdb/MixedIndexDatabase.java

84 lines
2.0 KiB
Java
Raw Normal View History

2018-11-19 15:16:12 +01:00
package org.warp.jcwdb;
import it.unimi.dsi.fastutil.longs.Long2LongLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import java.io.IOException;
import java.nio.file.Path;
2018-12-11 23:00:51 +01:00
import java.util.function.Consumer;
2018-11-19 15:16:12 +01:00
public class MixedIndexDatabase implements IndexManager {
private final FileIndexManager fileIndices;
private final CacheIndexManager cacheIndices;
2018-12-19 12:19:03 +01:00
public MixedIndexDatabase(Path dataFile, Path metadataFile) throws IOException {
2018-11-27 10:26:01 +01:00
this.fileIndices = new FileIndexManager(dataFile, metadataFile);
this.cacheIndices = new CacheIndexManager();
2018-11-19 15:16:12 +01:00
}
@Override
2018-11-21 01:02:25 +01:00
public <T> T get(long index, DBReader<T> reader) throws IOException {
if (cacheIndices.has(index)) {
return cacheIndices.get(index, reader);
} else {
return fileIndices.get(index, reader);
}
2018-11-19 15:16:12 +01:00
}
@Override
2018-11-21 01:02:25 +01:00
public int getType(long index) throws IOException {
if (cacheIndices.has(index)) {
return cacheIndices.getType(index);
} else {
return fileIndices.getType(index);
}
2018-11-20 18:39:48 +01:00
}
2018-12-11 23:00:51 +01:00
@Override
public long getHash(long index) throws IOException {
if (cacheIndices.has(index)) {
return cacheIndices.getHash(index);
} else {
return fileIndices.getHash(index);
}
}
2018-11-20 18:39:48 +01:00
@Override
2018-11-21 01:02:25 +01:00
public <T> long add(DBDataOutput<T> writer) throws IOException {
return fileIndices.add(writer);
2018-11-19 15:16:12 +01:00
}
@Override
2018-12-11 23:00:51 +01:00
public <T> IndexDetails set(long index, DBDataOutput<T> writer) throws IOException {
2018-11-21 01:02:25 +01:00
if (cacheIndices.has(index)) {
2018-12-11 23:00:51 +01:00
return cacheIndices.set(index, writer);
2018-11-21 01:02:25 +01:00
} else {
2018-12-11 23:00:51 +01:00
return fileIndices.set(index, writer);
2018-11-21 01:02:25 +01:00
}
}
2018-11-19 15:16:12 +01:00
2018-11-21 01:02:25 +01:00
@Override
public void delete(long index) throws IOException {
cacheIndices.delete(index);
fileIndices.delete(index);
2018-11-19 15:16:12 +01:00
}
@Override
public boolean has(long index) {
2018-11-21 01:02:25 +01:00
return cacheIndices.has(index) || fileIndices.has(index);
2018-11-19 15:16:12 +01:00
}
2018-11-20 18:39:48 +01:00
@Override
public void close() throws IOException {
// TODO: move all cached indices to filesIndices before closing.
this.cacheIndices.close();
this.fileIndices.close();
}
2018-11-27 17:47:19 +01:00
@Override
public long clean() {
return fileIndices.clean()
+ cacheIndices.clean();
}
2018-11-19 15:16:12 +01:00
}