Hello everyone,
I'm Robbie, i'm looking at hawtdb for future project, it looks
promising and a alternative at redis db but on the java side.
I've understood the basics but my code failed to persist the data in
the file. It looks like a mistake for my part but i can't figure
where !
Here's my code:
import static org.fusesource.hawtdb.api.Predicates.and;
import static
org.fusesource.hawtdb.api.Predicates.gt;
import static
org.fusesource.hawtdb.api.Predicates.lt;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import org.fusesource.hawtbuf.codec.LongCodec;
import org.fusesource.hawtdb.api.BTreeIndexFactory;
import org.fusesource.hawtdb.api.PageFile;
import org.fusesource.hawtdb.api.PageFileFactory;
import org.fusesource.hawtdb.api.Predicate;
import org.fusesource.hawtdb.api.SortedIndex;
public class Starter {
/**
* @param args
*/
public static void main(String[] args) {
new Starter();
}
public Starter() {
PageFileFactory plasmaFileFactory = new PageFileFactory();
plasmaFileFactory.setFile(new File("C:/Users/admin/Documents/DEV/
plasma.dat"));
plasmaFileFactory.open();
PageFile plasmaFile = plasmaFileFactory.getPageFile();
BTreeIndexFactory<Long, Plasma> indexFactory = new
BTreeIndexFactory<Long, Plasma>();
indexFactory.setDeferredEncoding(true);
indexFactory.setKeyCodec(LongCodec.INSTANCE);
SortedIndex<Long, Plasma> plasmatron;
if (!plasmaFile.allocator().isAllocated(0)) {
plasmatron = indexFactory.create(plasmaFile);
for (long i = 0; i < 2500; i++) {
// contains long as key as several
BigDecimal for testing space allocation of the DB, we have 2.500.000
samples
Plasma p = new Plasma(i);
plasmatron.put(i, p);
if (i % 1000 == 0) {
System.out.println("->" + i);
}
}
// I was hoping this will correct the stuff :(
plasmaFile.flush();
plasmaFileFactory.close();
} else {
plasmatron = indexFactory.open(plasmaFile);
Predicate<Long> p = and(gt(100l), lt(200l));
Iterator<Map.Entry<Long, Plasma>> i = plasmatron.iterator(p);
while (i.hasNext()) {
Map.Entry<Long, Plasma> entry = i.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
}