Class: Rufus::Tokyo::Dystopia::Core
- Inherits:
-
Object
- Object
- Rufus::Tokyo::Dystopia::Core
- Extended by:
- Openable
- Defined in:
- lib/rufus/tokyo/dystopia/core.rb
Overview
Tokyo Dystopia Indexed Database
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all records from the db.
-
#close ⇒ Object
Close and detach from the database.
-
#count ⇒ Object
(also: #rnum)
Return the number of records in the database.
-
#delete(id) ⇒ Object
Remove the given document from the index.
-
#fetch(id) ⇒ Object
Return the document at the specified index.
-
#fsize ⇒ Object
return the disk space used by the index.
-
#initialize(path, mode = "a+", locking = true) ⇒ Core
constructor
Opens/creates a new Tokyo dystopia database.
- #lib ⇒ Object
-
#path ⇒ Object
Report the path of the database.
-
#search(expression) ⇒ Object
Return the document ids of the documents that matche the search expression.
-
#store(id, text) ⇒ Object
Add a new document to the database.
Methods included from Openable
Constructor Details
#initialize(path, mode = "a+", locking = true) ⇒ Core
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
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 84 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() lib.tcidbopen( @db, path, mode_bits | lock_bits ) || raise_error end |
Class Method Details
.lib ⇒ Object
39 40 41 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 39 def self.lib ::Rufus::Tokyo::DystopiaLib end |
.locking_to_bits(locking) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 56 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(mode) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 43 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 |
Instance Method Details
#clear ⇒ Object
Remove all records from the db
157 158 159 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 157 def clear lib.tcidbvanish( @db ) end |
#close ⇒ Object
Close and detach from the database. This instance can not be used anymore
98 99 100 101 102 103 104 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 98 def close lib.tcidbclose( @db ) || raise_error lib.tcidbdel( @db ) @db = nil end |
#count ⇒ Object Also known as: rnum
Return the number of records in the database
172 173 174 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 172 def count lib.tcidbrnum( @db ) end |
#delete(id) ⇒ Object
Remove the given document from the index
116 117 118 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 116 def delete( id ) lib.tcidbout( @db, id ) || raise_error end |
#fetch(id) ⇒ Object
Return the document at the specified index
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 123 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 ⇒ Object
return the disk space used by the index
180 181 182 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 180 def fsize lib.tcidbfsiz( @db ) end |
#path ⇒ Object
Report the path of the database
164 165 166 167 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 164 def path s = lib.tcidbpath( @db ) return File.( s ) if s end |
#search(expression) ⇒ Object
Return the document ids of the documents that matche the search expression
tokyocabinet.sourceforge.net/dystopiadoc/#dystopiaapi and scroll down to ‘Compound Expression of Search’
144 145 146 147 148 149 150 151 152 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 144 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(id, text) ⇒ Object
Add a new document to the database
109 110 111 |
# File 'lib/rufus/tokyo/dystopia/core.rb', line 109 def store( id, text ) lib.tcidbput( @db, id, text ) || raise_error end |