Skip to content

Instantly share code, notes, and snippets.

@drogus
Created December 29, 2008 17:17
Show Gist options
  • Save drogus/41314 to your computer and use it in GitHub Desktop.
Save drogus/41314 to your computer and use it in GitHub Desktop.
import com.db4o.*;
import com.db4o.activation.ActivationPurpose;
import com.db4o.activation.Activator;
import com.db4o.collections.ArrayList4;
import com.db4o.config.Configuration;
import com.db4o.diagnostic.Diagnostic;
import com.db4o.diagnostic.DiagnosticListener;
import com.db4o.query.*;
import com.db4o.ta.Activatable;
import com.db4o.ta.NotTransparentActivationEnabled;
import com.db4o.ta.TransparentActivationSupport;
class Author implements Activatable {
private ArrayList4<Book> books;
private String name;
private Activator _activator;
public Author(String name) {
this.name = name;
this.books = new ArrayList4<Book>();
}
public void addBook(Book book) {
activate(ActivationPurpose.WRITE);
if(!books.contains(book)) {
books.add(book);
}
}
public String getName() {
activate(ActivationPurpose.READ);
return name;
}
public ArrayList4<Book> getBooks() {
activate(ActivationPurpose.READ);
return this.books;
}
@Override
public String toString() {
return "Author: " + getName() + "; Books: " + getBooks().size();
}
/*Bind the class to the specified object container, create the activator*/
public void bind(Activator activator) {
if (_activator == activator) {
return;
}
if (activator != null && _activator != null) {
throw new IllegalStateException();
}
_activator = activator;
}
/*Call the registered activator to activate the next level,
* the activator remembers the objects that were already
* activated and won't activate them twice.
*/
public void activate(ActivationPurpose purpose) {
if (_activator == null)
return;
_activator.activate(purpose);
}
}
class Book implements Activatable {
private String title;
private Activator _activator;
public Book(String title) {
this.title = title;
}
/*Bind the class to the specified object container, create the activator*/
public void bind(Activator activator) {
if (_activator == activator) {
return;
}
if (activator != null && _activator != null) {
throw new IllegalStateException();
}
_activator = activator;
}
/*Call the registered activator to activate the next level,
* the activator remembers the objects that were already
* activated and won't activate them twice.
*/
public void activate(ActivationPurpose purpose) {
if (_activator == null)
return;
_activator.activate(purpose);
}
}
public class Books {
public static void clear(ObjectContainer db, Class klass) {
Query query = db.query();
query.constrain(klass);
ObjectSet results = query.execute();
while(results.hasNext()) {
db.delete(results.next());
}
}
public static void run(boolean store) {
ObjectContainer db = Db4o.openFile(configureTA(), "db/test.db4o");
System.out.println("Clear.");
//clear database
clear(db, Author.class);
clear(db, Book.class);
Author author = new Author("Philip K. Dick");
if(store) {
System.out.println("Store author");
db.store(author); // this line will screw things up
}
Book ubik = new Book("Ubik");
Book blade = new Book("Blade Runner");
System.out.println("Add books.");
author.addBook(ubik);
author.addBook(blade);
System.out.println("Second store.");
db.store(author);
db.close();
}
public static void print() {
ObjectContainer db = Db4o.openFile(configureTA(), "db/test.db4o");
Query query = db.query();
query.constrain(Author.class);
ObjectSet results = query.execute();
while(results.hasNext()) {
System.out.println(results.next());
}
db.close();
}
public static void main(String[] args) {
run(false);
print();
run(true);
print();
}
private static void activateDiagnostics(Configuration configuration) {
// Add diagnostic listener that will show all the classes that are not
// TA aware.
configuration.diagnostic().addListener(new DiagnosticListener() {
public void onDiagnostic(Diagnostic diagnostic) {
if (!(diagnostic instanceof NotTransparentActivationEnabled)) {
return;
}
System.out.println(diagnostic.toString());
}
});
}
// end activateDiagnostics
private static Configuration configureTA() {
Configuration configuration = Db4o.newConfiguration();
// add TA support
configuration.add(new TransparentActivationSupport());
// activate TA diagnostics to reveal the classes that are not TA-enabled.
//activateDiagnostics(configuration);
return configuration;
}
}
With first store disabled:
Author: Philip K. Dick; Books: 2
With first store enabled:
Author: Philip K. Dick; Books: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment