Class: Ignite::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/ignite/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name) ⇒ Cache

Returns a new instance of Cache.



5
6
7
8
9
# File 'lib/ignite/cache.rb', line 5

def initialize(client, name)
  @client = client
  @name = name
  @cache_id = hash_code(name)
end

Instance Attribute Details

#cache_idObject (readonly)

Returns the value of attribute cache_id.



3
4
5
# File 'lib/ignite/cache.rb', line 3

def cache_id
  @cache_id
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/ignite/cache.rb', line 3

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ignite/cache.rb', line 3

def name
  @name
end

Instance Method Details

#clearObject



156
157
158
159
160
161
162
163
# File 'lib/ignite/cache.rb', line 156

def clear
  req = Request.new(OP_CACHE_CLEAR)
  req.int cache_id
  req.byte 0

  client.send_request(req)
  nil
end

#clear_key(key) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/ignite/cache.rb', line 165

def clear_key(key)
  req = Request.new(OP_CACHE_CLEAR_KEY)
  req.int cache_id
  req.byte 0
  req.data_object key

  client.send_request(req)
  nil
end

#clear_keys(keys) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ignite/cache.rb', line 175

def clear_keys(keys)
  req = Request.new(OP_CACHE_CLEAR_KEYS)
  req.int cache_id
  req.byte 0
  req.int keys.size
  keys.each do |key|
    req.data_object key
  end

  client.send_request(req)
  nil
end

#destroyObject



287
288
289
290
291
292
# File 'lib/ignite/cache.rb', line 287

def destroy
  req = Request.new(OP_CACHE_DESTROY)
  req.int cache_id
  client.send_request(req)
  nil
end

#get(key) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/ignite/cache.rb', line 11

def get(key)
  req = Request.new(OP_CACHE_GET)
  req.int cache_id
  req.byte 0
  req.data_object key

  res = client.send_request(req)
  res.read_data_object
end

#get_all(keys) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ignite/cache.rb', line 21

def get_all(keys)
  req = Request.new(OP_CACHE_GET_ALL)
  req.int cache_id
  req.byte 0
  req.int keys.size
  keys.each do |key|
    req.data_object key
  end

  res = client.send_request(req)
  result = {}
  res.read_int.times do
    result[res.read_data_object] = res.read_data_object
  end
  result
end

#get_and_put(key, value) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/ignite/cache.rb', line 86

def get_and_put(key, value)
  req = Request.new(OP_CACHE_GET_AND_PUT)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req).read_data_object
end

#get_and_put_if_absent(key, value) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/ignite/cache.rb', line 125

def get_and_put_if_absent(key, value)
  req = Request.new(OP_CACHE_GET_AND_PUT_IF_ABSENT)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req).read_data_object
end

#get_and_remove(key) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/ignite/cache.rb', line 106

def get_and_remove(key)
  req = Request.new(OP_CACHE_GET_AND_REMOVE)
  req.int cache_id
  req.byte 0
  req.data_object key

  client.send_request(req).read_data_object
end

#get_and_replace(key, value) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ignite/cache.rb', line 96

def get_and_replace(key, value)
  req = Request.new(OP_CACHE_GET_AND_REPLACE)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req).read_data_object
end

#get_or_createObject



280
281
282
283
284
285
# File 'lib/ignite/cache.rb', line 280

def get_or_create
  req = Request.new(OP_CACHE_GET_OR_CREATE_WITH_NAME)
  req.string name
  client.send_request(req)
  self
end

#key?(key) ⇒ Boolean Also known as: contains_key

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/ignite/cache.rb', line 63

def key?(key)
  req = Request.new(OP_CACHE_CONTAINS_KEY)
  req.int cache_id
  req.byte 0
  req.data_object key

  client.send_request(req).read_bool
end

#keys?(keys) ⇒ Boolean Also known as: contains_keys

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ignite/cache.rb', line 73

def keys?(keys)
  req = Request.new(OP_CACHE_CONTAINS_KEYS)
  req.int cache_id
  req.byte 0
  req.int keys.size
  keys.each do |key|
    req.data_object key
  end

  client.send_request(req).read_bool
end

#put(key, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ignite/cache.rb', line 38

def put(key, value)
  req = Request.new(OP_CACHE_PUT)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req)
  nil
end

#put_all(objects) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ignite/cache.rb', line 49

def put_all(objects)
  req = Request.new(OP_CACHE_PUT_ALL)
  req.int cache_id
  req.byte 0
  req.int objects.size
  objects.each do |key, value|
    req.data_object key
    req.data_object value
  end

  client.send_request(req)
  nil
end

#put_if_absent(key, value) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/ignite/cache.rb', line 115

def put_if_absent(key, value)
  req = Request.new(OP_CACHE_PUT_IF_ABSENT)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req).read_bool
end

#remove_allObject



232
233
234
235
236
237
238
239
# File 'lib/ignite/cache.rb', line 232

def remove_all
  req = Request.new(OP_CACHE_REMOVE_ALL)
  req.int cache_id
  req.byte 0

  client.send_request(req)
  nil
end

#remove_if_equals(key, compare) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/ignite/cache.rb', line 197

def remove_if_equals(key, compare)
  req = Request.new(OP_CACHE_REMOVE_IF_EQUALS)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object compare

  client.send_request(req).read_bool
end

#remove_key(key) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/ignite/cache.rb', line 188

def remove_key(key)
  req = Request.new(OP_CACHE_REMOVE_KEY)
  req.int cache_id
  req.byte 0
  req.data_object key

  client.send_request(req).read_bool
end

#remove_keys(keys) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ignite/cache.rb', line 219

def remove_keys(keys)
  req = Request.new(OP_CACHE_REMOVE_KEYS)
  req.int cache_id
  req.byte 0
  req.int keys.size
  keys.each do |key|
    req.data_object key
  end

  client.send_request(req)
  nil
end

#replace(key, value) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/ignite/cache.rb', line 135

def replace(key, value)
  req = Request.new(OP_CACHE_REPLACE)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object value

  client.send_request(req).read_bool
end

#replace_if_equals(key, compare, value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/ignite/cache.rb', line 145

def replace_if_equals(key, compare, value)
  req = Request.new(OP_CACHE_REPLACE_IF_EQUALS)
  req.int cache_id
  req.byte 0
  req.data_object key
  req.data_object compare
  req.data_object value

  client.send_request(req).read_bool
end

#scan(page_size: 1000) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ignite/cache.rb', line 241

def scan(page_size: 1000)
  return to_enum(:scan, page_size: page_size) unless block_given?

  # TODO filter
  filter = nil

  req = Request.new(OP_QUERY_SCAN)
  req.int cache_id
  req.byte 0
  req.data_object filter
  req.byte 0 unless filter.nil?
  req.int page_size
  req.int(-1)
  req.bool false

  res = client.send_request(req)
  cursor_id = res.read_long
  row_count = res.read_int
  row_count.times do
    yield res.read_data_object, res.read_data_object
  end
  more_results = res.read_bool

  while more_results
    req = Request.new(OP_QUERY_SCAN_CURSOR_GET_PAGE)
    req.long cursor_id

    # docs for OP_QUERY_SCAN_CURSOR_GET_PAGE response are incorrect
    # 1. no cursor_id
    # 2. row_count is int, not log
    res = client.send_request(req)
    row_count = res.read_int
    row_count.times do
      yield res.read_data_object, res.read_data_object
    end
    more_results = res.read_bool
  end
end

#sizeObject Also known as: get_size

TODO add arguments



208
209
210
211
212
213
214
215
216
# File 'lib/ignite/cache.rb', line 208

def size
  req = Request.new(OP_CACHE_GET_SIZE)
  req.int cache_id
  req.byte 0
  req.int 0
  req.byte 0

  client.send_request(req).read_long
end