Made value in EntryReference private
This commit is contained in:
parent
b00cd8d6b5
commit
468e58994c
@ -1,6 +1,7 @@
|
|||||||
package org.warp.jcwdb;
|
package org.warp.jcwdb;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* You must have only a maximum of 1 reference for each index
|
* You must have only a maximum of 1 reference for each index
|
||||||
@ -10,7 +11,7 @@ public class EntryReference<T> implements Castable {
|
|||||||
private final JCWDatabase.EntryReferenceTools db;
|
private final JCWDatabase.EntryReferenceTools db;
|
||||||
private final long entryIndex;
|
private final long entryIndex;
|
||||||
private final DBTypeParser<T> parser;
|
private final DBTypeParser<T> parser;
|
||||||
public T value;
|
private T value;
|
||||||
private volatile boolean closed;
|
private volatile boolean closed;
|
||||||
private final Object closeLock = new Object();
|
private final Object closeLock = new Object();
|
||||||
|
|
||||||
@ -50,6 +51,28 @@ public class EntryReference<T> implements Castable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void editValue(Function<T, T> editFunction) throws IOException {
|
||||||
|
this.value = editFunction.apply(this.value);
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use editValue instead
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Deprecated()
|
||||||
|
public T getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT ATTEMPT TO MODIFY THE VALUE RETURNED
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public T getValueUnsafe() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T cast() {
|
public <T> T cast() {
|
||||||
return (T) this;
|
return (T) this;
|
||||||
|
@ -71,14 +71,20 @@ public class FileIndexManager implements IndexManager {
|
|||||||
@Override
|
@Override
|
||||||
public <T> void set(long index, DBDataOutput<T> data) throws IOException {
|
public <T> void set(long index, DBDataOutput<T> data) throws IOException {
|
||||||
checkClosed();
|
checkClosed();
|
||||||
|
final int dataSize = data.getSize();
|
||||||
final IndexDetails indexDetails = getIndexMetadataUnsafe(index);
|
final IndexDetails indexDetails = getIndexMetadataUnsafe(index);
|
||||||
if (indexDetails == null || indexDetails.getSize() != data.getSize()) { // TODO: should be indexDetails.getSize() < data.getSize(). Need to create a method to mark memory free if the size is bigger than the needed, instead of allocating a new space.
|
if (indexDetails == null || indexDetails.getSize() < dataSize) {
|
||||||
|
// Allocate new space
|
||||||
allocateAndWrite(index, data);
|
allocateAndWrite(index, data);
|
||||||
} else {
|
} else {
|
||||||
if (indexDetails.getSize() > data.getSize()) {
|
// Check if size changed
|
||||||
editIndex(index, indexDetails.getOffset(), data.getSize(), indexDetails.getType(), data.calculateHash());
|
if (indexDetails.getSize() > dataSize) {
|
||||||
fileAllocator.markFree(indexDetails.getOffset() + data.getSize(), data.getSize());
|
// Mark free the unused bytes
|
||||||
|
fileAllocator.markFree(indexDetails.getOffset() + dataSize, dataSize);
|
||||||
}
|
}
|
||||||
|
// Update index details
|
||||||
|
editIndex(index, indexDetails, indexDetails.getOffset(), dataSize, indexDetails.getType(), data.calculateHash());
|
||||||
|
// Write data
|
||||||
writeExact(indexDetails, data);
|
writeExact(indexDetails, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,12 +184,44 @@ public class FileIndexManager implements IndexManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit index data if a change is detected
|
||||||
|
* @param index
|
||||||
|
* @param oldData Old index data to check
|
||||||
|
* @param offset offset
|
||||||
|
* @param size size
|
||||||
|
* @param type type
|
||||||
|
* @param hash hash
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private IndexDetails editIndex(long index, IndexDetails oldData, long offset, int size, int type, long hash) {
|
||||||
|
if (oldData.getOffset() != offset || oldData.getSize() != size || oldData.getType() != type || oldData.getHash() != hash) {
|
||||||
|
editIndex(index, offset, size, type, hash);
|
||||||
|
} else {
|
||||||
|
return oldData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit index data
|
||||||
|
* @param index
|
||||||
|
* @param offset
|
||||||
|
* @param size
|
||||||
|
* @param type
|
||||||
|
* @param hash
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private IndexDetails editIndex(long index, long offset, int size, int type, long hash) {
|
private IndexDetails editIndex(long index, long offset, int size, int type, long hash) {
|
||||||
IndexDetails indexDetails = new IndexDetails(offset, size, type, hash);
|
IndexDetails indexDetails = new IndexDetails(offset, size, type, hash);
|
||||||
editIndex(index, indexDetails);
|
editIndex(index, indexDetails);
|
||||||
return indexDetails;
|
return indexDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit index data
|
||||||
|
* @param index
|
||||||
|
* @param details
|
||||||
|
*/
|
||||||
private void editIndex(long index, IndexDetails details) {
|
private void editIndex(long index, IndexDetails details) {
|
||||||
synchronized (indicesMapsAccessLock) {
|
synchronized (indicesMapsAccessLock) {
|
||||||
loadedIndices.put(index, details);
|
loadedIndices.put(index, details);
|
||||||
|
@ -26,7 +26,7 @@ public class App {
|
|||||||
System.out.println("Time elapsed: " + (time01 - time0));
|
System.out.println("Time elapsed: " + (time01 - time0));
|
||||||
System.out.println("Loading root...");
|
System.out.println("Loading root...");
|
||||||
EntryReference<LightList<Animal>> rootRef = db.getRoot(Animal.class);
|
EntryReference<LightList<Animal>> rootRef = db.getRoot(Animal.class);
|
||||||
LightList<Animal> root = rootRef.value;
|
LightList<Animal> root = rootRef.getValue();
|
||||||
long time1 = System.currentTimeMillis();
|
long time1 = System.currentTimeMillis();
|
||||||
System.out.println("Time elapsed: " + (time1 - time01));
|
System.out.println("Time elapsed: " + (time1 - time01));
|
||||||
System.out.println("Root size: " + root.size());
|
System.out.println("Root size: " + root.size());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user