6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
|
# File 'lib/ok_hbase/concerns/indexable.rb', line 6
def use_index(index_name, opts={})
options = opts.with_indifferent_access
attributes = options[:attributes]
prefix_length = options[:prefix_length]
index_id = options[:index_id]
pack_pattern = options[:pack_pattern]
auto_create = options[:auto_create]
@@_indexes ||= {}
@@_indexes = @@_indexes.with_indifferent_access
@@_indexes[index_name] = options
define_singleton_method :indexes do
@@_indexes
end
define_singleton_method :encode_for_row_key do |value|
value = 1 if value.to_s.downcase == "true"
value = 0 if value.to_s.downcase == "false"
value = value.unpack('Q>').first if value.is_a?(String)
value
end
define_singleton_method :key_for_index do |index_name, data|
options = @@_indexes[index_name]
row = self.row_class.new(table: self, default_column_family: self.default_column_family, raw_data: data)
row_key_components = options[:attributes].map do |attribute|
value = if attribute == :index_id
options[:index_id]
else
row.attributes[attribute] || row.send(attribute)
end
encode_for_row_key(value)
end
row_key_components.pack(options[:pack_pattern].join(''))
end
define_singleton_method index_name do |idx_options, &block|
expected_option_keys = attributes[0...prefix_length]
prefix_pack_pattern = pack_pattern[0...prefix_length].join('')
prefix_components = expected_option_keys.map do |key|
value = key == :index_id ? index_id : idx_options[key]
encode_for_row_key(value)
end
row_prefix = prefix_components.pack(prefix_pack_pattern)
scan(row_prefix: row_prefix, &block)
end
end
|