Class: Rufus::Tokyo::Dystopia::Core

Tokyo Dystopia Indexed Database

http://tokyocabinet.sourceforge.net/dystopiadoc/#dystopiaapi

Constructor Summary

public initialize(path, mode = "a+", locking = true)

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

Meta Tags

Raises:

[Error]
[View source]


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

public lib
[View source]


36
37
38
# File 'lib/rufus/tokyo/dystopia/core.rb', line 36

def self.lib
  ::Rufus::Tokyo::DystopiaLib
end

locking_to_bits

public locking_to_bits(locking)
[View source]


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

public mode_to_bits(mode)
[View source]


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

public clear

Remove all records from the db

[View source]


159
160
161
# File 'lib/rufus/tokyo/dystopia/core.rb', line 159

def clear
  lib.tcidbvanish( @db )
end

close

public close

Close and detach from the database. This instance can not be used anymore

[View source]


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

public count

Also known as: rnum

Return the number of records in the database

[View source]


174
175
176
# File 'lib/rufus/tokyo/dystopia/core.rb', line 174

def count
  lib.tcidbrnum( @db )
end

delete

public delete(id)

Remove the given document from the index

[View source]


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

public fetch(id)

Return the document at the specified index

[View source]


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

public fsize

return the disk space used by the index

[View source]


182
183
184
# File 'lib/rufus/tokyo/dystopia/core.rb', line 182

def fsize
  lib.tcidbfsiz( @db )
end

lib

public lib
[View source]


62
63
64
# File 'lib/rufus/tokyo/dystopia/core.rb', line 62

def lib
  Core.lib
end

path

public path

Report the path of the database

[View source]


166
167
168
169
# File 'lib/rufus/tokyo/dystopia/core.rb', line 166

def path
  s = lib.tcidbpath( @db )
  return File.expand_path( s ) if s
end

search

public search(expression)

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’

[View source]


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

public store(id, text)

Add a new document to the database

[View source]


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
Generated on Friday, September 18 2009 at 10:35:45 AM by YARD 0.2.3.5 (ruby-1.8.7).