Merge remote-tracking branch 'origin/master'

This commit is contained in:
Andrea Cavalli 2018-12-05 02:43:49 +01:00
commit aa5cf24add
17 changed files with 462 additions and 126 deletions

View File

@ -35,6 +35,11 @@
<artifactId>kryo</artifactId>
<version>5.0.0-RC1</version>
</dependency>
<dependency>
<groupId>net.openhft</groupId>
<artifactId>zero-allocation-hashing</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
<build>

View File

@ -3,9 +3,10 @@ package org.warp.jcwdb;
public interface DBDataOutput<T> {
int getSize();
int getType();
long calculateHash();
DBWriter<T> getWriter();
static <T> DBDataOutput<T> create(DBWriter<T> writer, int type, int size) {
static <T> DBDataOutput<T> create(DBWriter<T> writer, int type, int size, long hash) {
return new DBDataOutput<T>() {
@Override
@ -17,6 +18,11 @@ public interface DBDataOutput<T> {
public int getType() {
return type;
}
@Override
public long calculateHash() {
return hash;
}
@Override
public DBWriter<T> getWriter() {

View File

@ -5,7 +5,10 @@ import java.io.ByteArrayOutputStream;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import net.openhft.hashing.LongHashFunction;
public class DBGenericObjectParser extends DBTypeParserImpl<Object> {
private static final LongHashFunction hashFunction = net.openhft.hashing.LongHashFunction.xx();
private static final Kryo kryo = new Kryo();
static {
kryo.setRegistrationRequired(false);
@ -25,9 +28,22 @@ public class DBGenericObjectParser extends DBTypeParserImpl<Object> {
kryo.writeClassAndObject(tmpO, value);
tmpO.flush();
final byte[] bytes = baos.toByteArray();
final long hash = hashFunction.hashBytes(bytes);
tmpO.close();
return DBDataOutput.create((o) -> {
o.write(bytes);
}, DBStandardTypes.GENERIC_OBJECT, bytes.length);
}, DBStandardTypes.GENERIC_OBJECT, bytes.length, hash);
}
@Override
public long calculateHash(Object value) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output tmpO = new Output(baos);
kryo.writeClassAndObject(tmpO, value);
tmpO.flush();
final byte[] bytes = baos.toByteArray();
final long hash = hashFunction.hashBytes(bytes);
tmpO.close();
return hash;
}
}

View File

@ -2,6 +2,8 @@ package org.warp.jcwdb;
import java.util.ArrayList;
import it.unimi.dsi.fastutil.longs.LongArrayList;
public class DBLightListParser extends DBTypeParserImpl<LightList> {
private final JCWDatabase db;
@ -11,23 +13,33 @@ public class DBLightListParser extends DBTypeParserImpl<LightList> {
public DBReader<LightList> getReader() {
return (i, size) -> {
ArrayList<Long> internalList = new ArrayList<>();
long max = size / Long.BYTES;
LongArrayList internalList = new LongArrayList();
LongArrayList hashes = new LongArrayList();
long max = size / (Long.BYTES * 2);
for (int item = 0; item < max; item++){
long itm = i.readLong();
long hash = i.readLong();
internalList.add(itm);
hashes.add(hash);
}
return new LightList(db, internalList);
return new LightList(db, internalList, hashes);
};
}
public DBDataOutput<LightList> getWriter(final LightList value) {
final int elementsCount = value.internalList.size();
final int elementsCount = value.size();
return DBDataOutput.create((o) -> {
ArrayList<Long> list = value.internalList;
for (Long item : list) {
o.writeLong(item);
LongArrayList list = value.internalList;
LongArrayList hashes = value.hashes;
for (int i = 0; i < elementsCount; i++) {
o.writeLong(list.getLong(i));
o.writeLong(hashes.getLong(i));
}
}, DBStandardTypes.LIGHT_LIST, elementsCount * Long.BYTES);
}, DBStandardTypes.LIGHT_LIST, elementsCount * Long.BYTES * 2, calculateHash(value));
}
@Override
public long calculateHash(LightList value) {
return value.hashCodeLong();
}
}

View File

@ -1,12 +1,13 @@
package org.warp.jcwdb;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import com.esotericsoftware.kryo.io.Output;
import net.openhft.hashing.LongHashFunction;
public class DBStringParser extends DBTypeParserImpl<String> {
private static final LongHashFunction hashFunction = net.openhft.hashing.LongHashFunction.xx();
private static final DBReader<String> defaultReader = (i, size) -> {
return i.readString();
return new String(i.readBytes(size), StandardCharsets.UTF_16LE);
};
public DBReader<String> getReader() {
@ -14,14 +15,13 @@ public class DBStringParser extends DBTypeParserImpl<String> {
}
public DBDataOutput<String> getWriter(final String value) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output tmpO = new Output(baos);
tmpO.writeString(value);
tmpO.flush();
final byte[] bytes = baos.toByteArray();
tmpO.close();
return DBDataOutput.create((o) -> {
o.write(bytes);
}, DBStandardTypes.STRING, bytes.length);
o.write(value.getBytes(StandardCharsets.UTF_16LE));
}, DBStandardTypes.STRING, value.length() * 2, calculateHash(value));
}
@Override
public long calculateHash(String value) {
return hashFunction.hashBytes(value.getBytes(StandardCharsets.UTF_16LE));
}
}

View File

@ -3,4 +3,5 @@ package org.warp.jcwdb;
public interface DBTypeParser<T> extends Castable {
DBReader<T> getReader();
DBDataOutput<T> getWriter(final T value);
long calculateHash(final T value);
}

View File

@ -35,7 +35,15 @@ public class EntryReference<T> implements Castable {
public long getIndex() {
return entryIndex;
}
public long calculateHash() {
return parser.calculateHash(value);
}
/**
* Note that this method won't be called when closing without saving
* @throws IOException
*/
public void save() throws IOException {
if (!closed) {
db.write(entryIndex, parser.getWriter(value));
@ -61,4 +69,19 @@ public class EntryReference<T> implements Castable {
closed = true;
}
}
public void closeWithoutSaving() {
if (closed) {
return;
}
synchronized (closeLock) {
if (closed) {
return;
}
db.removeEntryReference(entryIndex);
closed = true;
}
}
}

View File

@ -76,11 +76,11 @@ public class FileIndexManager implements IndexManager {
public <T> void set(long index, DBDataOutput<T> data) throws IOException {
checkClosed();
final IndexDetails indexDetails = getIndexMetadataUnsafe(index);
if (indexDetails == null || indexDetails.getSize() < data.getSize()) {
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.
allocateAndWrite(index, data);
} else {
if (indexDetails.getSize() > data.getSize()) {
editIndex(index, indexDetails.getOffset(), data.getSize(), indexDetails.getType());
editIndex(index, indexDetails.getOffset(), data.getSize(), indexDetails.getType(), data.calculateHash());
fileAllocator.markFree(indexDetails.getOffset()+data.getSize(), data.getSize());
}
writeExact(indexDetails, data);
@ -93,7 +93,8 @@ public class FileIndexManager implements IndexManager {
final int size = data.getSize();
final long offset = fileAllocator.allocate(size);
final int type = data.getType();
final IndexDetails indexDetails = new IndexDetails(offset, size, type);
final long hash = data.calculateHash();
final IndexDetails indexDetails = new IndexDetails(offset, size, type, hash);
final long index = createIndexMetadata(indexDetails);
writeExact(indexDetails, data);
return index;
@ -121,8 +122,9 @@ public class FileIndexManager implements IndexManager {
private void allocateAndWrite(final long index, DBDataOutput<?> w) throws IOException {
final int size = w.getSize();
final int type = w.getType();
final long hash = w.calculateHash();
final long offset = fileAllocator.allocate(size);
IndexDetails details = editIndex(index, offset, size, type);
IndexDetails details = editIndex(index, offset, size, type, hash);
writeExact(details, w);
}
@ -179,8 +181,8 @@ public class FileIndexManager implements IndexManager {
}
}
private IndexDetails editIndex(long index, long offset, int size, int type) {
IndexDetails indexDetails = new IndexDetails(offset, size, type);
private IndexDetails editIndex(long index, long offset, int size, int type, long hash) {
IndexDetails indexDetails = new IndexDetails(offset, size, type, hash);
editIndex(index, indexDetails);
return indexDetails;
}
@ -224,9 +226,13 @@ public class FileIndexManager implements IndexManager {
// If it's not deleted continue
if ((metadataByteBuffer.getInt() & IndexDetails.MASK_DELETED) == 0) {
final long offset = metadataByteBuffer.getLong();
// final long sizeAndType = metadataByteBuffer.getLong();
// final int size = (int)(sizeAndType >> 32);
// final int type = (int)sizeAndType;
final int size = metadataByteBuffer.getInt();
final int type = metadataByteBuffer.getInt();
final IndexDetails indexDetails = new IndexDetails(offset, size, type);
final long hash = metadataByteBuffer.getLong();
final IndexDetails indexDetails = new IndexDetails(offset, size, type, hash);
editIndex(index, indexDetails);
return indexDetails;
}
@ -282,11 +288,17 @@ public class FileIndexManager implements IndexManager {
private void writeIndexDetails(SeekableByteChannel position, IndexDetails indexDetails) throws IOException {
synchronized (metadataByteBufferLock) {
final int size = indexDetails.getSize();
final int type = indexDetails.getType();
final long offset = indexDetails.getOffset();
final long hash = indexDetails.getHash();
metadataByteBuffer.rewind();
metadataByteBuffer.putInt(0);
metadataByteBuffer.putLong(indexDetails.getOffset());
metadataByteBuffer.putInt(indexDetails.getSize());
metadataByteBuffer.putInt(indexDetails.getType());
metadataByteBuffer.putLong(offset);
metadataByteBuffer.putInt(size);
metadataByteBuffer.putInt(type);
//metadataByteBuffer.putLong((long)size << 32 | type & 0xFFFFFFFFL);
metadataByteBuffer.putLong(hash);
metadataByteBuffer.rewind();
position.write(metadataByteBuffer);
}

View File

@ -10,22 +10,26 @@ public class IndexDetails {
public static final int OFFSET_BYTES = Long.BYTES;
public static final int DATA_SIZE_BYTES = Integer.BYTES;
public static final int TYPE_BYTES = Integer.BYTES;
public static final int TOTAL_BYTES = BITMASK_SIZE + OFFSET_BYTES + DATA_SIZE_BYTES + TYPE_BYTES;
public static final int HASH_BYTES = Long.BYTES;
public static final int TOTAL_BYTES = BITMASK_SIZE + OFFSET_BYTES + DATA_SIZE_BYTES + TYPE_BYTES + HASH_BYTES;
public static final int MASK_DELETED = 0b00000001;
private final long offset;
private final int size;
private final int type;
private final long hash;
public IndexDetails(long offset, int size, int type) {
public IndexDetails(long offset, int size, int type, long hash) {
this.offset = offset;
this.size = size;
this.type = type;
this.hash = hash;
}
public IndexDetails(IndexDetails indexDetails) {
this.offset = indexDetails.offset;
this.size = indexDetails.size;
this.type = indexDetails.type;
this.hash = indexDetails.hash;
}
public long getOffset() {
@ -39,29 +43,46 @@ public class IndexDetails {
public int getType() {
return type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IndexDetails that = (IndexDetails) o;
return offset == that.offset &&
size == that.size &&
type == that.type;
public long getHash() {
return hash;
}
@Override
public int hashCode() {
return Objects.hash(offset);
final int prime = 31;
int result = 1;
result = prime * result + (int) (hash ^ (hash >>> 32));
result = prime * result + (int) (offset ^ (offset >>> 32));
result = prime * result + size;
result = prime * result + type;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IndexDetails other = (IndexDetails) obj;
if (hash != other.hash)
return false;
if (offset != other.offset)
return false;
if (size != other.size)
return false;
if (type != other.type)
return false;
return true;
}
@Override
public String toString() {
return "IndexDetails{" +
"offset=" + offset +
", size=" + size +
", type=" + type +
'}';
return "IndexDetails [offset=" + offset + ", size=" + size + ", type=" + type + ", hash=" + hash + "]";
}
}

View File

@ -9,6 +9,7 @@ import java.util.Iterator;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
public class JCWDatabase implements AutoCloseable, Cleanable {
@ -42,11 +43,15 @@ public class JCWDatabase implements AutoCloseable, Cleanable {
}
public <T> EntryReference<LightList<T>> getRoot() throws IOException {
return getRoot(Object.class).cast();
}
public <T> EntryReference<LightList<T>> getRoot(Class<T> clazz) throws IOException {
checkClosed();
if (exists(0)) {
return get(0);
} else {
LightList<T> newRoot = new LightList<T>(this, new ArrayList<>());
LightList<T> newRoot = new LightList<T>(this, new LongArrayList(), new LongArrayList());
return set(0, newRoot);
}
}
@ -209,4 +214,10 @@ public class JCWDatabase implements AutoCloseable, Cleanable {
indices.set(index, writer);
}
}
@SuppressWarnings("unchecked")
protected <T> long calculateHash(T o) {
return ((DBTypeParser<T>) typesManager.get(o.getClass())).calculateHash(o);
}
}

View File

@ -2,14 +2,20 @@ package org.warp.jcwdb;
import java.io.IOException;
import java.util.*;
import java.util.function.Predicate;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class LightList<T> implements List<T> {
public final ArrayList<Long> internalList;
public final LongArrayList internalList;
public final LongArrayList hashes;
private final transient JCWDatabase db;
public LightList(JCWDatabase db, ArrayList<Long> internalList) {
public LightList(JCWDatabase db, LongArrayList internalList, LongArrayList hashes) {
this.internalList = internalList;
this.hashes = hashes;
this.db = db;
}
@ -27,8 +33,9 @@ public class LightList<T> implements List<T> {
public boolean contains(Object o) {
if (o != null) {
for (Long element : internalList) {
EntryReference<T> ref = null;
try {
EntryReference<T> ref = db.get(element);
ref = db.get(element);
if (o.equals(ref.value)) {
return true;
}
@ -40,6 +47,7 @@ public class LightList<T> implements List<T> {
return false;
}
@SuppressWarnings("unchecked")
@Override
public Iterator<T> iterator() {
final ArrayList<T> elements = new ArrayList<>();
@ -53,12 +61,13 @@ public class LightList<T> implements List<T> {
return elements.iterator();
}
@SuppressWarnings("unchecked")
@Override
public T[] toArray() {
final T[] elements = (T[]) new Objects[internalList.size()];
for (int i = 0; i < elements.length; i++) {
try {
elements[i] = (T) db.get(internalList.get(i)).value;
elements[i] = (T) db.get(internalList.getLong(i)).value;
} catch (IOException e) {
e.printStackTrace();
}
@ -66,12 +75,13 @@ public class LightList<T> implements List<T> {
return elements;
}
@SuppressWarnings("unchecked")
@Override
public <T1> T1[] toArray(T1[] a) {
final T1[] elements = (T1[]) new Objects[internalList.size()];
for (int i = 0; i < elements.length; i++) {
try {
elements[i] = (T1) db.get(internalList.get(i)).value;
elements[i] = (T1) db.get(internalList.getLong(i)).value;
} catch (IOException e) {
e.printStackTrace();
}
@ -81,117 +91,284 @@ public class LightList<T> implements List<T> {
@Override
public boolean add(T o) {
EntryReference<T> ref;
try {
ref = db.add(o);
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
EntryReference<T> ref = addEntry(o);
return ref != null;
}
public EntryReference<T> addEntry(T o) {
EntryReference<T> ref = addToDatabase(o);
if (internalList.add(ref.getIndex())) {
hashes.add(ref.getParser().calculateHash(ref.value));
} else {
return null;
}
return internalList.add(ref.getIndex());
return ref;
}
@Override
public boolean remove(Object o) {
EntryReference<T> ref;
try {
ref = db.add((T) o);
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
int removeIndex = indexOf(o);
if (removeIndex >= 0) {
internalList.removeLong(removeIndex);
hashes.removeLong(removeIndex);
return true;
}
return internalList.remove(ref.getIndex());
return false;
}
public boolean remove(EntryReference<T> ref) {
int removeIndex = indexOfEntry(ref);
if (removeIndex >= 0) {
internalList.removeLong(removeIndex);
hashes.removeLong(removeIndex);
return true;
}
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO: implement
return false;
for (Object o : c) {
int objIndex = indexOf(o);
if (objIndex < 0) {
return false;
}
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean addAll(Collection<? extends T> c) {
// TODO: implement
return false;
boolean result = false;
for (Object o : c) {
result |= add((T) o);
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean addAll(int index, Collection<? extends T> c) {
// TODO: implement
return false;
boolean result = false;
int delta = 0;
for (Object o : c) {
add(index + delta, (T) o);
result = true;
delta++;
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean removeAll(Collection<?> c) {
// TODO: implement
return false;
boolean result = false;
for (Object o : c) {
result |= remove((T) o);
}
return result;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO: implement
return false;
boolean result = false;
LongArrayList collectionHashes = new LongArrayList();
ObjectArrayList<Object> collection = new ObjectArrayList<>();
collection.addAll(c);
for (Object o : c) {
collectionHashes.add(db.calculateHash(o));
}
for (int i = 0; i < internalList.size(); i++) {
long hash = internalList.getLong(i);
int positionInCollection = collectionHashes.indexOf(hash);
if (positionInCollection == -1) {
remove(collection.get(positionInCollection));
result = true;
}
}
return result;
}
@Override
public void clear() {
// TODO: implement
internalList.clear();
hashes.clear();
}
@SuppressWarnings("unchecked")
@Override
public T get(int index) {
try {
return (T) db.get(internalList.get(index)).value;
return (T) db.get(internalList.getLong(index)).value;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public T set(int index, T element) {
// TODO: implement
return null;
EntryReference<T> ref = addToDatabase(element);
long oldIndex = internalList.set(index, ref.getIndex());
hashes.set(index, ref.calculateHash());
try {
ref.close();
return ((EntryReference<T>) (db.get(oldIndex))).value;
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
}
@Override
public void add(int index, T element) {
// TODO: implement
EntryReference<T> ref = addToDatabase(element);
internalList.add(index, ref.getIndex());
hashes.add(index, ref.getParser().calculateHash(ref.value));
}
@SuppressWarnings("unchecked")
@Override
public T remove(int index) {
// TODO: implement
return null;
long oldIndex = internalList.removeLong(index);
hashes.removeLong(index);
try {
return ((EntryReference<T>) (db.get(oldIndex))).value;
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
}
@Override
public int indexOf(Object o) {
// TODO: implement
return 0;
EntryReference<?> ref = addToDatabase(o);
long objToRemoveHash = ref.calculateHash();
for (int i = 0; i < hashes.size(); i++) {
long hash = hashes.getLong(i);
if (objToRemoveHash == hash) {
try {
if (ref.equals(db.get(internalList.getLong(i)))) {
return i;
}
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
}
}
return -1;
}
public int indexOfEntry(EntryReference<T> ref) {
long objToRemoveHash = ref.calculateHash();
for (int i = 0; i < hashes.size(); i++) {
long hash = hashes.getLong(i);
if (objToRemoveHash == hash) {
try {
if (ref.equals(db.get(internalList.getLong(i)))) {
return i;
}
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
}
}
return -1;
}
@Override
public int lastIndexOf(Object o) {
// TODO: implement
return 0;
EntryReference<?> ref = addToDatabase(o);
long objToRemoveHash = ref.calculateHash();
int lastValue = -1;
for (int i = 0; i < hashes.size(); i++) {
long hash = hashes.getLong(i);
if (objToRemoveHash == hash) {
try {
if (ref.equals(db.get(internalList.getLong(i)))) {
lastValue = i;
}
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
}
}
return lastValue;
}
@Deprecated
@Override
public ListIterator<T> listIterator() {
// TODO: implement
return null;
throw new RuntimeException("Not implemented!");
}
@Deprecated
@Override
public ListIterator<T> listIterator(int index) {
// TODO: implement
return null;
throw new RuntimeException("Not implemented!");
}
@Deprecated
@Override
public List<T> subList(int fromIndex, int toIndex) {
// TODO: implement
return null;
throw new RuntimeException("Not implemented!");
}
private <U> EntryReference<U> addToDatabase(U obj) {
EntryReference<U> ref;
try {
ref = db.add(obj);
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
return ref;
}
@Deprecated
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((hashes == null) ? 0 : hashes.hashCode());
result = prime * result + ((internalList == null) ? 0 : internalList.hashCode());
return result;
}
public long hashCodeLong() {
final int prime = 31;
int result1 = prime + ((hashes == null) ? 0 : hashes.hashCode());
int result2 = prime + ((internalList == null) ? 0 : internalList.hashCode());
long result = (((long) result1) << 32) | (result2 & 0xffffffffL);
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean removeIf(Predicate<? super T> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
for (int i = 0; i < internalList.size(); ) {
T obj;
try {
obj = ((EntryReference<T>) (db.get(internalList.getLong(i)).cast())).value;
} catch (IOException e) {
throw (NullPointerException) new NullPointerException().initCause(e);
}
if (filter.test(obj)) {
internalList.removeLong(i);
hashes.removeLong(i);
removed = true;
} else {
i++;
}
}
return removed;
}
}

View File

@ -0,0 +1,9 @@
package org.warp.jcwdb.exampleimpl;
public abstract class Animal {
protected int legsCount;
public static boolean hasFourLegs(Animal a) {
return a.legsCount == 4;
}
}

View File

@ -4,8 +4,12 @@ import org.warp.jcwdb.EntryReference;
import org.warp.jcwdb.JCWDatabase;
import org.warp.jcwdb.LightList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectList;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.function.Predicate;
public class App {
public static void main(String[] args) throws Exception {
@ -19,25 +23,47 @@ public class App {
long time01 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time01 - time0));
System.out.println("Loading root...");
LightList<String> root = ((EntryReference<LightList<String>>) db.getRoot().cast()).value;
EntryReference<LightList<Animal>> rootRef = db.getRoot(Animal.class);
LightList<Animal> root = rootRef.value;
long time1 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time1 - time01));
System.out.println("Root size: " + root.size());
System.out.println("Root:");
// for (int i = 0; i < root.size(); i++) {
// System.out.println(" - " + root.get(i));
// }
long prectime = System.currentTimeMillis();
for (int i = 0; i < 2000000/*2000000*/; i++) {
root.add("Test " + i);
for (int i = 0; i < 2000000000/* 2000000 */; i++) {
Animal animal = new StrangeAnimal(i % 40);
root.add(animal);
if (i > 0 && i % 200000 == 0) {
long precprectime = prectime;
prectime = System.currentTimeMillis();
System.out.println("Element "+i + " ("+(prectime-precprectime)+"ms)");
System.out.println("Element " + i + " (" + (prectime - precprectime) + "ms)");
}
}
long time2 = System.currentTimeMillis();
System.out.println("Root size: "+root.size());
System.out.println("Root size: " + root.size());
System.out.println("Time elapsed: " + (time2 - time1));
System.out.println("Used memory: "
+ ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB");
long time2_0 = System.currentTimeMillis();
System.out.println("Filtering strings...");
//root.removeIf(Animal::hasFourLegs);
long time2_1 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time2_1 - time2_0));
ObjectList<Animal> results = new ObjectArrayList<>();
root.forEach((value) -> {
if (Animal.hasFourLegs(value)) {
results.add(value);
}
//System.out.println("val:" + value);
});
long time2_2 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time2_2 - time2_1));
System.out.println("Used memory: "
+ ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB");
System.out.println("Used memory: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB");
System.out.println("Cleaning database (to reduce the amount of used memory and detect memory leaks)...");
long removedItems = db.clean();
@ -46,8 +72,13 @@ public class App {
System.out.println("Used memory: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB");
System.out.println("Time elapsed: " + (time3 - time2));
System.out.println("Saving database...");
System.out.println("Root size: " + root.size());
db.close();
long time4 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time4 - time3));
long time3 = System.currentTimeMillis();
System.out.println("Time elapsed: " + (time3 - time2_2));
}
public static <T> Predicate<T> not(Predicate<T> t) {
return t.negate();
}
}

View File

@ -0,0 +1,12 @@
package org.warp.jcwdb.exampleimpl;
public class Cat extends Animal {
public Cat() {
this.legsCount = 12;
}
@Override
public String toString() {
return "Cat [legsCount=" + legsCount + "]";
}
}

View File

@ -0,0 +1,12 @@
package org.warp.jcwdb.exampleimpl;
public class Dog extends Animal {
public Dog() {
this.legsCount = 4;
}
@Override
public String toString() {
return "Dog [legsCount=" + legsCount + "]";
}
}

View File

@ -0,0 +1,15 @@
package org.warp.jcwdb.exampleimpl;
public class StrangeAnimal extends Animal {
public StrangeAnimal() {
super();
}
public StrangeAnimal(int legs) {
super();
this.legsCount = legs;
}
@Override
public String toString() {
return "StrangeAnimal [legsCount=" + legsCount + "]";
}
}

View File

@ -115,31 +115,4 @@ public class AppTest
Files.delete(path);
Files.delete(idx);
}
/**
*
* @throws IOException
*/
@Test
public void shouldActAsAnArrayListWithDifferentObjects() throws IOException
{
Path path = Files.createTempFile("", ".db"), idx = Files.createTempFile("", ".idx");
JCWDatabase db = new JCWDatabase(path, idx);
EntryReference<LightList<String>> ref = db.getRoot();
LightList<String> list1 = ref.value;
ArrayList<String> list2 = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
String s = String.valueOf(i);
list1.add(s);
list2.add(s);
}
assertTrue(list1.size() == list2.size());
for (int i = 0; i < 10; i++) {
assertTrue(list1.get(i) == list2.get(i));
assertTrue(list2.get(i) == list1.get(i));
}
db.close();
Files.delete(path);
Files.delete(idx);
}
}