Class: OklahomaMixer::HashDatabase

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/oklahoma_mixer/hash_database.rb,
lib/oklahoma_mixer/hash_database/c.rb

Direct Known Subclasses

BTreeDatabase, FixedLengthDatabase, TableDatabase

Defined Under Namespace

Modules: C

Constant Summary collapse

MODES =

Constants ###

{ "r" => :OREADER,
"w" => :OWRITER,
"c" => :OCREAT,
"t" => :OTRUNC,
"e" => :ONOLCK,
"f" => :OLCKNB,
"s" => :OTSYNC }
OPTS =
{ "l" => :TLARGE,
"d" => :TDEFLATE,
"b" => :TBZIP,
"t" => :TTCBS }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, *args) ⇒ HashDatabase

File System ###


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oklahoma_mixer/hash_database.rb', line 28

def initialize(path, *args)
  options              = args.last.is_a?(Hash)   ? args.last  : { }
  mode                 = !args.first.is_a?(Hash) ? args.first : nil
  @path                = path
  @db                  = lib.new
  self.default         = options[:default]
  @in_transaction      = false
  @abort               = false
  @nested_transactions = options[:nested_transactions]
  
  try(:setmutex) if options[:mutex]
  tune(options)
  
  warn "mode option supersedes mode argument" if mode and options[:mode]
  mode_enum  = cast_to_enum_int(options.fetch(:mode, mode || "wc"), :mode)
  @read_only = (mode_enum & cast_to_enum_int("r", :mode)).nonzero?
  try(:open, path, mode_enum)
rescue Exception
  close if defined?(@db) and @db
  raise
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


58
59
60
# File 'lib/oklahoma_mixer/hash_database.rb', line 58

def path
  @path
end

Instance Method Details

#[](key) ⇒ Object


157
158
159
160
161
# File 'lib/oklahoma_mixer/hash_database.rb', line 157

def [](key)
  fetch(key, &@default)
rescue IndexError
  nil
end

#abortObject


310
311
312
313
314
# File 'lib/oklahoma_mixer/hash_database.rb', line 310

def abort
  fail Error::TransactionError, "not in transaction" unless @in_transaction
  @abort = true
  throw :finish_transaction
end

#clearObject


199
200
201
202
# File 'lib/oklahoma_mixer/hash_database.rb', line 199

def clear
  try(:vanish)
  self
end

#closeObject


83
84
85
# File 'lib/oklahoma_mixer/hash_database.rb', line 83

def close
  try(:del)  # closes before it deletes the object
end

#commitObject


305
306
307
308
# File 'lib/oklahoma_mixer/hash_database.rb', line 305

def commit
  fail Error::TransactionError, "not in transaction" unless @in_transaction
  throw :finish_transaction
end

#copy(path) ⇒ Object Also known as: backup


74
75
76
# File 'lib/oklahoma_mixer/hash_database.rb', line 74

def copy(path)
  try(:copy, path)
end

#default(key = nil) ⇒ Object

Getting and Setting Keys ###


91
92
93
# File 'lib/oklahoma_mixer/hash_database.rb', line 91

def default(key = nil)
  @default[key] if @default
end

#default=(value_or_proc) ⇒ Object


95
96
97
98
99
100
101
# File 'lib/oklahoma_mixer/hash_database.rb', line 95

def default=(value_or_proc)
  @default = case value_or_proc
             when Proc then value_or_proc
             when nil  then nil
             else           lambda { |key| value_or_proc }
             end
end

#defrag(steps = 0) ⇒ Object


79
80
81
# File 'lib/oklahoma_mixer/hash_database.rb', line 79

def defrag(steps = 0)
  try(:defrag, steps.to_i)
end

#delete(key, &missing_handler) ⇒ Object


191
192
193
194
195
196
197
# File 'lib/oklahoma_mixer/hash_database.rb', line 191

def delete(key, &missing_handler)
  value = fetch(key, &missing_handler)
  try(:out, cast_key_in(key), :no_error => {22 => nil})
  value
rescue IndexError
  nil
end

#delete_ifObject


259
260
261
262
263
# File 'lib/oklahoma_mixer/hash_database.rb', line 259

def delete_if
  each do |key, value|
    delete(key) if yield key, value
  end
end

#eachObject Also known as: each_pair


239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/oklahoma_mixer/hash_database.rb', line 239

def each
  try(:iterinit)
  loop do
    Utilities.temp_xstr do |key|
      Utilities.temp_xstr do |value|
        return self unless try( :iternext3, key.pointer, value.pointer,
                                :no_error => {22 => false} )
        yield [cast_key_out(key.to_s), cast_value_out(value.to_s)]
      end
    end
  end
end

#each_keyObject


229
230
231
232
233
234
235
236
237
# File 'lib/oklahoma_mixer/hash_database.rb', line 229

def each_key
  try(:iterinit)
  loop do
    return self unless key = try( :read_from_func, :iternext,
                                  :failure  => nil,
                                  :no_error => {22 => nil} )
    yield cast_key_out(key)
  end
end

#each_valueObject


253
254
255
256
257
# File 'lib/oklahoma_mixer/hash_database.rb', line 253

def each_value
  each do |key, value|
    yield value
  end
end

#empty?Boolean


219
220
221
# File 'lib/oklahoma_mixer/hash_database.rb', line 219

def empty?
  size.zero?
end

#fetch(key, *default) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/oklahoma_mixer/hash_database.rb', line 141

def fetch(key, *default)
  if value = try( :read_from_func, :get, cast_key_in(key),
                  :failure => nil, :no_error => {22 => nil} )
    cast_value_out(value)
  else
    if block_given?
      warn "block supersedes default value argument" unless default.empty?
      yield key
    elsif not default.empty?
      default.first
    else
      fail IndexError, "key not found"
    end
  end
end

#file_sizeObject


64
65
66
# File 'lib/oklahoma_mixer/hash_database.rb', line 64

def file_size
  lib.fsiz(@db)
end

#flushObject Also known as: sync, fsync


68
69
70
# File 'lib/oklahoma_mixer/hash_database.rb', line 68

def flush
  try(:sync)
end

#include?(key) ⇒ Boolean Also known as: has_key?, key?, member?


204
205
206
207
208
209
# File 'lib/oklahoma_mixer/hash_database.rb', line 204

def include?(key)
  fetch(key)
  true
rescue IndexError
  false
end

#keys(options = { }) ⇒ Object


174
175
176
177
178
179
180
181
# File 'lib/oklahoma_mixer/hash_database.rb', line 174

def keys(options = { })
  prefix = options.fetch(:prefix, "").to_s
  limit  = options.fetch(:limit,  -1)
  list   = ArrayList.new(lib.fwmkeys(@db, prefix, prefix.size, limit))
  list.map { |key| cast_key_out(key) }
ensure
  list.free if list
end

#optimize(options) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/oklahoma_mixer/hash_database.rb', line 50

def optimize(options)
  try( options[:tune] ? :tune : :optimize,
       options.fetch(:bnum,  0).to_i,
       options.fetch(:apow, -1).to_i,
       options.fetch(:fpow, -1).to_i,
       cast_to_enum_int(options.fetch(:opts, 0xFF), :opt) )
end

#read_only?Boolean


60
61
62
# File 'lib/oklahoma_mixer/hash_database.rb', line 60

def read_only?
  @read_only
end

#sizeObject Also known as: length


214
215
216
# File 'lib/oklahoma_mixer/hash_database.rb', line 214

def size
  lib.rnum(@db)
end

#store(key, value, mode = nil) ⇒ Object Also known as: []=


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/oklahoma_mixer/hash_database.rb', line 103

def store(key, value, mode = nil)
  k      = cast_key_in(key)
  v      = cast_value_in(value) unless mode == :add and not block_given?
  result = value
  if block_given?
    warn "block supersedes mode argument" unless mode.nil?
    callback = lambda { |old_value_pointer, old_size, returned_size, _|
      old_value         = old_value_pointer.get_bytes(0, old_size)
      replacement, size = cast_value_in(yield(key, old_value, value))
      returned_size.put_int(0, size)
      pointer = Utilities.malloc(size)
      pointer.put_bytes(0, replacement) unless pointer.address.zero?
      pointer
    }
    try(:putproc, k, v, callback, nil)
  else
    if mode == :keep
      result = try(:putkeep, k, v, :no_error => {21 => false})
    elsif mode == :cat
      try(:putcat, k, v)
    elsif mode == :async and self.class == HashDatabase
      try(:putasync, k, v)
    elsif mode == :add
      result = case value
               when Float then try( :adddouble, k, value,
                                    :failure => lambda { |n| n.nan? } )
               else            try( :addint, k, value.to_i,
                                    :failure => Utilities::INT_MIN )
               end
    else
      warn "unsupported mode for database type" if mode
      try(:put, k, v)
    end
  end
  result
end

#to_hash(set_default = true) ⇒ Object


265
266
267
268
269
270
271
272
# File 'lib/oklahoma_mixer/hash_database.rb', line 265

def to_hash(set_default = true)
  default = @default && lambda { |hash, key| @default[key] } if set_default
  hash    = Hash.new(&default)
  each do |key, value|
    hash[key] ||= value
  end
  hash
end

#transactionObject

Transactions ###


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/oklahoma_mixer/hash_database.rb', line 278

def transaction
  if @in_transaction
    case @nested_transactions
    when :ignore
      return yield
    when :fail, :raise
      fail Error::TransactionError, "nested transaction"
    end
  end
  
  @in_transaction = true
  @abort          = false
  
  begin
    catch(:finish_transaction) do
      try(:tranbegin)
      yield
    end
  rescue Exception
    @abort = true
    raise
  ensure
    try("tran#{@abort ? :abort : :commit}")
    @in_transaction = false
  end
end

#update(hash, &dup_handler) ⇒ Object


163
164
165
166
167
168
# File 'lib/oklahoma_mixer/hash_database.rb', line 163

def update(hash, &dup_handler)
  hash.each do |key, value|
    store(key, value, &dup_handler)
  end
  self
end

#valuesObject


183
184
185
186
187
188
189
# File 'lib/oklahoma_mixer/hash_database.rb', line 183

def values
  values = [ ]
  each_value do |value|
    values << value
  end
  values
end

#values_at(*keys) ⇒ Object


170
171
172
# File 'lib/oklahoma_mixer/hash_database.rb', line 170

def values_at(*keys)
  keys.map { |key| self[key] }
end