88 lines
2.3 KiB
Java
88 lines
2.3 KiB
Java
package org.warp.jcwdb.ann;
|
|
|
|
import org.warp.jcwdb.FileIndexManager;
|
|
|
|
import java.io.IOError;
|
|
import java.io.IOException;
|
|
|
|
public class DBObjectIndicesManager {
|
|
private final FileIndexManager indices;
|
|
|
|
DBObjectIndicesManager(FileIndexManager indices) {
|
|
this.indices = indices;
|
|
}
|
|
|
|
public long allocate(int fieldsCount, int propertiesCount) {
|
|
long uid = indices.add(calculateObjectSize(fieldsCount, propertiesCount));
|
|
//System.err.println("ALLOCATED UID " + uid);
|
|
return uid;
|
|
}
|
|
|
|
public void setNull(long uid) throws IOException {
|
|
indices.set(uid, 0, (w) -> w.write(new byte[0]));
|
|
}
|
|
|
|
public void set(long uid, long[] fields, long[] properties) throws IOException {
|
|
indices.set(uid, calculateObjectSize(fields, properties), (w) -> {
|
|
w.writeInt(fields.length);
|
|
w.writeInt(properties.length);
|
|
for (int i = 0; i < fields.length; i++) {
|
|
w.writeLong(fields[i]);
|
|
}
|
|
for (int i = 0; i < properties.length; i++) {
|
|
w.writeLong(properties[i]);
|
|
}
|
|
});
|
|
}
|
|
|
|
public DBObjectInfo get(long uid) throws IOException {
|
|
return indices.get(uid, (i, size) -> {
|
|
if (size < Integer.BYTES * 2) {
|
|
return null;
|
|
}
|
|
long[] indices = new long[i.readInt()];
|
|
long[] properties = new long[i.readInt()];
|
|
if (size != calculateObjectSize(indices, properties)) {
|
|
throw new IOError(new IOException("The size of the object is different!"));
|
|
}
|
|
for (int indicesI = 0; indicesI < indices.length; indicesI++) {
|
|
indices[indicesI] = i.readLong();
|
|
}
|
|
for (int propertiesI = 0; propertiesI < properties.length; propertiesI++) {
|
|
properties[propertiesI] = i.readLong();
|
|
}
|
|
return new DBObjectInfo(indices, properties);
|
|
});
|
|
}
|
|
|
|
public boolean has(long uid) {
|
|
return indices.has(uid);
|
|
}
|
|
|
|
private int calculateObjectSize(long[] fields, long[] properties) {
|
|
return calculateObjectSize(fields.length, properties.length);
|
|
}
|
|
|
|
private int calculateObjectSize(int fieldsCount, int propertiesCount) {
|
|
return Integer.BYTES * 2 + (fieldsCount + propertiesCount) * Long.BYTES;
|
|
}
|
|
|
|
public class DBObjectInfo {
|
|
private final long[] fields;
|
|
private final long[] properties;
|
|
|
|
public DBObjectInfo(long[] fields, long[] properties) {
|
|
this.fields = fields;
|
|
this.properties = properties;
|
|
}
|
|
|
|
public long[] getFields() {
|
|
return fields;
|
|
}
|
|
|
|
public long[] getProperties() {
|
|
return properties;
|
|
}
|
|
}
|
|
}
|