Class: STRC

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

Constant Summary collapse

Exception =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initializeSTRC

Returns a new instance of STRC.



6
7
8
# File 'lib/strc.rb', line 6

def initialize
	@dict = {}
end

Instance Method Details

#append(key, value) ⇒ Object

Append a value to a key



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/strc.rb', line 31

def append(key, value)
	if exists(key)
		item = get(key)
		if item.class == String and value.class == String
			set(key, item + value)
		else
			raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
		end
	else
		set(key, value)
	end
	return get(key).length
end

#decr(key) ⇒ Object

Decrement an integer



46
47
48
# File 'lib/strc.rb', line 46

def decr(key)
	decrby(key, 1)
end

#decrby(key, decrement) ⇒ Object

Decrement an integer by a certain amount



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/strc.rb', line 51

def decrby(key, decrement)
	unless exists(key)
		set(key, 0)
	end
	value = get(key)
	if value.class == Fixnum
		set(key, value - decrement)
	else
		raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
	end
	get(key)
end

#del(*keys) ⇒ Object

Delete a key



86
87
88
89
90
91
# File 'lib/strc.rb', line 86

def del(*keys)
	keys.each do |key|
		@dict.delete(key)
	end
	return keys.length
end

#exists(key) ⇒ Object

Returns if key exists



94
95
96
# File 'lib/strc.rb', line 94

def exists(key)
	@dict.has_key?(key)
end

#get(key) ⇒ Object

Get the value for the given key



26
27
28
# File 'lib/strc.rb', line 26

def get(key)
	@dict[key]
end

#hdel(key, *fields) ⇒ Object

Deletes fields from hash



399
400
401
402
403
404
405
406
407
408
409
# File 'lib/strc.rb', line 399

def hdel(key, *fields)
	hash = get_h(key)
	deleted = 0
	fields.each do |field|
		unless hash.delete(field).nil?
			deleted += 1
		end
	end
	set(key, hash)
	return deleted
end

#hexists(key, field) ⇒ Object

Returns whether or not the field exists in key



412
413
414
# File 'lib/strc.rb', line 412

def hexists(key, field)
	get_h(key).has_key?(field)
end

#hget(key, field) ⇒ Object

Get value at field in hash at key



394
395
396
# File 'lib/strc.rb', line 394

def hget(key, field)
	get_h(key)[field]
end

#hgetall(key) ⇒ Object

Returns an array of all fields and values in hash



417
418
419
# File 'lib/strc.rb', line 417

def hgetall(key)
	get_h(key).flatten
end

#hkeys(key) ⇒ Object

Returns array of all keys in hash at key



422
423
424
# File 'lib/strc.rb', line 422

def hkeys(key)
	get_h(key).keys
end

#hlen(key) ⇒ Object

Returns number of fields in the hash



432
433
434
# File 'lib/strc.rb', line 432

def hlen(key)
	get_h(key).length
end

#hset(key, field, value) ⇒ Object

Set file in hash stored at key to value.



378
379
380
381
382
# File 'lib/strc.rb', line 378

def hset(key, field, value)
	hash = get_h(key)
	hash[field] = value
	set(key, hash)
end

#hsetnx(key, field, value) ⇒ Object

Sets field in key only if it doesn’t exist



385
386
387
388
389
390
391
# File 'lib/strc.rb', line 385

def hsetnx(key, field, value)
	unless hexists(key, field)
		hset(key, field, value)
		return true
	end
	return false
end

#hvals(key) ⇒ Object

Returns array of all values in hash at key



427
428
429
# File 'lib/strc.rb', line 427

def hvals(key)
	get_h(key).values
end

#incr(key) ⇒ Object

Increment an integer



65
66
67
# File 'lib/strc.rb', line 65

def incr(key)
	incrby(key, 1)
end

#incrby(key, increment) ⇒ Object

Increment an integer by a certain amount



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/strc.rb', line 70

def incrby(key, increment)
	unless exists(key)
		set(key, 0)
	end
	value = get(key)
	if value.class == Fixnum
		set(key, value + increment)
	else
		raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
	end
	get(key)
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index



339
340
341
# File 'lib/strc.rb', line 339

def lindex(key, index)
	get_l(key)[index]
end

#llen(key) ⇒ Object

Returns length of list



288
289
290
# File 'lib/strc.rb', line 288

def llen(key)
	get_l(key).length
end

#lpop(key) ⇒ Object

Remove and get the first element in a list



331
332
333
334
335
336
# File 'lib/strc.rb', line 331

def lpop(key)
	list = get_l(key)
	element = list.shift
	set(key, list)
	return element
end

#lpush(key, *values) ⇒ Object

Prepend values to a list



316
317
318
319
320
321
# File 'lib/strc.rb', line 316

def lpush(key, *values)
	list = get_l(key)
	list = values + list
	set(key, list)
	return list.length
end

#lpushx(key, *values) ⇒ Object

LPUSH if the list exists



324
325
326
327
328
# File 'lib/strc.rb', line 324

def lpushx(key, *values)
	if exists(key)
		lpush(key, *values)
	end
end

#lrange(key, start, stop) ⇒ Object

Returns elements from key in the given range



282
283
284
285
# File 'lib/strc.rb', line 282

def lrange(key, start, stop)
	list = get_l(key)[start..stop]
	return list.nil? ? [] : list
end

#lset(key, index, value) ⇒ Object

Set value for an element at index in a list



344
345
346
347
348
# File 'lib/strc.rb', line 344

def lset(key, index, value)
	list = get_l(key)
	list[index] = value
	set(key, list)
end

#ltrim(key, start, stop) ⇒ Object

Trim a list to the specified range



351
352
353
# File 'lib/strc.rb', line 351

def ltrim(key, start, stop)
	set(key, lrange(key, start, stop))
end

#randomkeyObject

Returns a random key



99
100
101
# File 'lib/strc.rb', line 99

def randomkey
	@dict.keys.sample
end

#rename(key, newkey) ⇒ Object

Renames a key. Overwrites destination.



104
105
106
107
108
109
110
111
# File 'lib/strc.rb', line 104

def rename(key, newkey)
	if key == newkey
		raise STRC::Exception.new "ERR Source and destination objects are the same"
	end
	@dict[newkey] = @dict[key]
	del(key)
	return "OK"
end

#renamenx(key, newkey) ⇒ Object

Renames a key. Does not overwrite destination.



114
115
116
117
118
119
120
121
# File 'lib/strc.rb', line 114

def renamenx(key, newkey)
	if exists(newkey)
		return false
	else
		rename(key, newkey)
		return true
	end
end

#rpop(key) ⇒ Object

Remove and get the last element in a list



308
309
310
311
312
313
# File 'lib/strc.rb', line 308

def rpop(key)
	list = get_l(key)
	element = list.pop
	set(key, list)
	return element
end

#rpoplpush(source, destination) ⇒ Object

Removes an element from the end of one list and puts it at the beginning of another



357
358
359
# File 'lib/strc.rb', line 357

def rpoplpush(source, destination)
	lpush(destination, rpop(source))
end

#rpush(key, *values) ⇒ Object

Append values to a list



293
294
295
296
297
298
# File 'lib/strc.rb', line 293

def rpush(key, *values)
	list = get_l(key)
	list += values
	set(key, list)
	return list.length
end

#rpushx(key, *values) ⇒ Object

RPUSH if the list exists



301
302
303
304
305
# File 'lib/strc.rb', line 301

def rpushx(key, *values)
	if exists(key)
		rpush(key, *values)
	end
end

#sadd(key, *args) ⇒ Object

Add a member to a set



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/strc.rb', line 128

def sadd(key, *args)
	if args.length < 1
		raise STRC::Exception.new "Wrong number of arguments (1 for 2)"
	end
	set = get_s(key)
	added = 0
	args.each do |arg|
		unless set.include?(arg)
			set << arg
			added += 1
		end
	end
	@dict[key] = set
	return added
end

#scard(key) ⇒ Object

Gets the length of a set



248
249
250
251
# File 'lib/strc.rb', line 248

def scard(key)
	set = get_s(key)
	return set.length
end

#sdiff(*keys) ⇒ Object

Returns a list of the difference between the first set and subsequent sets



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

def sdiff(*keys)
	sets = []
	keys.each do |key|
		sets << get_s(key)
	end
	diff = sets.shift
	sets.each do |set|
		diff -= set
	end
	return diff.to_a
end

#sdiffstore(destination, *keys) ⇒ Object

Similar to SDIFF, but stores in destination instead of returning



188
189
190
191
# File 'lib/strc.rb', line 188

def sdiffstore(destination, *keys)
	elements = sdiff(*keys)
	sadd(destination, *elements)
end

#set(key, value) ⇒ Object

Set a given key to a value



11
12
13
14
# File 'lib/strc.rb', line 11

def set(key, value)
	@dict[key] = value
	return "OK"
end

#setnx(key, value) ⇒ Object

Set the value of a key only if it doesn’t already exist.



17
18
19
20
21
22
23
# File 'lib/strc.rb', line 17

def setnx(key, value)
	unless exists(key)
		set(key, value)
		return true
	end
	return false
end

#sinter(*keys) ⇒ Object

Returns a list of m



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/strc.rb', line 155

def sinter(*keys)
	sets = []
	keys.each do |key|
		sets << get_s(key)
	end
	inter = sets.shift
	sets.each do |set|
		inter &= set
	end
	return inter.to_a
end

#sinterstore(destination, *keys) ⇒ Object

Similar to SINTER, but stores in destination instead of returning



168
169
170
171
# File 'lib/strc.rb', line 168

def sinterstore(destination, *keys)
	elements = sinter(*keys)
	sadd(destination, *elements)
end

#sismember(key, member) ⇒ Object

Determine if the given value is a member of the set at key



237
238
239
240
# File 'lib/strc.rb', line 237

def sismember(key, member)
	set = get_s(key)
	return set.include?(member)
end

#smembers(key) ⇒ Object

Returns an array of members of the set



243
244
245
# File 'lib/strc.rb', line 243

def smembers(key)
	get_s(key).to_a
end

#smove(source, destination, member) ⇒ Object

Move an item from one set to another



194
195
196
197
198
199
200
201
202
203
# File 'lib/strc.rb', line 194

def smove(source, destination, member)
	unless sismember(source, member)
		return false
	end
	srem(source, member)
	unless sismember(destination, member)
		sadd(destination, member)
	end
	return true
end

#spop(key) ⇒ Object

Randomly remove and return an item from the set



230
231
232
233
234
# File 'lib/strc.rb', line 230

def spop(key)
	element = srandmember(key)
	srem(key, element)
	return element
end

#srandmember(key) ⇒ Object

Returns a random element from the set



225
226
227
# File 'lib/strc.rb', line 225

def srandmember(key)
	get_s(key).to_a.sample
end

#srem(key, member) ⇒ Object

Remove a member from a set



145
146
147
148
149
150
151
152
# File 'lib/strc.rb', line 145

def srem(key, member)
	if sismember(key, member)
		@dict[key].delete(member)
		return true
	else
		return false
	end
end

#sunion(*keys) ⇒ Object

Returs a list of all unique items in the given sets



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/strc.rb', line 206

def sunion(*keys)
	sets = []
	keys.each do |key|
		sets << get_s(key)
	end
	union = sets.shift
	sets.each do |set|
		union += set
	end
	return union.to_a
end

#sunionstore(destination, *keys) ⇒ Object

Similar to SUNION, but stores in destination instead of returning



219
220
221
222
# File 'lib/strc.rb', line 219

def sunionstore(destination, *keys)
	elements = sunion(*keys)
	sadd(destination, *elements)
end