db4o module

db4o module provides a high level function on top of the db4o API.

open_db4o ( odb_file name )

open_db4o() opens a db4o data file and returns a handle to manupilate it.

e.g.
db = open_db4o("d:/tmp/db4o.odb")
db . set ( object )

Newly stores objects or updates stored objects.

Example 1

Suppose you have Pilot class.
Pilot = beanclass("Pilot", [Serializable], {"name"=>String, "points"=>int}, ["name"])
e.g.
p = Pilot("foo")
db.set(p)
db.commit()

Example 2

columns = ["address", "zip_code"]
g = classGenerator(".")
zip = g.beanclass("zip", null, {"address"=>String, "zip_code"=>String}, columns)

db = open_db4o("zip_db")
db.bufferSize = 1000
for (i:readCSV(csv_file, columns)){
   db.set(zip(i.address, i.zip_code))
}
db.commit()
db.close()
db . delete ( object )

Deletes a stored object permanently.

e.g.
for (i: db.query(Pilot())) db.delete(i)
db . commit ( )

Commits the running transaction.

e.g.
db.commit()
db . rollback ( )

Rolls back the running transaction.

e.g.
try {
  ...
  db.commit()
} catch (Exception e){  
  db.rollback()
}
db . close ( )

Closes the ObjectContainer.

e.g.
db.close()
db . query ( )
db . query ( object )
db . query ( class, predicate_function(obj) )

If no argument is specified, it creates a Query object, from which a query is built in S.O.D.A style.

e.g.
oc = Db4o.configure().objectClass(zip)
oc.maximumActivationDepth(0)
oc.objectField("zip_code").indexed(true)
db = open_db4o("zip_db")
q = db.query()
q.constrain(zip)
q.descend("zip_code").constrain("1000000")
for (record : query.execute()){
   println(record)
}
db.close()

If an object is specified as the argument, it executes a query in 'Query By Example' style, and return a generator to retrieve the result.

e.g.
for (i: db.query(Pilot())) println(i.name)

If a class and a unary function are specified, it executes a query in Predicate style, and return a generator to retrieve the result.

e.g.
oc = Db4o.configure().objectClass(zip)
oc.maximumActivationDepth(0)
oc.objectField("zip_code").indexed(true)
db = open_db4o("zip_db")
for (record : db.query(zip, function (record) record.zip_code == "1000000")){
   println(record)
}
db.close()
db . getObjectContainer ( )
db . objectContainer

Returns the ObjectContainer from the handle that open_db4o() returns.

db . bufferSize

If bufferSize has an integer value, object added with db.set() are buffered up to bufferSize and automatically committed with db.commit().