yasha 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yasha.rb +350 -0
- metadata +68 -0
data/lib/yasha.rb
ADDED
@@ -0,0 +1,350 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'redis'
|
3
|
+
require 'json'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
class Yasha
|
7
|
+
|
8
|
+
attr_accessor :database, :table, :database_id, :table_id, :counter_key, :host, :port, :redis_connection, :table_struct
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@database = nil
|
12
|
+
@database_id = nil
|
13
|
+
@table = nil
|
14
|
+
@table_id = nil
|
15
|
+
@host = "127.0.0.1"
|
16
|
+
@port = 6379
|
17
|
+
@redis_connection = nil
|
18
|
+
@table_struct = nil
|
19
|
+
@counter_key = nil
|
20
|
+
self.initial_check
|
21
|
+
end
|
22
|
+
|
23
|
+
###CHECKS###
|
24
|
+
|
25
|
+
def self.initialize_redis
|
26
|
+
@redis_connection.set("YashA:DataBases", [].to_json)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.initial_check
|
30
|
+
@redis_connection = Redis.new(:host => @host,:port => @port)
|
31
|
+
self.initialize_redis if @redis_connection.get("YashA:DataBases").nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.is_database name
|
35
|
+
self.initial_check
|
36
|
+
JSON.parse(@redis_connection.get("YashA:DataBases")).include? name
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.is_table table, database
|
40
|
+
JSON.parse(@redis_connection.get("YashA:#{database}")).include? table
|
41
|
+
end
|
42
|
+
|
43
|
+
###CREATE###
|
44
|
+
|
45
|
+
def self.create_database name
|
46
|
+
self.initial_check
|
47
|
+
@redis_connection.set("YashA:#{name}", [].to_json)
|
48
|
+
yasha_databases = JSON.parse(@redis_connection.get("YashA:DataBases"))
|
49
|
+
yasha_databases << name
|
50
|
+
@redis_connection.set("YashA:DataBases", yasha_databases.to_json)
|
51
|
+
@database = name
|
52
|
+
@database_id = yasha_databases.index(name)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.create_table table_name, *fields
|
56
|
+
tables = JSON.parse(@redis_connection.get("YashA:#{@database}"))
|
57
|
+
tables << table_name
|
58
|
+
@redis_connection.set("YashA:#{@database}", tables.to_json)
|
59
|
+
@redis_connection.set("YashA:#{@database}:#{table_name}", fields.to_json)
|
60
|
+
@table = table_name
|
61
|
+
@table_struct = fields
|
62
|
+
@table_id = tables.index(table_name)
|
63
|
+
@counter_key = "YashA:counter:#{@database_id}:#{@table_id}"
|
64
|
+
@redis_connection.set(@counter_key, 0)
|
65
|
+
end
|
66
|
+
|
67
|
+
###SETTING###
|
68
|
+
|
69
|
+
def self.host_is host
|
70
|
+
@host = host
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.port_is port
|
74
|
+
@port = port
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.database_is name
|
78
|
+
self.initial_check
|
79
|
+
@database = name
|
80
|
+
|
81
|
+
yasha_databases = JSON.parse(@redis_connection.get("YashA:DataBases"))
|
82
|
+
@database_id = yasha_databases.index(name)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.table_is name
|
86
|
+
@table = name
|
87
|
+
|
88
|
+
p "YashA:#{@database}"
|
89
|
+
|
90
|
+
tables = JSON.parse(@redis_connection.get("YashA:#{@database}"))
|
91
|
+
@table_id = tables.index(name)
|
92
|
+
|
93
|
+
@table_struct = JSON.parse(@redis_connection.get("YashA:#{@database}:#{name}"))
|
94
|
+
|
95
|
+
@counter_key = "YashA:counter:#{@database_id}:#{@table_id}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.details
|
99
|
+
puts "Current Database: #{@database}, Table: #{@table}"
|
100
|
+
end
|
101
|
+
|
102
|
+
###INSERT###
|
103
|
+
|
104
|
+
def self.insert row
|
105
|
+
row.keys.each do |field|
|
106
|
+
return "#{field} not in table." if not @table_struct.include? field
|
107
|
+
end
|
108
|
+
|
109
|
+
@table_struct.each do |field|
|
110
|
+
row[field] = nil if not row.keys.include? field
|
111
|
+
end
|
112
|
+
|
113
|
+
row_index = @redis_connection.get(@counter_key)
|
114
|
+
|
115
|
+
row.each do |key, value|
|
116
|
+
yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
|
117
|
+
@redis_connection.set(yasha_key, row_index)
|
118
|
+
end
|
119
|
+
|
120
|
+
@redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{row_index}", row.to_json)
|
121
|
+
@redis_connection.incr(@counter_key)
|
122
|
+
end
|
123
|
+
|
124
|
+
###SELECT###
|
125
|
+
|
126
|
+
def self.select_by_condition key, value, limit = nil
|
127
|
+
result_indexs = []
|
128
|
+
rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:*")
|
129
|
+
rows = rows.slice(0, limit) if not limit.nil?
|
130
|
+
rows.each do |row|
|
131
|
+
result_indexs << @redis_connection.get(row)
|
132
|
+
end
|
133
|
+
return result_indexs
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.select_by_conditions query, limit = nil, internal = nil
|
137
|
+
result_indexs = []
|
138
|
+
|
139
|
+
query.each do |key, value|
|
140
|
+
condition_indexs = self.select_by_condition(key, value, limit)
|
141
|
+
result_indexs = result_indexs.empty? ? condition_indexs : result_indexs & condition_indexs
|
142
|
+
return nil if result_indexs.empty? or condition_indexs.empty?
|
143
|
+
end
|
144
|
+
|
145
|
+
results = []
|
146
|
+
|
147
|
+
result_indexs.uniq.each do |index|
|
148
|
+
row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
|
149
|
+
row["index"] = index
|
150
|
+
results << (internal.nil? ? OpenStruct.new(row) : row)
|
151
|
+
end
|
152
|
+
|
153
|
+
return results
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.select_by_index index
|
158
|
+
return nil if index > @redis_connection.get(@counter_key).to_i - 1 or index < 0
|
159
|
+
row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
|
160
|
+
row["index"] = index
|
161
|
+
return OpenStruct.new(row)
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.select_rows number
|
165
|
+
result_list = []
|
166
|
+
|
167
|
+
number.times do |id|
|
168
|
+
row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{id}"))
|
169
|
+
row["index"] = id
|
170
|
+
result_list << OpenStruct.new(row)
|
171
|
+
end
|
172
|
+
|
173
|
+
result_list.length == 1 ? result_list[0] : result_list
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.select_all limit = nil
|
178
|
+
limit.nil? ? self.select_rows(@redis_connection.get(@counter_key).to_i) : self.select_rows(limit)
|
179
|
+
end
|
180
|
+
|
181
|
+
def self.select query = nil
|
182
|
+
|
183
|
+
return self.select_all if query.nil?
|
184
|
+
|
185
|
+
case
|
186
|
+
when (query.has_key? :index) then return self.select_by_index(query[:index])
|
187
|
+
when (query.has_key? :limit and not query.has_key? :conditions) then return self.select_all(query[:limit])
|
188
|
+
when (query.has_key? :limit and query.has_key? :conditions) then return self.select_by_conditions(query[:conditions], query[:limit])
|
189
|
+
when (not query.has_key? :limit and query.has_key? :conditions) then return self.select_by_conditions(query[:conditions])
|
190
|
+
else return nil
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
###UPDATE###
|
196
|
+
|
197
|
+
def self.update_index index, updation
|
198
|
+
rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*:*:#{index}")
|
199
|
+
rows.each do |row|
|
200
|
+
@redis_connection.del(row)
|
201
|
+
end
|
202
|
+
|
203
|
+
row_data = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
|
204
|
+
row_data.each do |key, value|
|
205
|
+
row_data[key] = updation[key] if updation.keys.include? key
|
206
|
+
end
|
207
|
+
@redis_connection.del("YashA:Row:#{@database_id}:#{@table_id}:#{index}")
|
208
|
+
|
209
|
+
|
210
|
+
row_data.each do |key, value|
|
211
|
+
new_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{index}"
|
212
|
+
@redis_connection.set(new_key, index)
|
213
|
+
end
|
214
|
+
|
215
|
+
@redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{index}", row_data.to_json)
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
def self.update_row row, updation
|
220
|
+
|
221
|
+
row_index = row["index"]
|
222
|
+
row.delete("index")
|
223
|
+
updated_row = updation
|
224
|
+
|
225
|
+
row.each do |key, value|
|
226
|
+
if updation.keys.include? key
|
227
|
+
yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
|
228
|
+
@redis_connection.del(yasha_key)
|
229
|
+
else
|
230
|
+
updated_row[key] = value
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
updation.each do |key, value|
|
235
|
+
yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
|
236
|
+
@redis_connection.set(yasha_key, row_index)
|
237
|
+
end
|
238
|
+
|
239
|
+
@redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{row_index}", updated_row.to_json)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
def self.update updation
|
244
|
+
|
245
|
+
if updation.has_key? :index
|
246
|
+
self.update_index updation[:index], updation[:set]
|
247
|
+
else
|
248
|
+
return false if updation[:conditions].nil?
|
249
|
+
rows = self.select_by_conditions(updation[:conditions], nil, 0)
|
250
|
+
return false if rows.nil?
|
251
|
+
|
252
|
+
rows.each do |row|
|
253
|
+
self.update_row(row, updation[:set])
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
###DELETE###
|
260
|
+
|
261
|
+
def self.delete_by_index index
|
262
|
+
rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*:*:#{index}")
|
263
|
+
rows.each do |row|
|
264
|
+
@redis_connection.del(row)
|
265
|
+
end
|
266
|
+
|
267
|
+
@redis_connection.del("YashA:Row:#{@database_id}:#{@table_id}:#{index}")
|
268
|
+
end
|
269
|
+
|
270
|
+
def self.delete_all
|
271
|
+
rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*") + @redis_connection.keys("YashA:Row:#{@database_id}:#{@table_id}:*")
|
272
|
+
rows.each do |row|
|
273
|
+
@redis_connection.del(row)
|
274
|
+
end
|
275
|
+
@redis_connection.set(@counter_key, 0)
|
276
|
+
end
|
277
|
+
|
278
|
+
def self.delete_by_condition conditions
|
279
|
+
|
280
|
+
self.delete_all if conditions == "all"
|
281
|
+
|
282
|
+
rows = self.select_by_conditions(conditions, nil, 0)
|
283
|
+
return false if rows.nil?
|
284
|
+
|
285
|
+
rows.each do |row|
|
286
|
+
self.delete_by_index row["index"]
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
def self.delete deletion
|
292
|
+
|
293
|
+
if deletion.has_key? :index
|
294
|
+
self.delete_by_index deletion[:index]
|
295
|
+
else
|
296
|
+
self.delete_by_condition deletion[:conditions]
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
|
304
|
+
##########
|
305
|
+
# USAGE #
|
306
|
+
#########
|
307
|
+
|
308
|
+
|
309
|
+
#class Job < Yasha
|
310
|
+
# if self.is_database 'history'
|
311
|
+
# puts "DB Exists"
|
312
|
+
# self.database_is 'history'
|
313
|
+
# else
|
314
|
+
# puts "DB Dosent Exist"
|
315
|
+
# self.create_database 'history'
|
316
|
+
# end
|
317
|
+
|
318
|
+
# if self.is_table 'generals', 'history'
|
319
|
+
# puts "Table Exists"
|
320
|
+
# self.table_is 'generals'
|
321
|
+
# else
|
322
|
+
# puts "Table Dosent Exist"
|
323
|
+
# self.create_table 'generals', 'name', 'alias', 'nationality'
|
324
|
+
# end
|
325
|
+
#end
|
326
|
+
|
327
|
+
#Job.details
|
328
|
+
|
329
|
+
#Job.insert({"name" => "VasiliChuikov", "alias" => "SaviorStalingrad", "nationality" => "Russian"}) #WF
|
330
|
+
#Job.insert({"name" => "MontGomery", "alias" => "DesertStorm", "nationality" => "British"}) #WF
|
331
|
+
#Job.insert({"name" => "Fermanshtine", "alias" => "BerlinGuard", "nationality" => "German"}) #WF
|
332
|
+
#Job.insert({"name" => "DouglasMcAurthor", "alias" => "BeBack", "nationality" => "USA"}) #WF
|
333
|
+
#Job.insert({"name" => "ErwinRomell", "alias" => "DesertFox", "nationality" => "German"}) #WF
|
334
|
+
#Job.insert({"name" => "Patton", "alias" => "TheTank", "nationality" => "USA"}) #WF
|
335
|
+
#Job.insert({"name" => "Fredrik Paulo", "alias" => "Barbarosa", "nationality" => "German"}) #WF
|
336
|
+
|
337
|
+
#Job.select(:index => 5) #WF
|
338
|
+
#Job.select #WF
|
339
|
+
#Job.select(:limit => 6) #WF
|
340
|
+
#Job.select(:conditions => {"name" => "Patton"}) #WF
|
341
|
+
#Job.select(:conditions => {"nationality" => "German"}, :limit => 2) #WF
|
342
|
+
#Job.select(:conditions => {"nationality" => "German", "name" => "*o*", "alias" => "*t*"}) #WF
|
343
|
+
|
344
|
+
#Job.update(:set => {"name" => "WalterModel", "alias" => "BulgeBat"}, :conditions => {"name" => "Fermanshtine"}) #WF
|
345
|
+
#Job.update(:set => {"name" => "Fermanchtine", "alias" => "BerlinGuard", "nationality" => "German"}, :index => 2) #WF
|
346
|
+
|
347
|
+
#Job.delete(:index => 1) #WF
|
348
|
+
#Job.delete(:conditions => {"nationality" => "USA"}) #WF
|
349
|
+
#Job.delete(:conditions => "all") #WF
|
350
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yasha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Edwin Rozario
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-27 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: ORM for Redis which is used like an ORM for SQL database.
|
23
|
+
email:
|
24
|
+
- rozarioed@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/yasha.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: https://github.com/EdwinRozario/Yasha
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Mysql like usage of Redis.
|
67
|
+
test_files: []
|
68
|
+
|