strangedb/src/main/java/it/cavallium/strangedb/lists/StrandeDbList.java

95 lines
2.2 KiB
Java
Raw Normal View History

2019-03-07 11:41:45 +01:00
package it.cavallium.strangedb.lists;
2019-01-22 12:33:58 +01:00
import it.unimi.dsi.fastutil.longs.LongArrayList;
2019-03-07 11:41:45 +01:00
import it.cavallium.strangedb.EnhancedObject;
import it.cavallium.strangedb.database.IDatabaseTools;
2019-01-22 12:33:58 +01:00
import java.io.IOException;
import java.util.StringJoiner;
2019-03-07 11:41:45 +01:00
public abstract class StrandeDbList<T> extends EnhancedObject {
2019-01-22 12:33:58 +01:00
private final Object indicesAccessLock = new Object();
2019-02-05 17:56:28 +01:00
protected abstract LongArrayList getIndices();
2019-01-22 12:33:58 +01:00
2019-03-07 11:41:45 +01:00
public StrandeDbList() {
2019-02-01 00:04:51 +01:00
}
2019-03-07 11:41:45 +01:00
public StrandeDbList(IDatabaseTools databaseTools) throws IOException {
2019-03-02 17:47:24 +01:00
super(databaseTools);
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-05 17:56:28 +01:00
long uid = getIndices().getLong(index);
2019-02-01 00:04:51 +01:00
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 {
2019-03-02 17:47:24 +01:00
long uid = databaseTools.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
2019-02-05 17:56:28 +01:00
getIndices().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) {
2019-02-05 17:56:28 +01:00
long uid = getIndices().getLong(index);
writeItemToDisk(uid, value);
2019-01-22 12:33:58 +01:00
}
}
2019-02-01 00:04:51 +01:00
public void set(int index, T value) throws IOException {
2019-03-02 17:47:24 +01:00
long uid = databaseTools.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
2019-02-05 17:56:28 +01:00
getIndices().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 {
2019-03-02 17:47:24 +01:00
long uid = databaseTools.getObjectsIO().newNullObject();
2019-01-22 12:33:58 +01:00
synchronized (indicesAccessLock) {
2019-02-05 17:56:28 +01:00
getIndices().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) {
2019-02-05 17:56:28 +01:00
if (getIndices().size() > 0) {
return get(getIndices().size() - 1);
2019-01-22 12:33:58 +01:00
} else {
return null;
}
}
}
public boolean isEmpty() {
synchronized (indicesAccessLock) {
2019-02-05 17:56:28 +01:00
return getIndices().size() <= 0;
2019-01-22 12:33:58 +01:00
}
}
public int size() {
synchronized (indicesAccessLock) {
2019-02-05 17:56:28 +01:00
return getIndices().size();
2019-01-22 12:33:58 +01:00
}
}
protected abstract T loadItem(long uid) throws IOException;
protected abstract void writeItemToDisk(long uid, T item) throws IOException;
@Override
public String toString() {
2019-03-07 11:41:45 +01:00
return new StringJoiner(", ", StrandeDbList.class.getSimpleName() + "[", "]")
2019-02-05 17:56:28 +01:00
.add(getIndices().size() + " items")
2019-01-22 12:33:58 +01:00
.toString();
}
}