Class: Yasha

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYasha

Returns a new instance of Yasha.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/yasha.rb', line 10

def initialize
  @database = nil
  @database_id = nil
  @table = nil
  @table_id = nil
  @host = "127.0.0.1"
  @port = 6379
  @redis_connection = nil
  @table_struct = nil
  @counter_key = nil
  self.initial_check
end

Instance Attribute Details

#counter_keyObject

Returns the value of attribute counter_key.



8
9
10
# File 'lib/yasha.rb', line 8

def counter_key
  @counter_key
end

#databaseObject

Returns the value of attribute database.



8
9
10
# File 'lib/yasha.rb', line 8

def database
  @database
end

#database_idObject

Returns the value of attribute database_id.



8
9
10
# File 'lib/yasha.rb', line 8

def database_id
  @database_id
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/yasha.rb', line 8

def host
  @host
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/yasha.rb', line 8

def port
  @port
end

#redis_connectionObject

Returns the value of attribute redis_connection.



8
9
10
# File 'lib/yasha.rb', line 8

def redis_connection
  @redis_connection
end

#tableObject

Returns the value of attribute table.



8
9
10
# File 'lib/yasha.rb', line 8

def table
  @table
end

#table_idObject

Returns the value of attribute table_id.



8
9
10
# File 'lib/yasha.rb', line 8

def table_id
  @table_id
end

#table_structObject

Returns the value of attribute table_struct.



8
9
10
# File 'lib/yasha.rb', line 8

def table_struct
  @table_struct
end

Class Method Details

.create_database(name) ⇒ Object

CREATE###



45
46
47
48
49
50
51
52
53
# File 'lib/yasha.rb', line 45

def self.create_database name
  self.initial_check
  @redis_connection.set("YashA:#{name}", [].to_json)
  yasha_databases = JSON.parse(@redis_connection.get("YashA:DataBases"))
  yasha_databases << name
  @redis_connection.set("YashA:DataBases", yasha_databases.to_json)
  @database = name
  @database_id = yasha_databases.index(name)
end

.create_table(table_name, *fields) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yasha.rb', line 55

def self.create_table table_name, *fields
  tables = JSON.parse(@redis_connection.get("YashA:#{@database}"))
  tables << table_name
  @redis_connection.set("YashA:#{@database}", tables.to_json)
  @redis_connection.set("YashA:#{@database}:#{table_name}", fields.to_json)
  @table = table_name
  @table_struct = fields
  @table_id = tables.index(table_name) 
  @counter_key = "YashA:counter:#{@database_id}:#{@table_id}"
  @redis_connection.set(@counter_key, 0)
end

.database_is(name) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/yasha.rb', line 77

def self.database_is name
  self.initial_check
  @database = name

  yasha_databases = JSON.parse(@redis_connection.get("YashA:DataBases"))
  @database_id = yasha_databases.index(name)
end

.delete(deletion) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/yasha.rb', line 291

def self.delete deletion

  if deletion.has_key? :index
    self.delete_by_index deletion[:index]
  else
    self.delete_by_condition deletion[:conditions]
  end

end

.delete_allObject



270
271
272
273
274
275
276
# File 'lib/yasha.rb', line 270

def self.delete_all
  rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*") + @redis_connection.keys("YashA:Row:#{@database_id}:#{@table_id}:*")
  rows.each do |row|
    @redis_connection.del(row)
  end
  @redis_connection.set(@counter_key, 0)
end

.delete_by_condition(conditions) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/yasha.rb', line 278

def self.delete_by_condition conditions

  self.delete_all if conditions == "all"

  rows = self.select_by_conditions(conditions, nil, 0)
  return false if rows.nil?
  
  rows.each do |row|
    self.delete_by_index row["index"]
  end

end

.delete_by_index(index) ⇒ Object

DELETE###



261
262
263
264
265
266
267
268
# File 'lib/yasha.rb', line 261

def self.delete_by_index index
  rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*:*:#{index}")
  rows.each do |row|
    @redis_connection.del(row)
  end

  @redis_connection.del("YashA:Row:#{@database_id}:#{@table_id}:#{index}")
end

.detailsObject



98
99
100
# File 'lib/yasha.rb', line 98

def self.details
  puts "Current Database: #{@database}, Table: #{@table}"
end

.host_is(host) ⇒ Object

SETTING###



69
70
71
# File 'lib/yasha.rb', line 69

def self.host_is host
  @host = host
end

.initial_checkObject



29
30
31
32
# File 'lib/yasha.rb', line 29

def self.initial_check
  @redis_connection = Redis.new(:host => @host,:port => @port)
  self.initialize_redis if @redis_connection.get("YashA:DataBases").nil?
end

.initialize_redisObject

CHECKS###



25
26
27
# File 'lib/yasha.rb', line 25

def self.initialize_redis
  @redis_connection.set("YashA:DataBases", [].to_json)
end

.insert(row) ⇒ Object

INSERT###



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/yasha.rb', line 104

def self.insert row
  row.keys.each do |field|
    return "#{field} not in table." if not @table_struct.include? field
  end    

  @table_struct.each do |field|
    row[field] = nil if not row.keys.include? field 
  end

  row_index = @redis_connection.get(@counter_key)

  row.each do |key, value|
    yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
    @redis_connection.set(yasha_key, row_index)
  end
  
  @redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{row_index}", row.to_json)
  @redis_connection.incr(@counter_key)
end

.is_database(name) ⇒ Object



34
35
36
37
# File 'lib/yasha.rb', line 34

def self.is_database name
  self.initial_check
  JSON.parse(@redis_connection.get("YashA:DataBases")).include? name
end

.is_table(table, database) ⇒ Object



39
40
41
# File 'lib/yasha.rb', line 39

def self.is_table table, database
  JSON.parse(@redis_connection.get("YashA:#{database}")).include? table
end

.port_is(port) ⇒ Object



73
74
75
# File 'lib/yasha.rb', line 73

def self.port_is port
  @port = port
end

.select(query = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/yasha.rb', line 181

def self.select query = nil

  return self.select_all if query.nil?

  case
  when (query.has_key? :index) then return self.select_by_index(query[:index])
  when (query.has_key? :limit and not query.has_key? :conditions) then return self.select_all(query[:limit])
  when (query.has_key? :limit and query.has_key? :conditions) then return self.select_by_conditions(query[:conditions], query[:limit])
  when (not query.has_key? :limit and query.has_key? :conditions) then return self.select_by_conditions(query[:conditions])
  else return nil
  end

end

.select_all(limit = nil) ⇒ Object



177
178
179
# File 'lib/yasha.rb', line 177

def self.select_all limit = nil
  limit.nil? ? self.select_rows(@redis_connection.get(@counter_key).to_i) : self.select_rows(limit)
end

.select_by_condition(key, value, limit = nil) ⇒ Object

SELECT###



126
127
128
129
130
131
132
133
134
# File 'lib/yasha.rb', line 126

def self.select_by_condition key, value, limit = nil
  result_indexs = []
  rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:*")
  rows = rows.slice(0, limit) if not limit.nil?
  rows.each do |row|
    result_indexs << @redis_connection.get(row)
  end
  return result_indexs
end

.select_by_conditions(query, limit = nil, internal = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/yasha.rb', line 136

def self.select_by_conditions query, limit = nil, internal = nil
  result_indexs = []

  query.each do |key, value|
    condition_indexs = self.select_by_condition(key, value, limit)
    result_indexs = result_indexs.empty? ? condition_indexs : result_indexs & condition_indexs
    return nil if result_indexs.empty? or condition_indexs.empty?
  end
  
  results = []

  result_indexs.uniq.each do |index|
    row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
    row["index"] = index
    results << (internal.nil? ? OpenStruct.new(row) : row)
  end

  return results

end

.select_by_index(index) ⇒ Object



157
158
159
160
161
162
# File 'lib/yasha.rb', line 157

def self.select_by_index index
  return nil if index > @redis_connection.get(@counter_key).to_i - 1 or index < 0
  row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
  row["index"] = index
  return OpenStruct.new(row)
end

.select_rows(number) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/yasha.rb', line 164

def self.select_rows number
  result_list = []

  number.times do |id|
    row = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{id}"))
    row["index"] = id
    result_list << OpenStruct.new(row)      
  end

  result_list.length == 1 ? result_list[0] : result_list

end

.table_is(name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/yasha.rb', line 85

def self.table_is name
  @table = name

  p "YashA:#{@database}"

  tables = JSON.parse(@redis_connection.get("YashA:#{@database}"))
  @table_id = tables.index(name)

  @table_struct = JSON.parse(@redis_connection.get("YashA:#{@database}:#{name}"))

  @counter_key = "YashA:counter:#{@database_id}:#{@table_id}"
end

.update(updation) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/yasha.rb', line 243

def self.update updation

  if updation.has_key? :index
    self.update_index updation[:index], updation[:set]
  else
    return false if updation[:conditions].nil?
    rows = self.select_by_conditions(updation[:conditions], nil, 0)
    return false if rows.nil?

    rows.each do |row|
      self.update_row(row, updation[:set])
    end
  end

end

.update_index(index, updation) ⇒ Object

UPDATE###



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/yasha.rb', line 197

def self.update_index index, updation
  rows = @redis_connection.keys("YashA:#{@database_id}:#{@table_id}:*:*:#{index}")
  rows.each do |row|
    @redis_connection.del(row)
  end
 
  row_data = JSON.parse(@redis_connection.get("YashA:Row:#{@database_id}:#{@table_id}:#{index}"))
  row_data.each do |key, value|
    row_data[key] = updation[key]  if updation.keys.include? key
  end
  @redis_connection.del("YashA:Row:#{@database_id}:#{@table_id}:#{index}")


  row_data.each do |key, value|
    new_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{index}"
    @redis_connection.set(new_key, index)
  end

  @redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{index}", row_data.to_json)

end

.update_row(row, updation) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/yasha.rb', line 219

def self.update_row row, updation

  row_index = row["index"]
  row.delete("index")
  updated_row = updation

  row.each do |key, value|
    if updation.keys.include? key
      yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
      @redis_connection.del(yasha_key)
    else
      updated_row[key] = value
    end
  end

  updation.each do |key, value|
    yasha_key = "YashA:#{@database_id}:#{@table_id}:#{@table_struct.index(key)}:#{value}:#{row_index}"
    @redis_connection.set(yasha_key, row_index)
  end
  
  @redis_connection.set("YashA:Row:#{@database_id}:#{@table_id}:#{row_index}", updated_row.to_json)
    
end