Class: Schron::Datastore::Mongo

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/schron/datastore/mongo.rb,
lib/schron/datastore/mongo/metadata.rb,
lib/schron/datastore/mongo/serializer.rb

Defined Under Namespace

Classes: Metadata, Serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Mongo

Returns a new instance of Mongo.



16
17
18
# File 'lib/schron/datastore/mongo.rb', line 16

def initialize(db)
  @db = db
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



14
15
16
# File 'lib/schron/datastore/mongo.rb', line 14

def db
  @db
end

Instance Method Details

#exec_count(query) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/schron/datastore/mongo.rb', line 81

def exec_count(query)
  query_selector = selector_for(query)
  query_opts = query_opts_for(query)
  query_opts.delete(:sort)
  coll(query.kind).count(query_opts.merge(query: query_selector))
rescue EmptyQueryError
  0
end

#exec_query(query) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/schron/datastore/mongo.rb', line 72

def exec_query(query)
  selector = selector_for(query)
  query_opts = query_opts_for(query)
  docs = coll(query.kind).find(selector, query_opts)
  docs.map{ |d| unserialize(query.kind, d) }
rescue EmptyQueryError
  []
end

#get(kind, id) ⇒ Object



20
21
22
23
24
25
# File 'lib/schron/datastore/mongo.rb', line 20

def get(kind, id)
  doc = coll(kind).find_one(id_selector(id))
  doc ?
    unserialize(kind, doc) :
    nil
end

#insert(kind, hash) ⇒ Object



32
33
34
35
36
37
# File 'lib/schron/datastore/mongo.rb', line 32

def insert(kind, hash)
  hash[:id] ||= Schron::Id.generate
  serialized = serialize(kind, hash)
  coll(kind).insert(serialized)
  hash
end

#multi_get(kind, ids) ⇒ Object



27
28
29
30
# File 'lib/schron/datastore/mongo.rb', line 27

def multi_get(kind, ids)
  docs = coll(kind).find(multiple_id_selector(ids))
  Schron::Util.sorted_by_id_list(docs.map{ |doc| unserialize(kind, doc) }, ids)
end

#multi_insert(kind, hashes) ⇒ Object



39
40
41
42
43
44
# File 'lib/schron/datastore/mongo.rb', line 39

def multi_insert(kind, hashes)
  hashes.each { |h| h[:id] ||= Schron::Id.generate }
  docs = hashes.map { |h| serialize(kind, h) }
  coll(kind).insert(docs)
  hashes
end

#multi_remove(kind, ids) ⇒ Object



67
68
69
70
# File 'lib/schron/datastore/mongo.rb', line 67

def multi_remove(kind, ids)
  coll(kind).remove(multiple_id_selector(ids))
  nil
end

#multi_update(kind, hashes) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/schron/datastore/mongo.rb', line 53

def multi_update(kind, hashes)
  Schron::Id.require_all!(hashes)
  hashes.each do |hash|
    doc = serialize(kind, hash)
    coll(kind).update(id_selector(hash[:id]), doc)
  end
  hashes
end

#remove(kind, id) ⇒ Object



62
63
64
65
# File 'lib/schron/datastore/mongo.rb', line 62

def remove(kind, id)
  coll(kind).remove(id_selector(id))
  nil
end

#reset!Object



90
91
92
93
94
95
96
97
98
# File 'lib/schron/datastore/mongo.rb', line 90

def reset!
  protect_reset do
    @db.collections.each do |coll|
      unless coll.name =~ /^system\./
        coll.drop
      end
    end
  end
end

#update(kind, hash) ⇒ Object



46
47
48
49
50
51
# File 'lib/schron/datastore/mongo.rb', line 46

def update(kind, hash)
  Schron::Id.require!(hash)
  doc = serialize(kind, hash)
  coll(kind).update(id_selector(hash[:id]), doc)
  hash
end