Class: Rod::Berkeley::Database
- Inherits:
-
Object
- Object
- Rod::Berkeley::Database
- Defined in:
- lib/rod/berkeley/database.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
The environment of the database.
-
#path ⇒ Object
readonly
The path of the database.
Class Method Summary collapse
-
.key_missing_exception ⇒ Object
The C definition of the KeyMissing exception.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the database.
-
#get(key, transaction = nil) ⇒ Object
Return the value of the database for the specified
key
. -
#initialize(environment) ⇒ Database
constructor
Initializes the database as working within the given
environment
. -
#open(path, access_method, options = {}) ⇒ Object
Opens the Berkeley DB database at given
path
with givenaccess_method
. -
#opened? ⇒ Boolean
Returns true if the database is opened.
-
#put(key, value, transaction = nil) ⇒ Object
Put the
value
to the database at the specifiedkey
.
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
#environment ⇒ Object (readonly)
The environment of the database.
5 6 7 |
# File 'lib/rod/berkeley/database.rb', line 5 def environment @environment end |
#path ⇒ Object (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_exception ⇒ Object
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
#close ⇒ Object
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
45 46 47 48 49 50 51 52 53 |
# File 'lib/rod/berkeley/database.rb', line 45 def open(path,access_method,={}) # 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,) @path = path @opened = true end |
#opened? ⇒ Boolean
Returns true if the database is opened.
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 |