Class: Rufus::Tokyo::Dystopia::Core
Tokyo Dystopia Indexed Database
http://tokyocabinet.sourceforge.net/dystopiadoc/#dystopiaapi
Constructor Summary
Opens/creates a new Tokyo dystopia database
The modes are equivelent to those when opening a file:
‘r’ : readonly ‘r’ : read/write does not create or truncate ‘w’ : write only, create and truncate ‘w’ : read/write, create and truncate ‘a’ : write only, create if db does not exist ‘a+’ : read/write, create if db does not exist
The third parameter ‘locking’ can be one of ‘true’, ‘false’ or :nonblocking
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 81 def initialize( path, mode = "a+", locking = true ) mode_bits = Core.mode_to_bits( mode ) raise Error.new( "Invalid mode '#{mode}'" ) unless mode_bits lock_bits = Core.locking_to_bits( locking ) raise Error.new( "Invalid Locking mode #{locking}" ) unless lock_bits @db = lib.tcidbnew() rc = lib.tcidbopen( @db, path, mode_bits | lock_bits ) raise_error unless rc == 1 end |
Public Visibility
Public Class Method Summary
| lib | |
|---|---|
| locking_to_bits(locking) | |
| mode_to_bits(mode) |
Public Instance Method Summary
| #clear |
Remove all records from the db. |
|---|---|
| #close |
Close and detach from the database. |
| #count #rnum |
Return the number of records in the database. |
| #delete(id) |
Remove the given document from the index. |
| #fetch(id) |
Return the document at the specified index. |
| #fsize |
return the disk space used by the index. |
| #lib | |
| #path |
Report the path of the database. |
| #search(expression) |
Return the document ids of the documents that matche the search expression. |
| #store(id, text) |
Add a new document to the database. |
Public Class Method Details
lib
36 37 38 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 36 def self.lib ::Rufus::Tokyo::DystopiaLib end |
locking_to_bits
53 54 55 56 57 58 59 60 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 53 def self.locking_to_bits( locking ) @locking_to_bits ||= { true => 0, false => lib::NOLCK, :nonblocking => lib::LCKNB } return @locking_to_bits[locking] end |
mode_to_bits
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 40 def self.mode_to_bits( mode ) @modes_to_bits ||= { "r" => lib::READER, "r+" => lib::READER | lib::WRITER, "w" => lib::WRITER | lib::CREAT | lib::TRUNC, "w+" => lib::READER | lib::WRITER | lib::CREAT | lib::TRUNC, "a" => lib::WRITER | lib::CREAT, "a+" => lib::READER | lib::WRITER | lib::CREAT , } return @modes_to_bits[mode] end |
Public Instance Method Details
clear
Remove all records from the db
159 160 161 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 159 def clear lib.tcidbvanish( @db ) end |
close
Close and detach from the database. This instance can not be used anymore
96 97 98 99 100 101 102 103 104 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 96 def close rc = lib.tcidbclose( @db ) raise_error unless rc == 1 lib.tcidbdel( @db ) raise_error unless rc == 1 @db = nil end |
count
Also known as: rnum
Return the number of records in the database
174 175 176 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 174 def count lib.tcidbrnum( @db ) end |
delete
Remove the given document from the index
117 118 119 120 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 117 def delete( id ) rc = lib.tcidbout( @db, id ) raise_error unless rc == 1 end |
fetch
Return the document at the specified index
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 125 def fetch( id ) r = nil begin r = lib.tcidbget( @db, id ) rescue => e # if we have 'no record found' then return nil if lib.tcidbecode( @db ) == 22 then return nil else raise_error end end return r end |
fsize
return the disk space used by the index
182 183 184 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 182 def fsize lib.tcidbfsiz( @db ) end |
lib
62 63 64 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 62 def lib Core.lib end |
path
Report the path of the database
166 167 168 169 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 166 def path s = lib.tcidbpath( @db ) return File.( s ) if s end |
search
Return the document ids of the documents that matche the search expression
http://tokyocabinet.sourceforge.net/dystopiadoc/#dystopiaapi and scroll down to ‘Compound Expression of Search’
146 147 148 149 150 151 152 153 154 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 146 def search( expression ) out_count = ::FFI::MemoryPointer.new :pointer out_list = ::FFI::MemoryPointer.new :pointer out_list = lib.tcidbsearch2( @db, expression, out_count ) count = out_count.read_int results = out_list.get_array_of_uint64(0, count ) return results end |
store
Add a new document to the database
109 110 111 112 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 109 def store( id, text ) rc = lib.tcidbput( @db, id, text ) raise_error unless rc == 1 end |