Class: Oinky::Index

Inherits:
Object
  • Object
show all
Includes:
RowSet
Defined in:
lib/oinky.rb,
lib/oinky/query.rb

Overview

module RowSet

Defined Under Namespace

Classes: Cursor

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RowSet

#__each_filtered, #__make_filter_proc, #__rs_filter, #cursor_each, #each, #new_cursor

Constructor Details

#initialize(table, handle) ⇒ Index

Returns a new instance of Index.



1037
1038
1039
# File 'lib/oinky.rb', line 1037

def initialize(table, handle)
  @table, @handle = table, handle
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



1041
1042
1043
# File 'lib/oinky.rb', line 1041

def table
  @table
end

Instance Method Details

#column_countObject



1044
1045
1046
# File 'lib/oinky.rb', line 1044

def column_count
  Internal.oinky_idx_column_count(@handle)
end

#columnsObject



1073
1074
1075
1076
# File 'lib/oinky.rb', line 1073

def columns
  definition
  @colnames
end

#dbObject



1042
# File 'lib/oinky.rb', line 1042

def db ; @table.db ; end

#definitionObject



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/oinky.rb', line 1051

def definition
  return @definition if @definition

  sz = self.column_count()
  nm = FFI::MemoryPointer.new(Internal::DB_string)
  uniq = FFI::MemoryPointer.new(:char)
  defs = Internal::IndexColumnDefn::IxColumnSet.new(sz)
  Internal.wrap_oinky(Internal.oinky_index_get_definition(@handle, nm, uniq, defs, sz))

  # Unlike the table, ordering of the index column definitions is 
  # semantically important.
  @definition = { 
    :name=>Internal::DB_string.new(nm).to_s,
    :unique=>uniq.read_char != 0 ? true : false,
    :columns=> (0..sz-1).map{|i|
      defs[i].to_h
    }
  }
  @colnames = @definition[:columns].map{|c| c[:column_name] }
  return @definition
end

#filter(f) ⇒ Object

Specialize the rowset filter to add specification by array(position)



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/oinky/query.rb', line 151

def filter(f)
  if f.is_a? Array
    c = new_cursor().seek(f)
    rg = f
    cols = self.columns
    if f.size > cols.size
      raise ArgumentError.new("Position specification exceeds index width.")
    end
    # truncate our test width to what we were given
    cols = cols[0..f.size-1]
    # Use an array for indirection in the first parameter, so
    # the two lambdas can share state.
    result = [true]
    f = lambda {|v| 
      r = true
      cols.each_with_index{|cn,i|
        if rg[i].is_a? Proc
          r &&= rg[i].call(v[cn])
        else
          r &&= (v[cn] == rg[i])
        end
      }
      result[0] = r
      r
    }
    result[0] = c.is_valid? && f.call(c.select_all)
    # We stop on the first rejection
    done = lambda { |c| !result[0]}
  else
    c = new_cursor().seek_first()
    done = nil
  end
  e = __rs_filter(f,c,done)
  if block_given?
    e.each {|v| yield v}
    return self
  else
    return e
  end
end

#nameObject



1082
1083
1084
1085
1086
# File 'lib/oinky.rb', line 1082

def name
  return @idxname if @idxname
  dbs = Internal.oinky_index_name(@handle)
  @idxname = dbs.to_s
end

#row_countObject



1047
1048
1049
# File 'lib/oinky.rb', line 1047

def row_count
  Internal.oinky_idx_row_count(@handle)
end

#selectorObject



1078
1079
1080
# File 'lib/oinky.rb', line 1078

def selector
  table.select_columns(self.columns)
end