ActiveDocument

ActiveDocument is a persistent Model store built on Berkeley DB. It was inspired by ActiveRecord, and in some cases, it can be used as a drop-in replacement. The performance of ActiveDocument can exceed a traditional ORM for many applications, because the database is stored locally and all lookups must use a predefined index. Also, attributes do not have to be cast after they are read from the database like in ActiveRecord. Instead, Ruby objects are stored directly in Berkeley DB and loaded using Marshal, which makes loading objects much faster. For more information on the diffences between Berkeley DB and a relational Database, see (www.oracle.com/database/docs/Berkeley-DB-v-Relational.pdf).

Usage:

require 'rubygems'
require 'active_document'

class User < ActiveDocument::Base
  path '/data/bdb'
  accessor :first_name, :last_name, :username, :email_address, :tags

  primary_key :username
  index_by [:last_name, :first_name]
  index_by :email_address, :unique => true
  index_by :tags, :multi_key => true
end

User.create(
  :first_name => 'John',
  :last_name  => 'Stewart',
  :username   => 'lefty',
  :email_address => '[email protected]',
  :tags => [:funny, :liberal]
)

User.create(
  :first_name => 'Martha',
  :last_name  => 'Stewart',
  :username   => 'helen',
  :email_address => '[email protected]',
  :tags => [:conservative, :convict]
)

User.create(
  :first_name => 'Stephen',
  :last_name  => 'Colbert',
  :username   => 'steve',
  :email_address => '[email protected]',
  :tags => [:conservative, :funny]
)

User.find('lefty').attributes
=> {"username"=>"lefty", "last_name"=>"Stewart", "email_address"=>"[email protected]", "first_name"=>"John"}

User.find_by_email_address("[email protected]").username
=> "lefty"

User.find_all_by_last_name("Stewart").collect {|u| u.first_name}
=> ["John", "Martha"]

User.find_all_by_tag(:funny).collect {|u| u.username}
=> ["lefty", "steve"]

Complex finds:

Any find can take multiple keys, a key range, or multiple key ranges.

User.create(
  :first_name => 'Will',
  :last_name  => 'Smith',
  :username   => 'legend',
  :email_address => '[email protected]',
  :tags => [:actor, :rapper]
)

User.find_all_by_last_name("Stewart", "Smith").collect {|u| u.username}
=> ["lefty", "helen", "legend"]

User.find_all_by_last_name("Smith".."Stuart").collect {|u| u.username}
=> ["legend", "lefty", "helen"]

User.find_all_by_last_name("Smith".."Stuart", "Colbert").collect {|u| u.username}
=> ["legend", "lefty", "helen", "steve"]

User.find_all_by_last_name("Aardvark".."Daisy", "Smith".."Stuart").collect {|u| u.username}
=> ["steve", "legend", "lefty", "helen"]

Limit and Offset:

Any find can also take :limit, :offset, :page, and :per_page as options. These can be used for paginating large lists.

User.find_all_by_username(:limit => 2).collect {|u| u.username}
=> ["helen", "lefty"]

User.find_all_by_username(:limit => 2, :offset => 2).collect {|u| u.username}
=> ["legend", "steve"]

User.find_all_by_username(:per_page => 2, :page => 1).collect {|u| u.username}
=> ["helen", "lefty"]

User.find_all_by_username(:per_page => 2, :page => 2).collect {|u| u.username}
=> ["legend", "steve"]

Install:

sudo gem install ninjudd-bdb -s http://gems.github.com
sudo gem install ninjudd-tuple -s http://gems.github.com
sudo gem install ninjudd-active_document -s http://gems.github.com

License:

Copyright © 2009 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE