Can figure why the Page is never allocated

8 views
Skip to first unread message

Solido

unread,
Aug 22, 2010, 11:16:46 AM8/22/10
to hawtdb
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());
}

}




}
}

Hiram Chirino

unread,
Aug 23, 2010, 7:27:43 AM8/23/10
to haw...@googlegroups.com
It would work if you used TxPageFileFactory instead.

Since the PageFileFactory is not transactional, it does not even
attempt to ensure consistency and does not track persistent page file
allocations. On restart it thinks the page file empty. You would
need to use the TxPageFileFactory or store the allocated page file
list externally if you really wanted to persist across restarts.

--
Regards,
Hiram

Blog: http://hiramchirino.com

Open Source SOA
http://fusesource.com/

Solido

unread,
Aug 23, 2010, 9:19:31 AM8/23/10
to hawtdb
I've restarted the project to follow this principe and it works well.
I must have read previous response you done to better understand it.
Thank you !
Reply all
Reply to author
Forward
0 new messages