Class: Store::Mongodb

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

Defined Under Namespace

Classes: EqualFilter, GTEFilter, GTFilter, LTFilter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_name) ⇒ Mongodb

Returns a new instance of Mongodb.



10
11
12
13
# File 'lib/store/mongodb.rb', line 10

def initialize database_name
  @database_name = database_name
  @free_connections ||= []
end

Instance Attribute Details

#timestamperObject



6
7
8
# File 'lib/store/mongodb.rb', line 6

def timestamper
  @timestamper ||= lambda { Time.new }
end

Instance Method Details

#all(table) ⇒ Object



71
72
73
# File 'lib/store/mongodb.rb', line 71

def all table
  find(table,{})
end

#closeObject



15
16
17
18
# File 'lib/store/mongodb.rb', line 15

def close
  @db.close
  @db=nil
end

#collate(table, filters, opts = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/store/mongodb.rb', line 130

def collate table, filters, opts={}
  # need to get all items, or else we can't calculate facets
  start = opts.delete(:start)
  limit = opts.delete(:limit)
  facets = opts.delete(:facets)
  facetlimit = opts.delete(:facetlimit)

  result = {
    items: find(table, filters, opts)
  }

  if facets
    result[:facets] = calculate_facets(facets, result[:items])

    if facetlimit
      result[:facets].each do |k,v|
        v.pop while v.count > facetlimit
      end
    end
  end

  result[:count] = result[:items].count

  if start
    start.times do |i|
      result[:items].shift
    end
  end

  if limit
    result[:items].pop while result[:items].count > limit
  end

  result
end

#count(table) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/store/mongodb.rb', line 75

def count table
  connect do |db|
    resp = db.collection(table).count
    f = Fiber.current
    resp.callback {|count| f.resume count }
    resp.errback {|err| raise err }

    Fiber.yield
  end
end

#create(table, entry) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/store/mongodb.rb', line 20

def create table, entry
  connect do |db|
    entry['created_at'] = entry['updated_at'] = timestamper.call

    resp = db.collection(table).safe_insert(entry)

    f = Fiber.current
    resp.callback{|doc| f.resume(doc)}
    resp.errback{|err| f.resume(:err, err)}

    result, error = Fiber.yield

    if result == :err
      raise error.inspect
    else
      result
    end
  end
end

#create_equal_filter(field, name) ⇒ Object

filter factories



167
168
169
# File 'lib/store/mongodb.rb', line 167

def create_equal_filter field, name
  EqualFilter.new(field, name)
end

#create_gt_filter(field, name) ⇒ Object



173
174
175
# File 'lib/store/mongodb.rb', line 173

def create_gt_filter field, name
  GTFilter.new(field, name)
end

#create_gte_filter(field, name) ⇒ Object



176
177
178
# File 'lib/store/mongodb.rb', line 176

def create_gte_filter field, name
  GTEFilter.new(field, name)
end

#create_lt_filter(field, name) ⇒ Object



170
171
172
# File 'lib/store/mongodb.rb', line 170

def create_lt_filter field, name
  LTFilter.new(field, name)
end

#find(table, filters, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/store/mongodb.rb', line 92

def find table, filters, opts={}
  real_filters = {}
  filters.inject(real_filters) do |hash,f|
    f.add_filter(hash)
    hash
  end

  if opts[:sort]
    fields = opts.delete(:sort).split(',')
    opts[:sort] = []
    fields.each do |field|
      sort = field.split('=')
      name = sort[0]
      order = (sort[1] || '1') == '1' ? :asc : :desc
      opts[:sort] << [name,order]
    end
  end

  if opts[:start]
    start = opts.delete(:start)
    opts[:skip] = start
  end

  connect do |db|
    f = Fiber.current
    docs = []
    resp = db.collection(table).find(real_filters, opts).each do |doc|
      if doc
        docs << doc
      else
        f.resume if f.alive?
      end
    end
    Fiber.yield
    docs
  end
end

#reset(table) ⇒ Object



86
87
88
89
90
# File 'lib/store/mongodb.rb', line 86

def reset table
  connect do |db|
    db.collection(table).remove()
  end
end

#update(table, id, entry) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/store/mongodb.rb', line 40

def update table, id, entry
  if entry.keys.any?{|key| key.kind_of?(Symbol) }
    raise "MongoDb can't handle symbols, use only string keys!"
  end
  matcher = []
  filter = id.kind_of?(Hash) ? id : { _id: id }

  filter.each do |k,v|
    matcher << create_equal_filter(k,v)
  end

  connect do |db|
    old_entry = find(table, matcher).first

    if old_entry
      entry = old_entry.merge(entry)
      entry['updated_at'] = timestamper.call

      f = Fiber.current
      resp = db.collection(table).safe_update(filter, entry)
      resp.errback{|err| exit -1}
      resp.callback{|doc| f.resume doc}
      Fiber.yield
      entry
    else
      id = create(table, entry)
      find(table, matcher).first
    end
  end
end