Class: Yasha
- Inherits:
-
Object
- Object
- Yasha
- Defined in:
- lib/yasha.rb
Instance Attribute Summary collapse
-
#counter_key ⇒ Object
Returns the value of attribute counter_key.
-
#database ⇒ Object
Returns the value of attribute database.
-
#database_id ⇒ Object
Returns the value of attribute database_id.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#redis_connection ⇒ Object
Returns the value of attribute redis_connection.
-
#table ⇒ Object
Returns the value of attribute table.
-
#table_id ⇒ Object
Returns the value of attribute table_id.
-
#table_struct ⇒ Object
Returns the value of attribute table_struct.
Class Method Summary collapse
-
.create_database(name) ⇒ Object
CREATE###.
- .create_table(table_name, *fields) ⇒ Object
- .database(name) ⇒ Object
- .database?(name) ⇒ Boolean
- .delete(deletion) ⇒ Object
- .delete_all ⇒ Object
- .delete_by_condition(conditions) ⇒ Object
-
.delete_by_index(index) ⇒ Object
DELETE###.
- .details ⇒ Object
-
.host(host) ⇒ Object
SETTING###.
- .initial_check ⇒ Object
-
.initialize_redis ⇒ Object
CHECKS###.
- .initialize_yasha ⇒ Object
-
.insert(row) ⇒ Object
INSERT###.
- .port(port) ⇒ Object
- .select(query = nil) ⇒ Object
- .select_all(limit = nil) ⇒ Object
-
.select_by_condition(key, value, limit = nil) ⇒ Object
SELECT###.
- .select_by_conditions(query, limit = nil, internal = nil) ⇒ Object
- .select_by_index(index) ⇒ Object
- .select_rows(number) ⇒ Object
- .table(name) ⇒ Object
- .table?(table, database) ⇒ Boolean
- .update(updation) ⇒ Object
-
.update_index(index, updation) ⇒ Object
UPDATE###.
- .update_row(row, updation) ⇒ Object
Instance Attribute Details
#counter_key ⇒ Object
Returns the value of attribute counter_key.
8 9 10 |
# File 'lib/yasha.rb', line 8 def counter_key @counter_key end |
#database ⇒ Object
Returns the value of attribute database.
8 9 10 |
# File 'lib/yasha.rb', line 8 def database @database end |
#database_id ⇒ Object
Returns the value of attribute database_id.
8 9 10 |
# File 'lib/yasha.rb', line 8 def database_id @database_id end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/yasha.rb', line 8 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/yasha.rb', line 8 def port @port end |
#redis_connection ⇒ Object
Returns the value of attribute redis_connection.
8 9 10 |
# File 'lib/yasha.rb', line 8 def redis_connection @redis_connection end |
#table ⇒ Object
Returns the value of attribute table.
8 9 10 |
# File 'lib/yasha.rb', line 8 def table @table end |
#table_id ⇒ Object
Returns the value of attribute table_id.
8 9 10 |
# File 'lib/yasha.rb', line 8 def table_id @table_id end |
#table_struct ⇒ Object
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(name) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/yasha.rb', line 77 def self.database name self.initial_check @database = name yasha_databases = JSON.parse(@redis_connection.get("YashA:DataBases")) @database_id = yasha_databases.index(name) end |
.database?(name) ⇒ Boolean
34 35 36 37 |
# File 'lib/yasha.rb', line 34 def self.database? name self.initial_check JSON.parse(@redis_connection.get("YashA:DataBases")).include? name end |
.delete(deletion) ⇒ Object
289 290 291 292 293 294 295 296 297 |
# File 'lib/yasha.rb', line 289 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_all ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/yasha.rb', line 268 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
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/yasha.rb', line 276 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###
259 260 261 262 263 264 265 266 |
# File 'lib/yasha.rb', line 259 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 |
.details ⇒ Object
96 97 98 |
# File 'lib/yasha.rb', line 96 def self.details puts "Current Database: #{@database}, Table: #{@table}" end |
.host(host) ⇒ Object
SETTING###
69 70 71 |
# File 'lib/yasha.rb', line 69 def self.host host @host = host end |
.initial_check ⇒ Object
28 29 30 31 32 |
# File 'lib/yasha.rb', line 28 def self.initial_check self.initialize_yasha @redis_connection = Redis.new(:host => @host,:port => @port) self.initialize_redis if @redis_connection.get("YashA:DataBases").nil? end |
.initialize_redis ⇒ Object
CHECKS###
24 25 26 |
# File 'lib/yasha.rb', line 24 def self.initialize_redis @redis_connection.set("YashA:DataBases", [].to_json) end |
.initialize_yasha ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/yasha.rb', line 10 def self.initialize_yasha @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 end |
.insert(row) ⇒ Object
INSERT###
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/yasha.rb', line 102 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 |
.port(port) ⇒ Object
73 74 75 |
# File 'lib/yasha.rb', line 73 def self.port port @port = port end |
.select(query = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/yasha.rb', line 179 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
175 176 177 |
# File 'lib/yasha.rb', line 175 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###
124 125 126 127 128 129 130 131 132 |
# File 'lib/yasha.rb', line 124 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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/yasha.rb', line 134 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
155 156 157 158 159 160 |
# File 'lib/yasha.rb', line 155 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
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/yasha.rb', line 162 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(name) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/yasha.rb', line 85 def self.table name @table = name 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 |
.table?(table, database) ⇒ Boolean
39 40 41 |
# File 'lib/yasha.rb', line 39 def self.table? table, database JSON.parse(@redis_connection.get("YashA:#{database}")).include? table end |
.update(updation) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/yasha.rb', line 241 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###
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/yasha.rb', line 195 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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/yasha.rb', line 217 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 |