rendered paste bodypackage db4o;
import java.io.Serializable;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.annotations.Indexed;
import com.db4o.query.Query;
public class PersistenceTest {
public static void main(String[] args) throws Exception {
// testbdb();
// testLucene();
testdb4o();
}
// private static void testbdb() throws DatabaseException, UnsupportedEncodingException {
// long l1 = System.currentTimeMillis();
//
// EnvironmentConfig envConfig = new EnvironmentConfig();
// envConfig.setAllowCreate(true);
//
// Environment env = new Environment(new File("bdb.database"), envConfig);
//
// DatabaseConfig dbConfig = new DatabaseConfig();
// dbConfig.setAllowCreate(true);
//
// Database db = env.openDatabase(null, "bdb.database", dbConfig);
//
// ClassCatalog catalog = new StoredClassCatalog(db);
// EntryBinding binding = new SerialBinding(catalog, Simple.class);
//
// for (int i = 0; i < 1000; i++) {
// Simple simple = new Simple("group", "foo " + i, i);
//
// DatabaseEntry key = new DatabaseEntry(Integer.toString(i).getBytes("UTF-8"));
// DatabaseEntry data = new DatabaseEntry();
//
// binding.objectToEntry(simple, data);
//
// db.put(null, key, data);
// }
//
// long l2 = System.currentTimeMillis();
// System.err.println("bdb: " + (l2-l1)/1000f);
//
// // need to use "secondary database" for retrieving by "group" field
// for (int i = 0; i < 1000; i++) {
// DatabaseEntry key = new DatabaseEntry(Integer.toString(i).getBytes("UTF-8"));
// DatabaseEntry data = new DatabaseEntry();
//
// if(db.get(null, key, data, LockMode.DEFAULT)!=OperationStatus.SUCCESS) {
// System.err.println("Can't find " + i);
// }
// }
// long l3 = System.currentTimeMillis();
// System.err.println("bdb: " + (l3-l2)/1000f);
//
//
// db.close();
// }
// private static void testLucene() throws IOException {
// IndexWriter w = new IndexWriter("lucene.database", new SimpleAnalyzer(), true);
//
// long l1 = System.currentTimeMillis();
// for (int i = 0; i < 1000; i++) {
// Document doc = new Document();
// doc.add(new Field("group", "group", Field.Store.YES, Field.Index.UN_TOKENIZED));
// doc.add(new Field("foo", "foo " + i, Field.Store.YES, Field.Index.UN_TOKENIZED));
// doc.add(new Field("n", Integer.toString(i), Field.Store.YES, Field.Index.UN_TOKENIZED));
//
// w.updateDocument(new Term("n", Integer.toString(i)), doc);
// }
//
// w.optimize();
// w.close();
//
// long l2 = System.currentTimeMillis();
// System.err.println("lucene: " + (l2-l1)/1000f);
//
// IndexReader r = IndexReader.open("lucene.database");
// IndexSearcher s = new IndexSearcher(r);
// PhraseQuery q = new PhraseQuery();
// q.add(new Term("group", "group"));
// Hits hits = s.search(q);
//
// if(hits.length()!=1000) {
// System.err.println("lucene: Invalid search result");
// }
//
// long l3 = System.currentTimeMillis();
// System.err.println("lucene: " + (l3-l2)/1000f);
// }
private static void testdb4o() {
Db4o.configure().flushFileBuffers(false);
ObjectContainer db = Db4o.openFile("db4o.database");
long l1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
final Simple s = new Simple("group", "foo " + i, i);
// ObjectSet<Simple> set = db.query(new Predicate<Simple>() {
// public boolean match(Simple s1) {
// return s1.foo.equals(s.foo) && s1.n==s.n;
// }});
Query query = db.query();
query.constrain(Simple.class);
query.descend("n").constrain(s.n);
ObjectSet set = query.execute();
if(set.hasNext()) {
db.set(set.next());
} else {
db.set(s);
}
}
db.commit();
long l2 = System.currentTimeMillis();
System.err.println("db4o: " + (l2-l1)/1000f);
Query query = db.query();
query.constrain(Simple.class);
query.descend("group").constrain("group");//$NON-NLS-1$
ObjectSet set = query.execute();
// ObjectSet<Simple> set = db.query(new Predicate<Simple>() {
// public boolean match(Simple s) {
// return s.group.equals("group");
// }
// });
if(set.size()!=1000) {
System.err.println("db4o: Invalid search result " + set.size());
}
long l3 = System.currentTimeMillis();
System.err.println("db4o: " + (l3-l2)/1000f);
db.close();
}
private static final class Simple implements Serializable {
@Indexed
String group;
String foo;
@Indexed
int n;
public Simple(String group, String foo, int n) {
this.group = group;
this.foo = foo;
this.n = n;
}
}
}