Class: MongoPersistedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/util/mongo_persisted_hash.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(hostname, db, collection, name) ⇒ MongoPersistedHash

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

Parameters:

  • hostname (String)

    of the MongoDB server

  • db (String)

    the database where we want to persist the array

  • collection (String)

    the collection we are using to persist this collection

  • name (String)

    the _id of the document we are using to persist this Hash



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#[](key) ⇒ Object

Methods borrowed from Ruby’s Hash class



38
39
40
41
# File 'lib/util/mongo_persisted_hash.rb', line 38

def []( key )
  hash = load_hash
  hash[key]
end

#[]=(key, val) ⇒ Object



43
44
45
46
47
# 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) ⇒ Boolean

Adds a <key, value> pair to the PersistedHash _only if_ there is currently no value associated with the specified key. <key, value> pair was added successfully

Returns:

  • (Boolean)

    false if the key already exists, true if the



102
103
104
105
106
107
108
# 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) ⇒ Object



49
50
51
52
53
54
# 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) ⇒ Boolean

Removes a <key, value> pair from the PersistedHash _only if_ there is currently a value associated with the specified key. the specified key, true otherwise

Returns:

  • (Boolean)

    false if there is no value associated with



114
115
116
117
118
119
# 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?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/util/mongo_persisted_hash.rb', line 56

def empty?
  hash = load_hash
  hash.delete '_id'
  hash.empty?
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/util/mongo_persisted_hash.rb', line 62

def has_key?( key )
  hash = load_hash
  hash.has_key? key
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/util/mongo_persisted_hash.rb', line 67

def include?( key )
  has_key? key
end

#keysObject



83
84
85
86
87
# File 'lib/util/mongo_persisted_hash.rb', line 83

def keys
  hash = load_hash
  hash.delete '_id'
  hash.keys
end

#lengthObject



89
90
91
92
93
# File 'lib/util/mongo_persisted_hash.rb', line 89

def length
  hash = load_hash
  hash.delete '_id'
  hash.length
end

#load_hashObject

private



124
125
126
127
128
129
130
131
132
133
134
# 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

#saveObject



146
147
148
149
150
151
152
# 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) ⇒ Object

Enable/disable auto save



31
32
33
# File 'lib/util/mongo_persisted_hash.rb', line 31

def set_auto_save(as)
  @auto_save = as
end

#store_hash(hash) ⇒ Object



137
138
139
140
141
142
143
144
# 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_hObject



77
78
79
80
81
# File 'lib/util/mongo_persisted_hash.rb', line 77

def to_h
  hash = load_hash
  hash.delete '_id'
  hash
end

#to_sObject



71
72
73
74
75
# File 'lib/util/mongo_persisted_hash.rb', line 71

def to_s
  hash = load_hash
  hash.delete '_id'
  hash.to_s
end