Class: Rod::Berkeley::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rod/berkeley/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Database

Initializes the database as working within the given environment.



12
13
14
15
16
# File 'lib/rod/berkeley/database.rb', line 12

def initialize(environment)
  # TODO allow standalone databases
  @environment = environment
  @opened = false
end

Instance Attribute Details

#environmentObject (readonly)

The environment of the database.



5
6
7
# File 'lib/rod/berkeley/database.rb', line 5

def environment
  @environment
end

#pathObject (readonly)

The path of the database.



8
9
10
# File 'lib/rod/berkeley/database.rb', line 8

def path
  @path
end

Class Method Details

.key_missing_exceptionObject

The C definition of the KeyMissing exception.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rod/berkeley/database.rb', line 86

def key_missing_exception
  str =<<-END
  |VALUE keyMissingException(){
  |  VALUE klass;
  |
  |  klass = rb_const_get(rb_cObject, rb_intern("Rod"));
  |  klass = rb_const_get(klass, rb_intern("KeyMissing"));
  |  return klass;
  |}
  END
  str.margin
end

Instance Method Details

#closeObject

Closes the database.



56
57
58
59
# File 'lib/rod/berkeley/database.rb', line 56

def close
  _close
  @opened = false
end

#get(key, transaction = nil) ⇒ Object

Return the value of the database for the specified key. The operation might be protected by the transaction. The key is marshaled before being stored looked up in the database.



79
80
81
82
# File 'lib/rod/berkeley/database.rb', line 79

def get(key,transaction=nil)
  marshaled_key = Marshal.dump(key)
  Marshal.load(_get_strings(marshaled_key,transaction))
end

#open(path, access_method, options = {}) ⇒ Object

Opens the Berkeley DB database at given path with given access_method.

The followin access methods are supported (see the Berkeley DB documentation for full description of the access methods):

  • :btree - sorted, balanced tree

  • :hash - hash table

  • :queue - queue

  • :recno - recno

  • :heap - heap NOT SUPPORTED!

The following options are supported (see the Berkeley DB documentation for full description of the flags):

  • +:auto_commit: - automaticaly commit each database change, BDB: DB_AUTO_COMMIT

  • +:create: - create the database if it doesn’t exist, BDB: DB_CREATE

  • +:create_exclusive: - check if the database exists when creating it, if it exists - raise an error, BDB: DB_EXCL

  • +:multiversion: - use multiversion concurrency control to perform transaction snapshot isolation, BDB: DB_MULTIVERSION

  • +:no_mmap: - don’t map this database to the process memory, BDB: DB_NOMMAP

  • +:readonly: - work in readonly mode - all database changes will fail, BDB: DB_RDONLY

  • +:read_uncommitted: - perform transaction degree 1 isolation, BDB: DB_READ_UNCOMMITTED

  • :threads allow multiple threads within one process to access the databases, BDB: DB_THREAD

  • +:truncate: - empty the database on creation, BDB: DB_TRUNCATE

Raises:



45
46
47
48
49
50
51
52
53
# File 'lib/rod/berkeley/database.rb', line 45

def open(path,access_method,options={})
  # TODO check for validity of the method
  # TODO check for validity of options
  # TODO check for conflicting options
  raise DatabaseError.new("The database is already opened at #{@path}.") if opened?
  _open(path,access_method,options)
  @path = path
  @opened = true
end

#opened?Boolean

Returns true if the database is opened.

Returns:

  • (Boolean)


62
63
64
# File 'lib/rod/berkeley/database.rb', line 62

def opened?
  @opened
end

#put(key, value, transaction = nil) ⇒ Object

Put the value to the database at the specified key. The operation might be protected by the transaction. Both the value and the key are marshaled before being stored.



69
70
71
72
73
# File 'lib/rod/berkeley/database.rb', line 69

def put(key,value,transaction=nil)
  marshaled_key = Marshal.dump(key)
  marshaled_value = Marshal.dump(value)
  _put_strings(marshaled_key,marshaled_value,transaction)
end