Class: OklahomaMixer::TableDatabase

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

Defined Under Namespace

Modules: C, Paginated

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, #delete, #delete_if, #each_key, #each_value, #empty?, #file_size, #flush, #include?, #initialize, #keys, #optimize, #read_only?, #size, #to_hash, #transaction, #update, #values, #values_at

Constructor Details

This class inherits a constructor from OklahomaMixer::HashDatabase

Instance Method Details

#add_index(column, type, keep = false) ⇒ Object

Indexes ###


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/oklahoma_mixer/table_database.rb', line 213

def add_index(column, type, keep = false)
  type =  case type.to_s
          when "lexical", "string"  then lib::INDEXES[:TDBITLEXICAL]
          when "decimal", "numeric" then lib::INDEXES[:TDBITDECIMAL]
          when "token"              then lib::INDEXES[:TDBITTOKEN]
          when "qgram"              then lib::INDEXES[:TDBITQGRAM]
          else
            fail Error::IndexError, "unknown index type"
          end
  type |= lib::INDEXES[:TDBITKEEP] if keep
  try( :setindex,
       cast_to_bytes_and_length(column_name(column)).first,
       type,
       :no_error => {21 => false} )
end

#all(options = { }, &iterator) ⇒ Object

Queries ###


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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/oklahoma_mixer/table_database.rb', line 114

def all(options = { }, &iterator)
  query(options) do |q|
    mode = results_mode(options)
    if not iterator.nil? and not read_only?
      results  = self
      callback = lambda { |key_pointer, key_size, doc_map, _|
        if mode != :docs
          key = cast_key_out(key_pointer.get_bytes(0, key_size))
        end
        if mode != :keys
          map = HashMap.new(doc_map)
          doc = map.to_hash { |string| cast_to_encoded_string(string) }
        end
        flags = case mode
                when :keys then iterator[key]
                when :docs then iterator[doc]
                when :aoh  then iterator[doc.merge!(:primary_key => key)]
                else            iterator[[key, doc]]
                end
        Array(flags).inject(0) { |returned_flags, flag|
          returned_flags | case flag.to_s
                           when "update"
                             if mode != :keys
                               map.replace(doc) { |key_or_value|
                                 cast_to_bytes_and_length(key_or_value)
                               }
                             end
                             lib::FLAGS[:TDBQPPUT]
                           when "delete" then lib::FLAGS[:TDBQPOUT]
                           when "break"  then lib::FLAGS[:TDBQPSTOP]
                           else               0
                           end
        }
      }
      unless lib.qryproc(q.pointer, callback, nil)
        error_code    = lib.ecode(@db)
        error_message = lib.errmsg(error_code)
        fail Error::QueryError,
             "#{error_message} (error code #{error_code})"
      end
      results
    else
      query_results(lib.qrysearch(q.pointer), mode, &iterator)
    end
  end
end

#count(options = { }) ⇒ Object


165
166
167
168
169
# File 'lib/oklahoma_mixer/table_database.rb', line 165

def count(options = { })
  count = 0
  all(options.merge(:select => :keys)) { count += 1 }
  count
end

#difference(q, *queries, &iterator) ⇒ Object Also known as: diff


204
205
206
# File 'lib/oklahoma_mixer/table_database.rb', line 204

def difference(q, *queries, &iterator)
  search([q] + queries, lib::SEARCHES[:TDBMSDIFF], &iterator)
end

#eachObject Also known as: each_pair

Iteration ###


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/oklahoma_mixer/table_database.rb', line 96

def each
  try(:iterinit)
  loop do
    pointer = try( :iternext3,
                   :failure  => lambda { |value| value.address.zero? },
                   :no_error => {22 => nil} )
    return self unless pointer
    value = cast_value_out(pointer)
    key   = value.delete("")
    yield [key, value]
  end
end

#fetch(key, *default) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/oklahoma_mixer/table_database.rb', line 70

def fetch(key, *default)
  if value = try( :get, cast_key_in(key),
                  :failure  => lambda { |value| value.address.zero? },
                  :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

#first(options = { }) ⇒ Object


161
162
163
# File 'lib/oklahoma_mixer/table_database.rb', line 161

def first(options = { })
  all(options.merge(:limit => 1)).first
end

#generate_unique_idObject Also known as: uid


87
88
89
# File 'lib/oklahoma_mixer/table_database.rb', line 87

def generate_unique_id
  try(:genuid, :failure => -1)
end

#intersection(q, *queries, &iterator) ⇒ Object Also known as: isect


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

def intersection(q, *queries, &iterator)
  search([q] + queries, lib::SEARCHES[:TDBMSISECT], &iterator)
end

#optimize_index(column) ⇒ Object Also known as: defrag_index


236
237
238
239
240
241
# File 'lib/oklahoma_mixer/table_database.rb', line 236

def optimize_index(column)
  try( :setindex,
       cast_to_bytes_and_length(column_name(column)).first,
       lib::INDEXES[:TDBITOPT],
       :no_error => {2 => false} )
end

#paginate(options) ⇒ Object


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/oklahoma_mixer/table_database.rb', line 171

def paginate(options)
  mode    = results_mode(options)
  results = (mode != :hoh ? [ ] : { }).extend(Paginated)
  fail Error::QueryError, ":page argument required" \
    unless options.include? :page
  results.current_page = (options[:page] || 1).to_i
  fail Error::QueryError, ":page must be >= 1" if results.current_page < 1
  results.per_page = (options[:per_page] || 30).to_i
  fail Error::QueryError, ":per_page must be >= 1" if results.per_page < 1
  results.total_entries = 0
  all(options.merge(:limit => nil)) do |kv|
    if results.total_entries >= results.offset and
       results.size          <  results.per_page
      if mode == :hoh
        results[kv.first] = kv.last
      else
        results << kv
      end
    end
    results.total_entries += 1
  end
  results
end

#remove_index(column) ⇒ Object


229
230
231
232
233
234
# File 'lib/oklahoma_mixer/table_database.rb', line 229

def remove_index(column)
  try( :setindex,
       cast_to_bytes_and_length(column_name(column)).first,
       lib::INDEXES[:TDBITVOID],
       :no_error => {2 => false} )
end

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

Getting and Setting Keys ###


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oklahoma_mixer/table_database.rb', line 38

def store(key, value, mode = nil, &dup_handler)
  if mode == :add and dup_handler.nil?
    super
  elsif dup_handler
    warn "block supersedes mode argument" unless mode.nil?
    k        = cast_key_in(key)
    callback = lambda { |old_value_pointer, old_size, returned_size, _|
      old_value         = cast_from_null_terminated_colums(
                            *old_value_pointer.get_bytes(0, old_size)
                          )
      replacement, size = cast_to_null_terminated_colums( 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, cast_to_null_terminated_colums(value), callback, nil)
    value
  else
    Utilities.temp_map do |map|
      map.update(value) { |string|
        cast_to_bytes_and_length(string)
      }
      result = super(key, map, mode, &dup_handler)
      result == map ? value : result
    end
  end
end

#union(q, *queries, &iterator) ⇒ Object


195
196
197
# File 'lib/oklahoma_mixer/table_database.rb', line 195

def union(q, *queries, &iterator)
  search([q] + queries, lib::SEARCHES[:TDBMSUNION], &iterator)
end