Class: OklahomaMixer::BTreeDatabase

Inherits:
HashDatabase show all
Defined in:
lib/oklahoma_mixer/b_tree_database.rb,
lib/oklahoma_mixer/b_tree_database/c.rb

Defined Under Namespace

Modules: C

Constant Summary

Constants inherited from HashDatabase

HashDatabase::MODES, HashDatabase::OPTS

Instance Attribute Summary

Attributes inherited from HashDatabase

#path

Instance Method Summary collapse

Methods inherited from HashDatabase

#[], #abort, #clear, #close, #commit, #copy, #default, #default=, #defrag, #empty?, #fetch, #file_size, #flush, #include?, #initialize, #read_only?, #to_hash, #transaction, #update, #values_at

Constructor Details

This class inherits a constructor from OklahomaMixer::HashDatabase

Instance Method Details

#delete(key, mode = nil, &missing_handler) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 86

def delete(key, mode = nil, &missing_handler)
  if mode == :dup
    values = values(key)
    if try(:out3, cast_key_in(key), :no_error => {22 => false})
      values
    else
      missing_handler ? missing_handler[key] : values
    end
  else
    super(key, &missing_handler)
  end
end

#delete_if(start = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 143

def delete_if(start = nil)
  cursor(start) do |iterator|
    loop do
      break unless key_and_value = iterator.key_and_value
      test = yield( cast_key_out(key_and_value.first),
                    cast_value_out(key_and_value.last) )
      break unless iterator.send(test ? :delete : :next)
    end
  end
end

#each(start = nil) ⇒ Object Also known as: each_pair



119
120
121
122
123
124
125
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 119

def each(start = nil)
  cursor_in_loop(start) do |iterator|
    throw(:finish_iteration) unless key_and_value = iterator.key_and_value
    yield [ cast_key_out(key_and_value.first),
            cast_value_out(key_and_value.last) ]
  end
end

#each_key(start = nil) ⇒ Object

Iteration ###



112
113
114
115
116
117
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 112

def each_key(start = nil)
  cursor_in_loop(start) do |iterator|
    throw(:finish_iteration) unless key = iterator.key
    yield cast_key_out(key)
  end
end

#each_value(start = nil) ⇒ Object



136
137
138
139
140
141
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 136

def each_value(start = nil)
  cursor_in_loop(start) do |iterator|
    throw(:finish_iteration) unless value = iterator.value
    yield cast_value_out(value)
  end
end

#keys(options = { }) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 42

def keys(options = { })
  if options.include? :range
    warn "range supersedes prefix" if options[:prefix]
    range = options[:range]
    fail ArgumentError, "Range expected" unless range.is_a? Range
    start          = cast_key_in(range.first)
    include_start  = !options.fetch(:exclude_start, false)
    finish         = cast_key_in(range.last)
    include_finish = !range.exclude_end?
    limit          = options.fetch(:limit, -1)
    begin
      list = ArrayList.new( lib.range( @db,
                                       *[ start,  include_start,
                                          finish, include_finish,
                                          limit ].flatten ) )
      list.map { |key| cast_key_out(key) }
    ensure
      list.free if list
    end
  else
    super
  end
end

#optimize(options) ⇒ Object

File System ###



12
13
14
15
16
17
18
19
20
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 12

def optimize(options)
  try( options[:tune] ? :tune : :optimize,
       options.fetch(:lmemb,  0).to_i,
       options.fetch(:nmemb,  0).to_i,
       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

#reverse_each(start = nil) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 128

def reverse_each(start = nil)
  cursor_in_loop(start, :reverse) do |iterator|
    throw(:finish_iteration) unless key_and_value = iterator.key_and_value
    yield [ cast_key_out(key_and_value.first),
            cast_value_out(key_and_value.last) ]
  end
end

#size(key = nil) ⇒ Object Also known as: length



99
100
101
102
103
104
105
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 99

def size(key = nil)
  if key.nil?
    super()
  else
    try(:vnum, cast_key_in(key), :failure => 0, :no_error => {22 => 0})
  end
end

#store(key, value, mode = nil) ⇒ Object

Getting and Setting Keys ###



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 26

def store(key, value, mode = nil)
  if mode == :dup
    if value.is_a? Array
      Utilities.temp_list(value.size) do |list|
        list.push(*value) { |string| cast_value_in(string) }
        try(:putdup3, cast_key_in(key), list.pointer)
      end
    else
      try(:putdup, cast_key_in(key), cast_value_in(value))
    end
    value
  else
    super
  end
end

#values(key = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/oklahoma_mixer/b_tree_database.rb', line 66

def values(key = nil)
  if key.nil?
    super()
  else
    begin
      pointer = try( :get4, cast_key_in(key),
                     :failure  => lambda { |ptr| ptr.address.zero? },
                     :no_error => {22 => nil} )
      if pointer.nil?
        [ ]
      else
        list = ArrayList.new(pointer)
        list.map { |value| cast_value_out(value) }
      end
    ensure
      list.free if list
    end
  end
end