Embedding the db4o Object-Oriented Database
where <filename> is the path to the file that holds the persistent content of the ObjectContainer. You put an object into the ObjectContainer using the set() method. So, we can store our new definition with:
db.set(_float);
which, believe it or not, is just about all you need to know about the set() method. That one call stores not only the _float DictEntry object, but all of its contained Defn objects as well. When you call db4o's set() method, the db4o engine invisibly spiders through the object's references, persisting all the child objects automatically. Just pass set() the root object of a complicated object tree, and the whole shebang is stored at one shot. You don't have to tell db4o about your object's structure; it discovers it automatically.
To retrieve an object from an ObjectContainer, we locate it with the help of db4o's QBE (query by example) mechanism. A QBE-style query is guided by an example, or template, object. More specifically, you perform a query by creating a template object, populating its fields with the values you want matched, showing the template object to the query system and saying, “See this? Go get all the objects that look like this one.”
So, assuming you want to retrieve our definitions for float, the process looks something like this:
// Create template
DictEntry DTemplate = new DictEntry("float", "");
// Execute QBE
ObjectSet results = db.get(DTemplate);
// Iterate through results set
while(results.hasNext())
{
DictEntry _entry = (DictEntry)results.next();
... process the DictEntry object ...
}
First, we create the template object, filling the fields we're interested in with the values we want matched. Fields that shouldn't participate in the query are filled with zero, the empty string, or null—depending on the data type. (In the above example, we're simply looking for the word float in the dictionary. We put an empty string in the pronunciation field for the templater object constructor, because the pronunciation is irrelevant to the query.)
Then, we execute the query by calling the ObjectContainer's get() method, with the template object passed in as the single argument. The query returns an ObjectSet, through which we can iterate to retrieve the results of the match.
At this point, we can easily create a database, fill it with words and definitions, and retrieve them using db4o's QBE mechanism. But, what if we want to experiment with different indexing-driven retrieval mechanisms? Because the database preserves relationships among the persistent objects, we can create custom indexing and navigation structures, place them in the database as well and “wire” our data objects into those structures.
We illustrate how simple this is by creating two dissimilar indexing schemes.
First, we create a binary tree. Each node of the tree carries as its payload a key/data pair. The key will be a text word from the dictionary, and the associated data item will be a reference to the DictEntry object in the database. So, we can fetch the binary tree from the database, execute a search for a specific word in the dictionary and fetch the matching DictEntry object (if found).
The architecture and behavior of binary trees are well known, so we won't go into much detail about them here. (In fact, many frameworks now provide them as standard data structures. We've created an explicit one to show how easily it can be stored in the database.) Our implementation appears in Listing 1. It is rudimentary, supporting only insertion and searching. It doesn't guarantee a balanced tree, but it serves for the purposes of illustration. The TreeNode class, which defines the structure of nodes within the binary tree, appears in Listing 2. (Note, we'll explain the purpose of the calls to db.activate() in Listing 1 shortly.)
Listing 1. Binary Tree
/*
* Binary Tree
*/
using System;
using com.db4o;
namespace PersistentTrees
{
/// <summary>
/// Description of BinaryTree.
/// </summary>
public class BinaryTree
{
// The tree's root
private TreeNode root;
public BinaryTree()
{
root = null;
}
public static BinaryTree nullfactory()
{
return(new BinaryTree());
}
// insert
// Add key to tree with associated object reference
public void insert(string _key, Object _data)
{
// Use recursion for this
root = insert(root, _key, _data);
}
// insert
// This is worker method for inserting key and data
// Insert _key into subtree t with _data associated
private TreeNode insert(TreeNode t, string _key, Object_data)
{
// If this subtree is empty, build a new node
if(t == null)
t = new TreeNode(_key, _data);
else
if(_key.CompareTo(t.Key)<=0)
t.Left = insert(t.Left,_key,_data);
else
t.Right = insert(t.Right,_key,_data);
return(t);
}
// search
// Search for a key in the tree.
// Return the array from the TreeNode if found, null if
// not
// db is the ObjectContainer holding the tree.
public Object[] search(string _key,
ObjectContainer db)
{
TreeNode t = search(root, _key, db);
if(t==null) return(null); // Not found
db.activate(t,4); // Activate to get data
return(t.getData());
}
// search
// This is the worker method for searching.
private TreeNode search(TreeNode t,
string _key,
ObjectContainer db)
{
// Empty tree?
if(t==null) return(null);
if(_key.CompareTo(t.Key)==0) return(t);
if(_key.CompareTo(t.Key)<0)
{
db.activate(t.Left,2);
return(t = search(t.Left,_key,db));
}
db.activate(t.Right,2);
return(t = search(t.Right,_key,db));
}
}
}
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- The Secret Password Is...
- RSS Feeds
- New Products




23 min 19 sec ago
26 min 26 sec ago
27 min 46 sec ago
4 hours 52 min ago
6 hours 43 min ago
11 hours 56 min ago
15 hours 8 min ago
17 hours 23 min ago
17 hours 52 min ago
18 hours 50 min ago