Technology news and Jobs
The Linux distillery
Rapid Linux apps using object databases
The Linux distillery
Rapid Linux apps using object databases | Rapid Linux apps using object databases |
|
| by David M Williams | |
| Wednesday, 11 June 2008 | |
|
Page 3 of 3 Let’s use our collection of books as described above; we’ll make classes to represent authors and actual publications. For each author we’ll track all the publications they’ve authored or co-authored. For all the books we’ll track who each author is.Featured Whitepaper
5 Best Practices for Smartphone Support
public class Author { private String name; private Date birthday; private Set<Publication> pubs; public Author(String name) { ... } public String getName() { ... } public void setName(String name) { ... } } public class Publication { private String title; private int year; private List<Author> authors; public Publication(String title) { ... } public String getTitle() { ... } public void setTitle(String title) { ... } } Now, to make a db4o database we don’t have to worry about ODBC drivers, or ports and sockets. It’s one line like so: ObjectContainer database = Db4o.openFile (“pubs.db”); As you can probably guess, this creates a disk file to store your data. For your own purposes – or for a web server or some other application that is only going to be executed in one place – this is fine. For a traditional fat client program that is to be used by many people and systems over a wide area network this won’t be sufficient. For those requirements db4o does offer a client/server mode. Let’s make an author and a publication. Publication CodeComplete = new Publication(“Code Complete”); Author steve = new Author(“Steve McConnell”); steve.spouse = “...”; steve.DateOfBirth = “...”; CodeComplete.addAuthor(steve); Ok! We have a book which also references an author. Let’s save the whole lot to disk. Brace yourself – database.set(CodeComplete); Done! That one line of code saves both objects to disk at the one time. There was no programming work on our part to save the individual fields that make up the book, to loop through its collection of authors, and save each of those being careful to maintain key linkages. At a later time, we can reload all the publications we may have with one line again: ObjectSet<Publication> publications = database.get(Publication.class); Alternatively, we may wish to restore just a specific publication. In that case, we fill in one field like so: Author steve = new Author(“Steve McConnell”); ObjectSet<Author> authors = database.get(steve); The object with author name “Steve McConnell” is restored from disk, and we now have his spouse name, date of birth and any books he contributed to. We can update objects by simply altering their values and re-setting them, like this: steve.Spouse = “zzz”; database.set(steve); The original ‘steve’ object on disk is overwritten with the new information. db4o has enough smarts not to make a new object and thus append to what we already have stored, but it updates the existing instance. Deleting objects is just as simple; database.delete(steve); I am certain you will agree this really is a no-brainer to work with. And yet, it’s not a trite system either. Database experts will be interested to know if db4o offers multi-threading, flexible indexing, atomic transactions and other such things. The answer to these is yes; it is high performance and maintains integrity of its data. db4o does not offer stored procedures but that’s not really an issue; the focus turns back onto putting code within the program and leaving the database to just store data. If you’re a developer, no matter the platform, give an object database a try and see how it can revolutionise your coding, as well as pave the way for more developers to come across to Linux. |
| < Next story in category | Previous story in the category > |
|---|




Tags




