102 lines
4.0 KiB
Java
102 lines
4.0 KiB
Java
package org.warp.jcwdb.exampleimpl;
|
|
|
|
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.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.function.Predicate;
|
|
|
|
public class App {
|
|
static long time3;
|
|
public static void main(String[] args) throws IOException {
|
|
if (args.length > 2 && Boolean.parseBoolean(args[2])) {
|
|
Files.delete(Paths.get(args[0]));
|
|
Files.delete(Paths.get(args[1]));
|
|
}
|
|
System.out.println("Loading database...");
|
|
long time0 = System.currentTimeMillis();
|
|
JCWDatabase db = new JCWDatabase(Paths.get(args[0]), Paths.get(args[1]));
|
|
db.registerClass(StrangeAnimal.class, 0);
|
|
try {
|
|
long time01 = System.currentTimeMillis();
|
|
System.out.println("Time elapsed: " + (time01 - time0));
|
|
System.out.println("Loading root...");
|
|
EntryReference<LightList<Animal>> rootRef = db.getRoot(Animal.class);
|
|
rootRef.editValue((root, saver) -> {
|
|
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++) {
|
|
Animal animal = new StrangeAnimal(i % 40);
|
|
root.addEntry(animal);
|
|
if (i > 0 && i % 200000 == 0) {
|
|
long precprectime = prectime;
|
|
prectime = System.currentTimeMillis();
|
|
System.out.println("Element " + i + " (" + (prectime - precprectime) + "ms)" + " Total Time: " + (prectime - time1));
|
|
}
|
|
}
|
|
long time2 = System.currentTimeMillis();
|
|
saver.save();
|
|
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...");
|
|
long oldSize = root.size();
|
|
root.removeIf(Animal::hasFourLegs);
|
|
long time2_1 = System.currentTimeMillis();
|
|
System.out.println("RemoveIf(x) removed items: " + (oldSize - root.size()));
|
|
System.out.println("Time elapsed: " + (time2_1 - time2_0));
|
|
ObjectList<Animal> results = new ObjectArrayList<>();
|
|
|
|
System.out.println("Retrieving items...");
|
|
root.forEachReference((valueReference) -> {
|
|
Animal value = valueReference.getValueReadOnlyUnsafe();
|
|
if (Animal.hasFourLegs(value)) {
|
|
results.add(value);
|
|
}
|
|
//System.out.println("val:" + value);
|
|
});
|
|
long time2_2 = System.currentTimeMillis();
|
|
System.out.println("Matches: " + results.size());
|
|
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)...");
|
|
db.clean();
|
|
time3 = System.currentTimeMillis();
|
|
System.out.println("Used memory: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024) + "MB");
|
|
System.out.println("Time elapsed: " + (time3 - time2_2));
|
|
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));
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
} finally {
|
|
if (db.isOpen()) {
|
|
db.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static <T> Predicate<T> not(Predicate<T> t) {
|
|
return t.negate();
|
|
}
|
|
}
|