Class: JOR::Storage

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

Constant Summary collapse

NAMESPACE =
"jor"
SELECTORS =
{
  :compare => ["$gt","$gte","$lt","$lte"],
  :sets => ["$in","$all","$not"]
}
SELECTORS_ALL =
SELECTORS.keys.inject([]) { |sel, element| sel | SELECTORS[element] }

Instance Method Summary collapse

Constructor Details

#initialize(redis_client = nil) ⇒ Storage

Returns a new instance of Storage.



14
15
16
# File 'lib/jor/storage.rb', line 14

def initialize(redis_client = nil)
  redis_client.nil? ? @redis = Redis.new() : @redis = redis_client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object (protected)



71
72
73
# File 'lib/jor/storage.rb', line 71

def method_missing(method)
  find_collection(method)
end

Instance Method Details

#collectionsObject



22
23
24
# File 'lib/jor/storage.rb', line 22

def collections
  redis.smembers("#{Storage::NAMESPACE}/collections")
end

#create_collection(name, options = {:auto_increment => false}) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
# File 'lib/jor/storage.rb', line 26

def create_collection(name, options = {:auto_increment => false})
  options = {:auto_increment => false}.merge(options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
  raise CollectionNotValid.new(name) if self.respond_to?(name)
  is_new = redis.sadd("#{Storage::NAMESPACE}/collections",name)
  raise CollectionAlreadyExists.new(name) if (is_new==false or is_new==0)
  redis.set("#{Storage::NAMESPACE}/collection/#{name}/auto-increment", options[:auto_increment])
  name
end

#destroy_allObject



45
46
47
48
49
# File 'lib/jor/storage.rb', line 45

def destroy_all()
  collections.each do |col|
    destroy_collection(col)
  end
end

#destroy_collection(name) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/jor/storage.rb', line 35

def destroy_collection(name)
  coll_to_be_removed = find_collection(name)
  coll_to_be_removed.delete({})
  redis.pipelined do
    redis.srem("#{Storage::NAMESPACE}/collections",name)
    redis.del("#{Storage::NAMESPACE}/collection/#{name}/auto-increment")
  end
  name
end

#infoObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jor/storage.rb', line 51

def info
  res = {}
  ri = redis.info

  res["used_memory_in_redis"] = ri["used_memory"].to_i
  res["num_collections"] = collections.size

  res["collections"] = {}
  collections.each do |c|
    coll = find_collection(c)
    res["collections"][coll.name] = {}
    res["collections"][coll.name]["num_documents"] = coll.count
    res["collections"][coll.name]["auto_increment"] = coll.auto_increment?
  end

  res
end

#redisObject



18
19
20
# File 'lib/jor/storage.rb', line 18

def redis
  @redis
end