cassandra-mavericks 0.21.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG +150 -0
- data/Gemfile +7 -0
- data/LICENSE +202 -0
- data/Manifest +94 -0
- data/README.md +373 -0
- data/Rakefile +191 -0
- data/bin/cassandra_helper +16 -0
- data/cassandra.gemspec +29 -0
- data/conf/0.6/cassandra.in.sh +47 -0
- data/conf/0.6/log4j.properties +38 -0
- data/conf/0.6/schema.json +57 -0
- data/conf/0.6/storage-conf.xml +352 -0
- data/conf/0.7/cassandra.in.sh +46 -0
- data/conf/0.7/cassandra.yaml +336 -0
- data/conf/0.7/log4j-server.properties +41 -0
- data/conf/0.7/schema.json +57 -0
- data/conf/0.7/schema.txt +45 -0
- data/conf/0.8/cassandra.in.sh +41 -0
- data/conf/0.8/cassandra.yaml +61 -0
- data/conf/0.8/log4j-server.properties +40 -0
- data/conf/0.8/schema.json +72 -0
- data/conf/0.8/schema.txt +57 -0
- data/conf/1.0/cassandra.in.sh +41 -0
- data/conf/1.0/cassandra.yaml +415 -0
- data/conf/1.0/log4j-server.properties +40 -0
- data/conf/1.0/schema.json +72 -0
- data/conf/1.0/schema.txt +57 -0
- data/conf/1.1/cassandra.in.sh +41 -0
- data/conf/1.1/cassandra.yaml +567 -0
- data/conf/1.1/log4j-server.properties +44 -0
- data/conf/1.1/schema.json +72 -0
- data/conf/1.1/schema.txt +57 -0
- data/conf/1.2/cassandra.in.sh +41 -0
- data/conf/1.2/cassandra.yaml +643 -0
- data/conf/1.2/log4j-server.properties +44 -0
- data/conf/1.2/schema.json +72 -0
- data/conf/1.2/schema.txt +57 -0
- data/ext/cassandra_native.c +69 -0
- data/ext/extconf.rb +9 -0
- data/lib/cassandra.rb +47 -0
- data/lib/cassandra/0.6.rb +7 -0
- data/lib/cassandra/0.6/cassandra.rb +113 -0
- data/lib/cassandra/0.6/columns.rb +78 -0
- data/lib/cassandra/0.6/protocol.rb +91 -0
- data/lib/cassandra/0.7.rb +7 -0
- data/lib/cassandra/0.7/cassandra.rb +2 -0
- data/lib/cassandra/0.7/columns.rb +4 -0
- data/lib/cassandra/0.7/protocol.rb +5 -0
- data/lib/cassandra/0.8.rb +7 -0
- data/lib/cassandra/0.8/cassandra.rb +25 -0
- data/lib/cassandra/0.8/columns.rb +28 -0
- data/lib/cassandra/0.8/protocol.rb +10 -0
- data/lib/cassandra/1.0.rb +7 -0
- data/lib/cassandra/1.0/cassandra.rb +1 -0
- data/lib/cassandra/1.0/columns.rb +1 -0
- data/lib/cassandra/1.0/protocol.rb +1 -0
- data/lib/cassandra/1.1.rb +7 -0
- data/lib/cassandra/1.1/cassandra.rb +1 -0
- data/lib/cassandra/1.1/columns.rb +1 -0
- data/lib/cassandra/1.1/protocol.rb +1 -0
- data/lib/cassandra/1.2.rb +7 -0
- data/lib/cassandra/1.2/cassandra.rb +1 -0
- data/lib/cassandra/1.2/columns.rb +1 -0
- data/lib/cassandra/1.2/protocol.rb +1 -0
- data/lib/cassandra/array.rb +8 -0
- data/lib/cassandra/batch.rb +41 -0
- data/lib/cassandra/cassandra.rb +1091 -0
- data/lib/cassandra/column_family.rb +3 -0
- data/lib/cassandra/columns.rb +172 -0
- data/lib/cassandra/comparable.rb +28 -0
- data/lib/cassandra/composite.rb +137 -0
- data/lib/cassandra/constants.rb +11 -0
- data/lib/cassandra/debug.rb +9 -0
- data/lib/cassandra/dynamic_composite.rb +118 -0
- data/lib/cassandra/helpers.rb +41 -0
- data/lib/cassandra/keyspace.rb +3 -0
- data/lib/cassandra/long.rb +58 -0
- data/lib/cassandra/mock.rb +536 -0
- data/lib/cassandra/ordered_hash.rb +195 -0
- data/lib/cassandra/protocol.rb +137 -0
- data/lib/cassandra/time.rb +11 -0
- data/test/cassandra_client_test.rb +20 -0
- data/test/cassandra_mock_test.rb +128 -0
- data/test/cassandra_test.rb +1367 -0
- data/test/comparable_types_test.rb +45 -0
- data/test/composite_type_test.rb +86 -0
- data/test/eventmachine_test.rb +42 -0
- data/test/ordered_hash_test.rb +386 -0
- data/test/test_helper.rb +19 -0
- data/vendor/0.6/gen-rb/cassandra.rb +1481 -0
- data/vendor/0.6/gen-rb/cassandra_constants.rb +12 -0
- data/vendor/0.6/gen-rb/cassandra_types.rb +482 -0
- data/vendor/0.7/gen-rb/cassandra.rb +1936 -0
- data/vendor/0.7/gen-rb/cassandra_constants.rb +12 -0
- data/vendor/0.7/gen-rb/cassandra_types.rb +681 -0
- data/vendor/0.8/gen-rb/cassandra.rb +2215 -0
- data/vendor/0.8/gen-rb/cassandra_constants.rb +12 -0
- data/vendor/0.8/gen-rb/cassandra_types.rb +824 -0
- data/vendor/1.0/gen-rb/cassandra.rb +2215 -0
- data/vendor/1.0/gen-rb/cassandra_constants.rb +12 -0
- data/vendor/1.0/gen-rb/cassandra_types.rb +857 -0
- data/vendor/1.1/gen-rb/cassandra.rb +2571 -0
- data/vendor/1.1/gen-rb/cassandra_constants.rb +12 -0
- data/vendor/1.1/gen-rb/cassandra_types.rb +928 -0
- data/vendor/1.2/gen-rb/cassandra.rb +3013 -0
- data/vendor/1.2/gen-rb/cassandra_constants.rb +13 -0
- data/vendor/1.2/gen-rb/cassandra_types.rb +965 -0
- metadata +288 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
class Cassandra
|
2
|
+
module Helpers
|
3
|
+
def extract_and_validate_params(column_family, keys, args, options)
|
4
|
+
options = options.dup
|
5
|
+
column_family = column_family.to_s
|
6
|
+
# Keys
|
7
|
+
[keys].flatten.each do |key|
|
8
|
+
raise ArgumentError, "Key #{key.inspect} must be a String for #{caller[2].inspect}." unless key.is_a?(String)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Options
|
12
|
+
if args.last.is_a?(Hash)
|
13
|
+
extras = args.last.keys - options.keys
|
14
|
+
raise ArgumentError, "Invalid options #{extras.inspect[1..-2]} for #{caller[1]}" if extras.any?
|
15
|
+
options.merge!(args.pop)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Ranges
|
19
|
+
column, sub_column = args[0], args[1]
|
20
|
+
raise ArgumentError, "Invalid arguments: subcolumns specified for a non-supercolumn family" if sub_column && !is_super(column_family)
|
21
|
+
klass, sub_klass = column_name_class(column_family), sub_column_name_class(column_family)
|
22
|
+
range_class = column ? sub_klass : klass
|
23
|
+
|
24
|
+
[:start, :finish].each do |opt|
|
25
|
+
options[opt] = options[opt] ? range_class.new(options[opt]).to_s : ''
|
26
|
+
end
|
27
|
+
|
28
|
+
[column_family, s_map(column, klass), s_map(sub_column, sub_klass), options]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convert stuff to strings.
|
32
|
+
def s_map(el, klass)
|
33
|
+
case el
|
34
|
+
when Array then el.map { |i| s_map(i, klass) }
|
35
|
+
when NilClass then nil
|
36
|
+
else
|
37
|
+
klass.new(el).to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
class Cassandra
|
3
|
+
# A temporally-ordered Long class for use in Cassandra column names
|
4
|
+
class Long < Comparable
|
5
|
+
|
6
|
+
# FIXME Should unify with or subclass Cassandra::UUID
|
7
|
+
def initialize(bytes = nil)
|
8
|
+
case bytes
|
9
|
+
when self.class # Long
|
10
|
+
@bytes = bytes.to_s
|
11
|
+
when String
|
12
|
+
case bytes.size
|
13
|
+
when 8 # Raw byte array
|
14
|
+
@bytes = bytes
|
15
|
+
when 18 # Human-readable UUID-like representation; inverse of #to_guid
|
16
|
+
elements = bytes.split("-")
|
17
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (malformed UUID-like representation)" if elements.size != 3
|
18
|
+
@bytes = [elements.join].pack('H32')
|
19
|
+
else
|
20
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (invalid bytecount)"
|
21
|
+
end
|
22
|
+
when Integer
|
23
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (integer out of range)" if bytes < 0 or bytes > 2**64
|
24
|
+
@bytes = [bytes >> 32, bytes % 2**32].pack("NN")
|
25
|
+
when NilClass, Time
|
26
|
+
# Time.stamp is 52 bytes, so we have 12 bytes of entropy left over
|
27
|
+
int = ((bytes || Time).stamp << 12) + rand(2**12)
|
28
|
+
@bytes = [int >> 32, int % 2**32].pack("NN")
|
29
|
+
else
|
30
|
+
raise TypeError, "Expected #{bytes.inspect} to cast to a #{self.class} (unknown source class)"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_i
|
35
|
+
@to_i ||= begin
|
36
|
+
ints = @bytes.unpack("NN")
|
37
|
+
(ints[0] << 32) +
|
38
|
+
ints[1]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_guid
|
43
|
+
"%08x-%04x-%04x" % @bytes.unpack("Nnn")
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
"<Cassandra::Long##{object_id} time: #{
|
48
|
+
Time.at((to_i >> 12) / 1_000_000).utc.inspect
|
49
|
+
}, usecs: #{
|
50
|
+
(to_i >> 12) % 1_000_000
|
51
|
+
}, jitter: #{
|
52
|
+
to_i % 2**12
|
53
|
+
}, guid: #{
|
54
|
+
to_guid
|
55
|
+
}>"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,536 @@
|
|
1
|
+
class SimpleUUID::UUID
|
2
|
+
def >=(other)
|
3
|
+
(self <=> other) >= 0
|
4
|
+
end
|
5
|
+
|
6
|
+
def <=(other)
|
7
|
+
(self <=> other) <= 0
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Cassandra
|
12
|
+
class Mock
|
13
|
+
include ::Cassandra::Helpers
|
14
|
+
include ::Cassandra::Columns
|
15
|
+
|
16
|
+
attr_reader :keyspace
|
17
|
+
|
18
|
+
def initialize(keyspace, schema)
|
19
|
+
@is_super = {}
|
20
|
+
@keyspace = keyspace
|
21
|
+
@column_name_class = {}
|
22
|
+
@sub_column_name_class = {}
|
23
|
+
@column_name_maker = {}
|
24
|
+
@sub_column_name_maker = {}
|
25
|
+
@indexes = {}
|
26
|
+
@schema = schema[keyspace]
|
27
|
+
clear_keyspace!
|
28
|
+
end
|
29
|
+
|
30
|
+
def disconnect!
|
31
|
+
end
|
32
|
+
|
33
|
+
def clear_keyspace!
|
34
|
+
@data = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear_column_family!(column_family)
|
38
|
+
@data[column_family.to_sym] = OrderedHash.new
|
39
|
+
end
|
40
|
+
|
41
|
+
def default_write_consistency=(value)
|
42
|
+
WRITE_DEFAULTS[:consistency] = value
|
43
|
+
end
|
44
|
+
|
45
|
+
def default_read_consistency=(value)
|
46
|
+
READ_DEFAULTS[:consistency] = value
|
47
|
+
end
|
48
|
+
|
49
|
+
def insert(column_family, key, hash_or_array, options = {})
|
50
|
+
if @batch
|
51
|
+
@batch << [:insert, column_family, key, hash_or_array, options]
|
52
|
+
else
|
53
|
+
raise ArgumentError if key.nil?
|
54
|
+
if !is_super(column_family)
|
55
|
+
insert_standard(column_family, key, hash_or_array)
|
56
|
+
else
|
57
|
+
insert_super(column_family, key, hash_or_array)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def insert_standard(column_family, key, hash_or_array)
|
63
|
+
old = cf(column_family)[key] || OrderedHash.new
|
64
|
+
cf(column_family)[key] = merge_and_sort(old, hash_or_array)
|
65
|
+
end
|
66
|
+
|
67
|
+
def insert_super(column_family, key, hash)
|
68
|
+
raise ArgumentError unless hash.is_a?(Hash)
|
69
|
+
cf(column_family)[key] ||= OrderedHash.new
|
70
|
+
|
71
|
+
hash.keys.each do |sub_key|
|
72
|
+
old = cf(column_family)[key][sub_key] || OrderedHash.new
|
73
|
+
cf(column_family)[key][sub_key] = merge_and_sort(old, hash[sub_key])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def batch(options={})
|
78
|
+
@batch = Cassandra::Batch.new(self, options)
|
79
|
+
yield
|
80
|
+
flush_batch(options)
|
81
|
+
ensure
|
82
|
+
@batch = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def flush_batch(options)
|
86
|
+
b = @batch
|
87
|
+
@batch = nil
|
88
|
+
b.each do |mutation|
|
89
|
+
send(*mutation)
|
90
|
+
end
|
91
|
+
@batch = b
|
92
|
+
end
|
93
|
+
|
94
|
+
def get(column_family, key, *columns_and_options)
|
95
|
+
column_family, column, sub_column, options =
|
96
|
+
extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
|
97
|
+
if !is_super(column_family)
|
98
|
+
get_standard(column_family, key, column, options)
|
99
|
+
else
|
100
|
+
get_super(column_family, key, column, sub_column, options)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_standard(column_family, key, column, options)
|
105
|
+
columns = cf(column_family)[key] || OrderedHash.new
|
106
|
+
row = columns_to_hash(column_family, columns)
|
107
|
+
|
108
|
+
if column
|
109
|
+
row[column]
|
110
|
+
else
|
111
|
+
row = apply_range(row, column_family, options[:start], options[:finish])
|
112
|
+
row = apply_count(row, options[:count], options[:reversed])
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def get_super(column_family, key, column, sub_column, options)
|
117
|
+
columns = cf(column_family)[key] || OrderedHash.new
|
118
|
+
row = columns_to_hash(column_family, columns)
|
119
|
+
|
120
|
+
if column
|
121
|
+
if sub_column
|
122
|
+
if row[column] &&
|
123
|
+
row[column][sub_column]
|
124
|
+
row[column][sub_column]
|
125
|
+
else
|
126
|
+
nil
|
127
|
+
end
|
128
|
+
else
|
129
|
+
row = row[column] || OrderedHash.new
|
130
|
+
row = apply_range(row, column_family, options[:start], options[:finish], false)
|
131
|
+
row = apply_count(row, options[:count], options[:reversed])
|
132
|
+
end
|
133
|
+
else
|
134
|
+
row
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def exists?(column_family, key, *columns_and_options)
|
139
|
+
column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, [key], columns_and_options, READ_DEFAULTS)
|
140
|
+
results = get(column_family, key, column, sub_column)
|
141
|
+
|
142
|
+
![{}, nil].include?(results)
|
143
|
+
end
|
144
|
+
|
145
|
+
def multi_get(column_family, keys, *columns_and_options)
|
146
|
+
column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, keys, columns_and_options, READ_DEFAULTS)
|
147
|
+
keys.inject(OrderedHash.new) do |hash, key|
|
148
|
+
hash[key] = get(column_family, key)
|
149
|
+
hash
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def remove(column_family, key, *columns_and_options)
|
154
|
+
column_family, columns, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)
|
155
|
+
|
156
|
+
if @batch
|
157
|
+
@batch << [:remove, column_family, key, columns, sub_column]
|
158
|
+
else
|
159
|
+
if columns
|
160
|
+
if sub_column
|
161
|
+
if columns.is_a? Array
|
162
|
+
raise ArgumentError, 'remove does not support sub_columns with array of columns'
|
163
|
+
end
|
164
|
+
|
165
|
+
if cf(column_family)[key][columns]
|
166
|
+
cf(column_family)[key][columns].delete(sub_column.to_s)
|
167
|
+
end
|
168
|
+
else
|
169
|
+
if cf(column_family)[key]
|
170
|
+
Array(columns).each do |column|
|
171
|
+
cf(column_family)[key].delete(column.to_s)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
else
|
176
|
+
cf(column_family).delete(key)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def get_columns(column_family, key, *columns_and_options)
|
182
|
+
column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)
|
183
|
+
d = get(column_family, key)
|
184
|
+
|
185
|
+
if sub_columns
|
186
|
+
sub_columns.collect do |sub_column|
|
187
|
+
d[columns][sub_column]
|
188
|
+
end
|
189
|
+
else
|
190
|
+
columns.collect do |column|
|
191
|
+
d[column]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def count_columns(column_family, key, *columns_and_options)
|
197
|
+
column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)
|
198
|
+
|
199
|
+
get(column_family, key, columns, options).keys.length
|
200
|
+
end
|
201
|
+
|
202
|
+
def multi_get_columns(column_family, keys, columns)
|
203
|
+
keys.inject(OrderedHash.new) do |hash, key|
|
204
|
+
hash[key] = get_columns(column_family, key, columns)
|
205
|
+
hash
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
def multi_count_columns(column_family, keys)
|
210
|
+
keys.inject(OrderedHash.new) do |hash, key|
|
211
|
+
hash[key] = count_columns(column_family, key)
|
212
|
+
hash
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
def get_range(column_family, options = {}, &blk)
|
217
|
+
column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options],
|
218
|
+
READ_DEFAULTS.merge(:start_key => nil,
|
219
|
+
:finish_key => nil,
|
220
|
+
:key_count => 100,
|
221
|
+
:columns => nil,
|
222
|
+
:reversed => false
|
223
|
+
)
|
224
|
+
)
|
225
|
+
res = _get_range(column_family,
|
226
|
+
options[:start_key],
|
227
|
+
options[:finish_key],
|
228
|
+
options[:key_count],
|
229
|
+
options[:columns],
|
230
|
+
options[:start],
|
231
|
+
options[:finish],
|
232
|
+
options[:count],
|
233
|
+
options[:consistency],
|
234
|
+
options[:reversed], &blk)
|
235
|
+
|
236
|
+
if blk.nil?
|
237
|
+
res
|
238
|
+
else
|
239
|
+
nil
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def get_range_keys(column_family, options = {})
|
244
|
+
get_range(column_family,options.merge!(:columns => [])).keys
|
245
|
+
end
|
246
|
+
|
247
|
+
def count_range(column_family, options = {})
|
248
|
+
Hash[get_range(column_family, options).select{|k,v| v.length > 0}].keys.compact.length
|
249
|
+
end
|
250
|
+
|
251
|
+
def each_key(column_family, options = {})
|
252
|
+
each(column_family, options.merge!(:columns => [])) do |key, value|
|
253
|
+
yield key
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
def each(column_family, options = {})
|
258
|
+
batch_size = options.delete(:batch_size) || 100
|
259
|
+
count = options.delete(:key_count)
|
260
|
+
yielded_count = 0
|
261
|
+
|
262
|
+
options[:start_key] ||= ''
|
263
|
+
last_key = nil
|
264
|
+
|
265
|
+
while options[:start_key] != last_key && (count.nil? || count > yielded_count)
|
266
|
+
options[:start_key] = last_key
|
267
|
+
res = get_range(column_family, options.merge!(:start_key => last_key, :key_count => batch_size))
|
268
|
+
res.each do |key, columns|
|
269
|
+
next if options[:start_key] == key
|
270
|
+
next if yielded_count == count
|
271
|
+
yield key, columns
|
272
|
+
yielded_count += 1
|
273
|
+
last_key = key
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
def create_index(ks_name, cf_name, c_name, v_class)
|
279
|
+
if @indexes[ks_name] &&
|
280
|
+
@indexes[ks_name][cf_name] &&
|
281
|
+
@indexes[ks_name][cf_name][c_name]
|
282
|
+
nil
|
283
|
+
|
284
|
+
else
|
285
|
+
@indexes[ks_name] ||= {}
|
286
|
+
@indexes[ks_name][cf_name] ||= {}
|
287
|
+
@indexes[ks_name][cf_name][c_name] = true
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
def drop_index(ks_name, cf_name, c_name)
|
292
|
+
if @indexes[ks_name] &&
|
293
|
+
@indexes[ks_name][cf_name] &&
|
294
|
+
@indexes[ks_name][cf_name][c_name]
|
295
|
+
|
296
|
+
@indexes[ks_name][cf_name].delete(c_name)
|
297
|
+
else
|
298
|
+
nil
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
def create_index_expression(c_name, value, op)
|
303
|
+
{:column_name => c_name, :value => value, :comparison => op}
|
304
|
+
end
|
305
|
+
alias :create_idx_expr :create_index_expression
|
306
|
+
|
307
|
+
def create_index_clause(idx_expressions, start = "", count = 100)
|
308
|
+
{:start => start, :index_expressions => idx_expressions, :count => count, :type => :index_clause}
|
309
|
+
end
|
310
|
+
alias :create_idx_clause :create_index_clause
|
311
|
+
|
312
|
+
def get_indexed_slices(column_family, idx_clause, *columns_and_options)
|
313
|
+
column_family, columns, _, options =
|
314
|
+
extract_and_validate_params_for_real(column_family, [], columns_and_options,
|
315
|
+
READ_DEFAULTS.merge(:key_count => 100, :start_key => nil, :key_start => nil))
|
316
|
+
|
317
|
+
start_key = options[:start_key] || options[:key_start] || ""
|
318
|
+
|
319
|
+
unless [Hash, OrderedHash].include?(idx_clause.class) && idx_clause[:type] == :index_clause
|
320
|
+
idx_clause = create_index_clause(idx_clause, start_key, options[:key_count])
|
321
|
+
end
|
322
|
+
|
323
|
+
ret = OrderedHash.new
|
324
|
+
cf(column_family).each do |key, row|
|
325
|
+
next if idx_clause[:start] != '' && key < idx_clause[:start]
|
326
|
+
next if ret.length == idx_clause[:count]
|
327
|
+
|
328
|
+
matches = []
|
329
|
+
idx_clause[:index_expressions].each do |expr|
|
330
|
+
next if row[expr[:column_name]].nil?
|
331
|
+
next unless row[expr[:column_name]].send(expr[:comparison].to_sym, expr[:value])
|
332
|
+
|
333
|
+
matches << expr
|
334
|
+
end
|
335
|
+
|
336
|
+
ret[key] = row if matches.length == idx_clause[:index_expressions].length
|
337
|
+
end
|
338
|
+
|
339
|
+
ret
|
340
|
+
end
|
341
|
+
|
342
|
+
def add(column_family, key, value, *columns_and_options)
|
343
|
+
if @batch
|
344
|
+
@batch << [:add, column_family, key, value, *columns_and_options]
|
345
|
+
else
|
346
|
+
column_family, column, sub_column, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, WRITE_DEFAULTS)
|
347
|
+
|
348
|
+
if is_super(column_family)
|
349
|
+
cf(column_family)[key] ||= OrderedHash.new
|
350
|
+
cf(column_family)[key][column] ||= OrderedHash.new
|
351
|
+
cf(column_family)[key][column][sub_column] ||= 0
|
352
|
+
cf(column_family)[key][column][sub_column] += value
|
353
|
+
else
|
354
|
+
cf(column_family)[key] ||= OrderedHash.new
|
355
|
+
cf(column_family)[key][column] ||= 0
|
356
|
+
cf(column_family)[key][column] += value
|
357
|
+
end
|
358
|
+
|
359
|
+
nil
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
def column_families
|
364
|
+
cf_defs = {}
|
365
|
+
schema.each do |key, value|
|
366
|
+
cf_def = Cassandra::ColumnFamily.new
|
367
|
+
|
368
|
+
value.each do |property, property_value|
|
369
|
+
cf_def.send(:"#{property}=", property_value)
|
370
|
+
end
|
371
|
+
|
372
|
+
cf_defs[key] = cf_def
|
373
|
+
end
|
374
|
+
|
375
|
+
cf_defs
|
376
|
+
end
|
377
|
+
|
378
|
+
def schema(load=true)
|
379
|
+
@schema
|
380
|
+
end
|
381
|
+
|
382
|
+
def column_family_property(column_family, key)
|
383
|
+
schema[column_family.to_s][key]
|
384
|
+
end
|
385
|
+
|
386
|
+
def add_column_family(cf)
|
387
|
+
@schema[cf.name.to_s] ||= OrderedHash.new
|
388
|
+
|
389
|
+
cf.instance_variables.each do |var|
|
390
|
+
@schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def update_column_family(cf)
|
395
|
+
return false unless @schema.include?(cf.name.to_s)
|
396
|
+
|
397
|
+
cf.instance_variables.each do |var|
|
398
|
+
@schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
def drop_column_family(column_family_name)
|
403
|
+
@schema.delete(column_family_name)
|
404
|
+
end
|
405
|
+
|
406
|
+
private
|
407
|
+
|
408
|
+
def schema_for_keyspace(keyspace)
|
409
|
+
@schema
|
410
|
+
end
|
411
|
+
|
412
|
+
def _get_range(column_family, start_key, finish_key, key_count, columns, start, finish, count, consistency, reversed, &blk)
|
413
|
+
ret = OrderedHash.new
|
414
|
+
start = to_compare_with_type(start, column_family)
|
415
|
+
finish = to_compare_with_type(finish, column_family)
|
416
|
+
cf(column_family).keys.sort.each do |key|
|
417
|
+
break if ret.keys.size >= key_count
|
418
|
+
if (start_key.nil? || key >= start_key) && (finish_key.nil? || key <= finish_key)
|
419
|
+
if columns
|
420
|
+
#ret[key] = columns.inject(OrderedHash.new){|hash, column_name| hash[column_name] = cf(column_family)[key][column_name]; hash;}
|
421
|
+
selected_hash = OrderedHash.new
|
422
|
+
cf(column_family)[key].each do |k, v|
|
423
|
+
selected_hash.[]=(k, v, cf(column_family)[key].timestamps[k]) if columns.include?(k)
|
424
|
+
end
|
425
|
+
ret[key] = columns_to_hash(column_family, selected_hash)
|
426
|
+
ret[key] = apply_count(ret[key], count, reversed)
|
427
|
+
blk.call(key,ret[key]) unless blk.nil?
|
428
|
+
else
|
429
|
+
#ret[key] = apply_range(cf(column_family)[key], column_family, start, finish, !is_super(column_family))
|
430
|
+
start, finish = finish, start if reversed
|
431
|
+
ret[key] = apply_range(columns_to_hash(column_family, cf(column_family)[key]), column_family, start, finish)
|
432
|
+
ret[key] = apply_count(ret[key], count, reversed)
|
433
|
+
blk.call(key,ret[key]) unless blk.nil?
|
434
|
+
end
|
435
|
+
end
|
436
|
+
end
|
437
|
+
ret
|
438
|
+
end
|
439
|
+
|
440
|
+
def extract_and_validate_params_for_real(column_family, keys, args, options)
|
441
|
+
column_family, columns, sub_column, options = extract_and_validate_params(column_family, keys, args, options)
|
442
|
+
options[:start] = nil if options[:start] == ''
|
443
|
+
options[:finish] = nil if options[:finish] == ''
|
444
|
+
[column_family, to_compare_with_types(columns, column_family), to_compare_with_types(sub_column, column_family, false), options]
|
445
|
+
end
|
446
|
+
|
447
|
+
def to_compare_with_types(column_names, column_family, standard=true)
|
448
|
+
if column_names.is_a?(Array)
|
449
|
+
column_names.collect do |name|
|
450
|
+
to_compare_with_type(name, column_family, standard)
|
451
|
+
end
|
452
|
+
else
|
453
|
+
to_compare_with_type(column_names, column_family, standard)
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
def to_compare_with_type(column_name, column_family, standard=true)
|
458
|
+
return column_name if column_name.nil?
|
459
|
+
klass = if standard
|
460
|
+
column_name_class(column_family)
|
461
|
+
else
|
462
|
+
sub_column_name_class(column_family)
|
463
|
+
end
|
464
|
+
|
465
|
+
klass.new(column_name)
|
466
|
+
end
|
467
|
+
|
468
|
+
def cf(column_family)
|
469
|
+
@data[column_family.to_sym] ||= OrderedHash.new
|
470
|
+
end
|
471
|
+
|
472
|
+
def merge_and_sort(old_stuff, new_stuff)
|
473
|
+
if new_stuff.is_a?(Array)
|
474
|
+
new_stuff = new_stuff.inject({}){|h,k| h[k] = nil; h }
|
475
|
+
end
|
476
|
+
|
477
|
+
new_stuff = new_stuff.to_a.inject({}){|h,k| h[k[0].to_s] = k[1]; h }
|
478
|
+
|
479
|
+
new_stuff.each { |k,v| old_stuff.[]=(k, v, (Time.now.to_f * 1000000).to_i) }
|
480
|
+
hash = OrderedHash.new
|
481
|
+
old_stuff.sort{ |a,b| a[0] <=> b[0] }.each do |k, v|
|
482
|
+
hash.[]=(k, v, old_stuff.timestamps[k])
|
483
|
+
end
|
484
|
+
hash
|
485
|
+
end
|
486
|
+
|
487
|
+
def columns_to_hash(column_family, columns)
|
488
|
+
column_class, sub_column_class = column_name_class(column_family), sub_column_name_class(column_family)
|
489
|
+
output = OrderedHash.new
|
490
|
+
|
491
|
+
columns.each do |column_name, value|
|
492
|
+
timestamp = columns.timestamps[column_name]
|
493
|
+
column = column_class.new(column_name)
|
494
|
+
|
495
|
+
if [Hash, OrderedHash].include?(value.class)
|
496
|
+
output[column] ||= OrderedHash.new
|
497
|
+
value.each do |sub_column, sub_column_value|
|
498
|
+
timestamp = value.timestamps[sub_column]
|
499
|
+
output[column].[]=(sub_column_class.new(sub_column), sub_column_value, timestamp)
|
500
|
+
end
|
501
|
+
else
|
502
|
+
output.[]=(column_class.new(column_name), value, timestamp)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
output
|
507
|
+
end
|
508
|
+
|
509
|
+
def apply_count(row, count, reversed=false)
|
510
|
+
if count
|
511
|
+
keys = row.keys.sort
|
512
|
+
keys = keys.reverse if reversed
|
513
|
+
keys = keys[0...count]
|
514
|
+
keys.inject(OrderedHash.new) do |memo, key|
|
515
|
+
memo.[]=(key, row[key], row.timestamps[key])
|
516
|
+
memo
|
517
|
+
end
|
518
|
+
else
|
519
|
+
row
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
def apply_range(row, column_family, strt, fin, standard=true)
|
524
|
+
start = to_compare_with_type(strt, column_family, standard)
|
525
|
+
finish = to_compare_with_type(fin, column_family, standard)
|
526
|
+
ret = OrderedHash.new
|
527
|
+
row.keys.each do |key|
|
528
|
+
if (start.nil? || key >= start) && (finish.nil? || key <= finish)
|
529
|
+
ret.[]=(key, row[key], row.timestamps[key])
|
530
|
+
end
|
531
|
+
end
|
532
|
+
ret
|
533
|
+
end
|
534
|
+
|
535
|
+
end
|
536
|
+
end
|