-
Notifications
You must be signed in to change notification settings - Fork 8
Java Object Query
Fast ACID NoSQL Application Database edited this page Sep 18, 2019
·
3 revisions
package example;
import com.db4o.*;
import com.db4o.config.ConfigScope;
import com.db4o.config.annotations.Indexed;
import com.db4o.cs.*;
import com.db4o.query.Predicate;
import com.db4o.ta.TransparentActivationSupport;
import com.db4o.ta.TransparentPersistenceSupport;
import java.io.File;
public class db4odemo {
public static class Node {
@Indexed
public String name;
public Node Left;
public Node Right;
public int Size = 99;
}
public static void main(String[] args) {
String dbname = "/tmp/node.j.db";
new File(dbname).delete();
long internalId;
String objectId;
var cfg = Db4oClientServer.newServerConfiguration();
cfg.common().objectClass(Node.class).cascadeOnActivate(true);
cfg.common().objectClass(Node.class).cascadeOnUpdate(true);
cfg.common().objectClass(Node.class).cascadeOnDelete(true);
cfg.common().objectClass(Node.class).callConstructor(true);
cfg.common().add(new TransparentActivationSupport());
cfg.common().add(new TransparentPersistenceSupport());
cfg.file().generateUUIDs(ConfigScope.GLOBALLY);
try (var server = Db4oClientServer.openServer(cfg, dbname, 0)) {
try (var oc = server.openClient()) {
var root = new Node();
root.name = "Root";
root.Left = new Node();
root.Left.name = "Left";
root.Right = new Node();
root.Right.name = "Right";
root.Right.Right = new Node();
root.Right.Right.name = "Right.Right";
oc.store(root);
oc.commit();
}
try (var oc = server.openClient()) {
var metaInfo = oc.ext().storedClass(Node.class);
// list a fields and check if they have a index
for (var field : metaInfo.getStoredFields()) {
if (field.hasIndex()) {
System.out.println("The field '" + field.getName() + "' is indexed");
} else {
System.out.println("The field '" + field.getName() + "' isn't indexed");
}
}
}
try (var oc = server.openClient()) {
var qo = new Node();
qo.name = "Root";
var ns = oc.queryByExample(qo);
var root = ns.next();
System.out.println(root.Right.Right.name);
internalId = oc.ext().getID(root);
objectId = oc.ext().getObjectInfo(root).getUUID().toString();
}
try (var oc = server.openClient()) {
System.out.println(objectId);
var root = oc.ext().getByUUID(Node.class, objectId);
System.out.println(root.Right.Right.name);
}
try (var oc = server.openClient()) {
System.out.println(internalId);
var root = oc.ext().getByID(Node.class, internalId);
System.out.println(root.Right.Right.name);
}
//Use Native Query, recommended
try (var oc = server.openClient()) {
var ns = oc.query(new Predicate<Node>() {
@Override
public boolean match(Node n) {
return n.name.equals("Root");
}
});
var root = ns.get(0);
System.out.println(root.Right.Right.name);
}
try (var oc = server.openClient()) {
var ns = oc.query(new Predicate<Node>() {
@Override
public boolean match(Node n) {
return n.name.equals("Right.Right");
}
});
var r_r = ns.get(0);
System.out.println(r_r.name);
}
// S.O.D.A. not recommended, dynamic program
try (var oc = server.openClient()) {
var q = oc.query();
q.constrain(Node.class);
q.descend("name").constrain("Root").equal();
var ns = q.execute(Node.class);
for (var n : ns) {
n.Right.Right.name += ".Update";
oc.store(n);
}
oc.commit();
}
//Use Complex Native Query
try (var oc = server.openClient()) {
var ns = oc.query(new Predicate<Node>() {
@Override
public boolean match(Node n) {
return n.name.compareTo("Right.Right") > 0
&& n.name.length() > "Root".length();
}
});
var r_r = ns.get(0);
System.out.println(r_r.name);
}
}
var ecfg = Db4oEmbedded.newConfiguration();
ecfg.common().objectClass(Node.class).cascadeOnActivate(true);
ecfg.common().objectClass(Node.class).cascadeOnUpdate(true);
ecfg.common().objectClass(Node.class).cascadeOnDelete(true);
ecfg.common().objectClass(Node.class).callConstructor(true);
ecfg.common().add(new TransparentActivationSupport());
ecfg.common().add(new TransparentPersistenceSupport());
ecfg.file().generateUUIDs(ConfigScope.GLOBALLY);
try (var oc = Db4oEmbedded.openFile(ecfg, dbname)) {
System.out.println(objectId);
var root = oc.ext().getByUUID(Node.class, objectId);
System.out.println(root.Right.Right.name);
System.out.println(internalId);
root = oc.ext().getByID(Node.class, internalId);
System.out.println(root.Right.Right.name);
}
ecfg = Db4oEmbedded.newConfiguration();
ecfg.common().objectClass(Node.class).cascadeOnActivate(true);
ecfg.common().objectClass(Node.class).cascadeOnUpdate(true);
ecfg.common().objectClass(Node.class).cascadeOnDelete(true);
ecfg.common().objectClass(Node.class).callConstructor(true);
ecfg.common().add(new TransparentActivationSupport());
ecfg.common().add(new TransparentPersistenceSupport());
ecfg.file().generateUUIDs(ConfigScope.GLOBALLY);
try (var oc = Db4oEmbedded.openFile(ecfg, dbname)) {
try (var see = oc.ext().openSession()) {
System.out.println(objectId);
var root = see.ext().getByUUID(Node.class, objectId);
System.out.println(root.Right.Right.name);
}
try (var see = oc.ext().openSession()) {
System.out.println(internalId);
var root = see.ext().getByID(Node.class, internalId);
System.out.println(root.Right.Right.name);
}
}
System.out.println("End.");
}
}
OUTPUT
The field 'v4ouuid' is indexed
The field 'name' is indexed
The field 'Left' isn't indexed
The field 'Right' isn't indexed
The field 'Size' isn't indexed
Right.Right
3s57fz2hukt13c4angdpefkxuiggymz4uor58i18dgkywk7lamqewp0a2mlr4cvsatz810
Right.Right
31
Right.Right
Right.Right
Right.Right
Right.Right.Update
3s57fz2hukt13c4angdpefkxuiggymz4uor58i18dgkywk7lamqewp0a2mlr4cvsatz810
Right.Right.Update
31
Right.Right.Update
3s57fz2hukt13c4angdpefkxuiggymz4uor58i18dgkywk7lamqewp0a2mlr4cvsatz810
Right.Right.Update
31
Right.Right.Update
End.