Class: Prolly::Ps::Storage::Mongodb

Inherits:
Base
  • Object
show all
Defined in:
lib/prolly/ps/storage/mongodb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#import

Constructor Details

#initializeMongodb

Returns a new instance of Mongodb.



14
15
16
17
18
19
20
# File 'lib/prolly/ps/storage/mongodb.rb', line 14

def initialize
  @session ||= Moped::Session.new(["127.0.0.1:27017", "127.0.0.1:27018"])
  @session.use 'pspace'

  super
  @rand_vars = []
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/prolly/ps/storage/mongodb.rb', line 12

def session
  @session
end

Instance Method Details

#add(datum) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/prolly/ps/storage/mongodb.rb', line 27

def add(datum)
  # create an index for each new datum key
  #new_rvs(datum).each do |rv|
  #  @session.indexes.create(rv.to_sym => 1)
  #end

  record_new_rand_vars(datum)

  @session[:samples].insert(datum)
end

#count(rvs, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/prolly/ps/storage/mongodb.rb', line 38

def count(rvs, options = {})
  reload = options["reload"] || false
  if rvs.kind_of?(Array)
    @session[:samples].find(
      Hash[*rvs.flat_map { |rv| [rv, { '$exists' => true }] }]
    ).count
  elsif rvs.kind_of?(Hash)
    @session[:samples].find(to_query_hash(rvs)).count
  end
end

#rand_varsObject



49
50
51
# File 'lib/prolly/ps/storage/mongodb.rb', line 49

def rand_vars
  @session[:rand_vars].find.map { |rv| rv[:name] }
end

#resetObject



22
23
24
25
# File 'lib/prolly/ps/storage/mongodb.rb', line 22

def reset
  super
  @session['samples'].drop
end

#uniq_vals(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/prolly/ps/storage/mongodb.rb', line 53

def uniq_vals(name)
  @session[:samples].aggregate([
    { "$match" => { name.to_sym => { "$exists" => true } } },
    { "$group" => { "_id": "$#{name}" } }
  ]).map { |e| e["_id"] }
end