Class: Ruote::MongoDbStorage

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, StorageBase
Defined in:
lib/ruote-mongodb/mongodb_storage.rb

Constant Summary collapse

@@collection_prefix =
"ruote_"
@@encoded_dollar_sign =
"~#~"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MongoDbStorage

Returns a new instance of MongoDbStorage.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 12

def initialize(options={})
  super()
  db_config = {"host"=>"localhost", "port"=>27017, "database"=>"Ruote"}
  options = options.dup
  if environment = options.delete(:environment)
    all_db_config=
      File.open(options.delete(:config) || 'config/database.yml','r') do |f|
        YAML.load(f)
      end

    raise "no configuration for environment: #{environment}" unless env_config = all_db_config[environment]
    db_config.merge!(env_config)
  end
  #args take precedent over config
  db_config.merge! options.delete(:connection) if options[:connection]

  @db = Mongo::Connection.new(db_config['host'], db_config['port']).
	db(db_config['database'])
  if db_config['username'] && db_config['password']
    @db.authenticate(db_config['username'], db_config['password'])
  end

  unless get('configurations','engine')
    put(options.merge('type'=>'configurations', '_id'=>'engine')) 
  end
end

Instance Method Details

#add_type(type) ⇒ Object



143
144
145
146
147
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 143

def add_type(type)
  synchronize do
    get_collection(type).create_index("_id")
  end
end

#close_connectionObject



39
40
41
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 39

def close_connection()
  @db.connection.close()
end

#delete(doc) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 83

def delete(doc)
  drev = doc['_rev']
  raise ArgumentError.new("can't delete doc without _rev") unless drev
  synchronize do
    raise "doc must have a type" unless doc['type']
    prev = get(doc['type'], doc['_id'])
    return true if prev.nil?
    doc['_rev'] ||= 0
    if prev['_rev'] == drev
      get_collection(doc['type']).remove("_id" => doc["_id"])
      nil
    else
      prev
    end
  end
end

#get(type, key) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 75

def get(type, key)
  synchronize do
    doc = get_collection(type).find_one("_id" => key)
    from_mongo doc if doc
    doc
  end
end

#get_many(type, key = nil, opts = {}) ⇒ Object



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
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 100

def get_many(type, key=nil, opts={})
  synchronize do
    return get_collection(type).count if opts[:count]
    criteria = {}
    find_opts = {}
    find_opts[:limit] = opts[:limit] if opts[:limit]
    find_opts[:skip] = opts[:skip] if opts[:skip]
    find_opts[:sort] = ["_id", opts[:descending] ? :descending : :ascending]
    if key
      id_criteria = Array(key).map do |k|
        case k.class.to_s
        when "String" then "!#{k}$"
        when "Regexp" then k.source
        else k.to_s
        end
      end
      criteria = {"_id" => /#{id_criteria.join "|"}/}
    end
    docs = get_collection(type).find(criteria, find_opts).to_a
    docs.each do |doc|
      from_mongo doc
    end
    docs
  end
end

#ids(type) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 126

def ids(type)
  synchronize do
    result = get_collection(type).find({}, {:fields=>["_id"]}).map do |row|
      row["_id"].to_s
    end
    result.sort
  end
end

#purge!Object



135
136
137
138
139
140
141
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 135

def purge!
  synchronize do
    @db.collection_names.each do |name| 
      @db.drop_collection(name) if name =~ /^#{@@collection_prefix}/
    end
  end
end

#purge_type!(type) ⇒ Object



149
150
151
152
153
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 149

def purge_type!(type)
  synchronize do
    @db.drop_collection(@@collection_prefix + type)
  end
end

#put(doc, opts = {}) ⇒ Object



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
70
71
72
73
# File 'lib/ruote-mongodb/mongodb_storage.rb', line 43

def put(doc, opts={})
  synchronize do
    raise "doc must have a type" unless doc['type']
    raise "doc must have an ID" unless doc['_id']
    pre = get(doc['type'], doc['_id'])

    if pre && pre['_rev'] != doc['_rev']
      return pre
    end

    if pre.nil? && doc['_rev']
      return true
    end

    doc = if opts[:update_rev]
            doc['_rev'] = pre ? pre['_rev'] : -1
            doc
          else
            doc.merge('_rev' => doc['_rev'] || -1)
          end

    doc['put_at'] = Ruote.now_to_utc_s
    doc['_rev'] = doc['_rev'] + 1

    encoded_doc = Rufus::Json.dup(doc)
    to_mongo encoded_doc
    get_collection(doc['type']).save(encoded_doc)

    nil
  end
end