|
||||||
Object DatabaseAll but the most trivial of Java applications have a need for persistence. The org.waterken.db library provides an easy and effective solution for using persistent Java objects. This software is distributed as part of the WaterkenTM Server, under the Open Source MIT License. DocumentationExampleApplication object
package com.example.counter;
import org.waterken.db.Key;
import org.waterken.db.Transaction;
public final class
Counter implements java.io.Serializable {
private int value;
private
Counter() {}
public static Incrementor
make() {
final Transaction T = Transaction.current();
return new Incrementor(T.create(new Counter()));
}
public static final class
Incrementor implements java.io.Serializable {
private Key scope;
Incrementor(final Key scope) {
this.scope = scope;
}
public int
getValue() { return ((Counter)scope.query()).value; }
public int
increment() {
final Counter m = (Counter)scope.update();
return ++m.value;
}
}
}
Using a database
package com.example.counter;
import org.waterken.db.Body;
import org.waterken.db.Database;
import org.waterken.db.Root;
import java.io.File;
public final class
Main {
public static Database
create(final File root) {
Database db = org.waterken.jdb.Database.create(root,
org.waterken.module.java.Maker.make(new File[] {},
org.waterken.module.confined.Maker.make()));
db.execute(new Body() {
public Object
run(final Root root) {
root.publish("my counter", Counter.make());
return null;
}
});
return db;
}
public static Database
open(final File root) {
return org.waterken.jdb.Database.open(root);
}
public static int
incrementCounter(final Database db) {
Integer r = (Integer)db.execute(new Body() {
public Object
run(final Root root) {
Counter.Incrementor counter =
(Counter.Incrementor)root.access(null, "my counter");
return new Integer(counter.increment());
}
});
return r.intValue();
}
}
|
||||||
|
top
Copyright 2000 - 2005 Waterken Inc. All rights reserved. |