class MongoPersistedHash

An hash that is automatically persisted to a MongoDB document. This class behaves similarly to a regular Hash but it persists every operation to a specified MongoDB document. Not all Hash operations are supported and we added some of our own.

Public Class Methods

new( hostname, db, collection, name ) click to toggle source

Creates a new MongoPersistedHash that is persisted as a document with _id name inside a MongoDB collection

@param [String] hostname of the MongoDB server @param [String] db the database where we want to persist the array @param [String] collection the collection we are using to persist this collection @param [String] name the _id of the document we are using to persist this Hash

# File lib/util/mongo_persisted_hash.rb, line 16
def initialize(  hostname, db, collection, name )
  Mongo::Logger.logger.level = ::Logger::INFO
  client = Mongo::Client.new([hostname], :database => db)
  @collection = client[collection]
  @doc_id = name

  # Semaphore for the write on DB synchronization
  @s = Mutex.new

  # Enable / disable auto save
  @auto_save = true;

end

Public Instance Methods

[]( key ) click to toggle source

Methods borrowed from Ruby's Hash class

# File lib/util/mongo_persisted_hash.rb, line 38
def []( key )
  hash = load_hash
  hash[key]
end
[]=( key, val ) click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 43
def []=( key, val )
  hash = load_hash
  hash[key]=val
  store_hash hash
end
add_key_value?(key, val) click to toggle source

Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key. @return [Boolean] false if the key already exists, true if the <key, value> pair was added successfully

# File lib/util/mongo_persisted_hash.rb, line 102
def add_key_value?(key, val)
  hash = load_hash
  return false if hash.key? key
  hash[key] = val
  store_hash hash
  true
end
delete( key ) click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 49
def delete( key )
  hash = load_hash
  return_value = hash.delete key
  store_hash hash
  return_value
end
delete_key_value?( key ) click to toggle source

Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key. @return [Boolean] false if there is no value associated with the specified key, true otherwise

# File lib/util/mongo_persisted_hash.rb, line 114
def delete_key_value?( key )
  hash = load_hash
  return false if hash.delete(key).nil?
  store_hash hash
  true
end
empty?() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 56
def empty?
  hash = load_hash
  hash.delete '_id'
  hash.empty?
end
has_key?( key ) click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 62
def has_key?( key )
  hash = load_hash
  hash.has_key? key
end
include?( key ) click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 67
def include?( key )
  has_key? key
end
keys() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 83
def keys
  hash = load_hash
  hash.delete '_id'
  hash.keys
end
length() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 89
def length
  hash = load_hash
  hash.delete '_id'
  hash.length
end
load_hash() click to toggle source

private

# File lib/util/mongo_persisted_hash.rb, line 124
def load_hash
  if(!defined? @r)
    @s.synchronize {
      @r = @collection.find({_id: @doc_id}).limit(1).first
    }
  end
  if(@r.nil?)
      @r = {'_id' => @doc_id};
  end
  @r
end
save() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 146
def save
  @s.synchronize {
    if(defined? @r and !@r.empty?)
      @collection.find({'_id' => @doc_id}).find_one_and_replace(@r, :upsert => :true)
    end
  }
end
set_auto_save(as) click to toggle source

Enable/disable auto save

# File lib/util/mongo_persisted_hash.rb, line 31
def set_auto_save(as)
  @auto_save = as
end
store_hash(hash) click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 137
def store_hash(hash)
  @s.synchronize {
    @r = hash
    if(@auto_save and !@r.empty?)
      @collection.find({'_id' => @doc_id}).find_one_and_replace(@r, :upsert => :true)
    end
  }
end
to_h() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 77
def to_h
  hash = load_hash
  hash.delete '_id'
  hash
end
to_s() click to toggle source
# File lib/util/mongo_persisted_hash.rb, line 71
def to_s
  hash = load_hash
  hash.delete '_id'
  hash.to_s
end