strangedb/src/main/java/org/warp/cowdb/lists/CowList.java

102 lines
2.2 KiB
Java
Raw Normal View History

2019-02-01 00:04:51 +01:00
package org.warp.cowdb.lists;
2019-01-22 12:33:58 +01:00
import it.unimi.dsi.fastutil.longs.LongArrayList;
2019-02-01 00:04:51 +01:00
import org.warp.cowdb.EnhancedObject;
import org.warp.cowdb.IDatabase;
import org.warp.jcwdb.ann.DBDataType;
import org.warp.jcwdb.ann.DBField;
2019-01-22 12:33:58 +01:00
import java.io.IOException;
import java.util.StringJoiner;
2019-02-01 00:04:51 +01:00
public abstract class CowList<T> extends EnhancedObject {
2019-01-22 12:33:58 +01:00
private final Object indicesAccessLock = new Object();
2019-02-01 00:04:51 +01:00
@DBField(id = 0, type = DBDataType.REFERENCES_LIST)
2019-01-22 12:33:58 +01:00
private LongArrayList indices;
2019-02-01 00:04:51 +01:00
public CowList() {
}
2019-02-01 00:04:51 +01:00
public CowList(IDatabase database) throws IOException {
2019-01-22 12:33:58 +01:00
super(database);
}
@Override
2019-02-01 00:04:51 +01:00
public void initialize() throws IOException {
indices = new LongArrayList();
2019-01-22 12:33:58 +01:00
}
2019-02-01 00:04:51 +01:00
public T get(int index) throws IOException {
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
2019-02-01 00:04:51 +01:00
long uid = indices.getLong(index);
return loadItem(uid);
2019-01-22 12:33:58 +01:00
}
}
2019-02-01 00:04:51 +01:00
public void add(T value) throws IOException {
long uid = database.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
indices.add(uid);
2019-02-01 00:04:51 +01:00
writeItemToDisk(uid, value);
2019-01-22 12:33:58 +01:00
}
}
2019-02-01 00:04:51 +01:00
public void update(int index, T value) throws IOException {
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
set(index, value);
}
}
2019-02-01 00:04:51 +01:00
public void set(int index, T value) throws IOException {
long uid = database.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
indices.set(index, uid);
2019-02-01 00:04:51 +01:00
writeItemToDisk(uid, value);
2019-01-22 12:33:58 +01:00
}
}
2019-02-01 00:04:51 +01:00
public void add(int index, T value) throws IOException {
long uid = database.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
indices.add(index, uid);
2019-02-01 00:04:51 +01:00
writeItemToDisk(uid, value);
2019-01-22 12:33:58 +01:00
}
}
2019-02-01 00:04:51 +01:00
public T getLast() throws IOException {
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
if (indices.size() > 0) {
return get(indices.size() - 1);
} else {
return null;
}
}
}
public boolean isEmpty() {
synchronized (indicesAccessLock) {
return indices.size() <= 0;
}
}
public int size() {
synchronized (indicesAccessLock) {
return indices.size();
}
}
protected abstract T loadItem(long uid) throws IOException;
protected abstract void writeItemToDisk(long uid, T item) throws IOException;
@Override
public String toString() {
2019-02-01 00:04:51 +01:00
return new StringJoiner(", ", CowList.class.getSimpleName() + "[", "]")
2019-01-22 12:33:58 +01:00
.add(indices.size() + " items")
.toString();
}
}