Class: Schron::Datastore::Mongo
- Inherits:
-
Object
- Object
- Schron::Datastore::Mongo
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
#db ⇒ Object
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
96
97
98
99
100
101
102
103
|
# File 'lib/schron/datastore/mongo.rb', line 96
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
87
88
89
90
91
92
93
94
|
# File 'lib/schron/datastore/mongo.rb', line 87
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
26
|
# File 'lib/schron/datastore/mongo.rb', line 20
def get(kind, id)
return nil unless id
doc = coll(kind).find_one(id_selector(id))
doc ?
unserialize(kind, doc) :
nil
end
|
#insert(kind, hash) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/schron/datastore/mongo.rb', line 35
def insert(kind, hash)
with_write_errors! do
hash[:id] ||= Schron::Id.generate
serialized = serialize(kind, hash)
coll(kind).insert(serialized)
hash
end
end
|
#multi_get(kind, ids) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/schron/datastore/mongo.rb', line 28
def multi_get(kind, ids)
ids = ids.compact
return [] if ids.empty?
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
44
45
46
47
48
49
50
51
|
# File 'lib/schron/datastore/mongo.rb', line 44
def multi_insert(kind, hashes)
with_write_errors! do
hashes.each { |h| h[:id] ||= Schron::Id.generate }
docs = hashes.map { |h| serialize(kind, h) }
coll(kind).insert(docs)
hashes
end
end
|
#multi_remove(kind, ids) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/schron/datastore/mongo.rb', line 80
def multi_remove(kind, ids)
with_write_errors! do
coll(kind).remove(multiple_id_selector(ids))
nil
end
end
|
#multi_update(kind, hashes) ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/schron/datastore/mongo.rb', line 62
def multi_update(kind, hashes)
with_write_errors! do
Schron::Id.require_all!(hashes)
hashes.each do |hash|
doc = serialize(kind, hash)
coll(kind).update(id_selector(hash[:id]), doc)
end
hashes
end
end
|
#remove(kind, id) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/schron/datastore/mongo.rb', line 73
def remove(kind, id)
with_write_errors! do
coll(kind).remove(id_selector(id))
nil
end
end
|
#reset! ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/schron/datastore/mongo.rb', line 105
def reset!
protect_reset do
@db.collections.each do |coll|
unless coll.name =~ /^system\./
coll.drop
end
end
end
end
|
#update(kind, hash) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/schron/datastore/mongo.rb', line 53
def update(kind, hash)
with_write_errors! do
Schron::Id.require!(hash)
doc = serialize(kind, hash)
coll(kind).update(id_selector(hash[:id]), doc)
hash
end
end
|