Class: PEROBS::DynamoDB
Overview
This class implements an Amazon DynamoDB storage engine for PEROBS.
Class Method Summary collapse
Instance Method Summary collapse
-
#check(id, repair) ⇒ TrueClass/FalseClass
Check if the stored object is syntactically correct.
-
#check_db(repair = false) ⇒ Object
Basic consistency check.
-
#clear_marks ⇒ Object
This method must be called to initiate the marking process.
-
#delete_database ⇒ Object
Delete the entire database.
-
#delete_unmarked_objects ⇒ Array
Permanently delete all objects that have not been marked.
-
#get_hash(name) ⇒ Hash
Load the Hash with the given name.
-
#get_object(id) ⇒ Hash
Load the given object from the filesystem.
-
#include?(id) ⇒ Boolean
Return true if the object with given ID exists.
-
#initialize(db_name, options = {}) ⇒ DynamoDB
constructor
Create a new DynamoDB object.
-
#is_marked?(id) ⇒ Boolean
Check if the object is marked.
-
#mark(id) ⇒ Object
Mark an object.
-
#put_hash(name, hash) ⇒ Object
Store a simple Hash as a JSON encoded file into the DB directory.
-
#put_object(obj, id) ⇒ Object
Store the given object into the cluster files.
Methods inherited from DataBase
#check_option, #deserialize, #serialize
Constructor Details
#initialize(db_name, options = {}) ⇒ DynamoDB
Create a new DynamoDB object.
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 |
# File 'lib/perobs/DynamoDB.rb', line 46 def initialize(db_name, = {}) # :marshal serialization results in a binary format that cannot easily # be stored in DynamoDB. We fall back to :yaml. if [:serializer] == :marshal [:serializer] = :yaml end super([:serializer] || :json) if .include?(:aws_id) && .include?(:aws_key) Aws.config[:credentials] = Aws::Credentials.new([:aws_id], [:aws_key]) end if .include?(:aws_region) Aws.config[:region] = [:aws_region] end @dynamodb = Aws::DynamoDB::Client.new @table_name = db_name ensure_table_exists(@table_name) # Read the existing DB config. @config = get_hash('config') check_option('serializer') put_hash('config', @config) end |
Class Method Details
Instance Method Details
#check(id, repair) ⇒ TrueClass/FalseClass
Check if the stored object is syntactically correct.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/perobs/DynamoDB.rb', line 175 def check(id, repair) begin get_object(id) rescue => e $stderr.puts "Cannot read object with ID #{id}: #{e.}" return false end true end |
#check_db(repair = false) ⇒ Object
Basic consistency check.
165 166 167 |
# File 'lib/perobs/DynamoDB.rb', line 165 def check_db(repair = false) # TODO: See if we can add checks here end |
#clear_marks ⇒ Object
This method must be called to initiate the marking process.
127 128 129 130 131 132 133 |
# File 'lib/perobs/DynamoDB.rb', line 127 def clear_marks each_item do |id| dynamo_mark_item(id, false) end # Mark the 'config' item so it will not get deleted. dynamo_mark_item('config') end |
#delete_database ⇒ Object
Delete the entire database. The database is no longer usable after this method was called.
75 76 77 78 79 |
# File 'lib/perobs/DynamoDB.rb', line 75 def delete_database dynamodb = Aws::DynamoDB::Client.new dynamodb.delete_table(:table_name => @table_name) dynamodb.wait_until(:table_not_exists, table_name: @table_name) end |
#delete_unmarked_objects ⇒ Array
Permanently delete all objects that have not been marked. Those are orphaned and are no longer referenced by any actively used object.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/perobs/DynamoDB.rb', line 138 def delete_unmarked_objects deleted_ids = [] each_item do |id| unless dynamo_is_marked?(id) dynamo_delete_item(id) deleted_ids << id end end deleted_ids end |
#get_hash(name) ⇒ Hash
Load the Hash with the given name.
104 105 106 107 108 109 110 |
# File 'lib/perobs/DynamoDB.rb', line 104 def get_hash(name) if (item = dynamo_get_item(name)) JSON.parse(item) else ::Hash.new end end |
#get_object(id) ⇒ Hash
Load the given object from the filesystem.
122 123 124 |
# File 'lib/perobs/DynamoDB.rb', line 122 def get_object(id) (item = dynamo_get_item(id.to_s)) ? deserialize(item) : nil end |
#include?(id) ⇒ Boolean
Return true if the object with given ID exists
89 90 91 |
# File 'lib/perobs/DynamoDB.rb', line 89 def include?(id) !dynamo_get_item(id.to_s).nil? end |
#is_marked?(id) ⇒ Boolean
Check if the object is marked.
158 159 160 |
# File 'lib/perobs/DynamoDB.rb', line 158 def is_marked?(id) dynamo_is_marked?(id.to_s) end |
#mark(id) ⇒ Object
Mark an object.
152 153 154 |
# File 'lib/perobs/DynamoDB.rb', line 152 def mark(id) dynamo_mark_item(id.to_s, true) end |
#put_hash(name, hash) ⇒ Object
Store a simple Hash as a JSON encoded file into the DB directory. numbers.
97 98 99 |
# File 'lib/perobs/DynamoDB.rb', line 97 def put_hash(name, hash) dynamo_put_item(name, hash.to_json) end |
#put_object(obj, id) ⇒ Object
Store the given object into the cluster files.
114 115 116 |
# File 'lib/perobs/DynamoDB.rb', line 114 def put_object(obj, id) dynamo_put_item(id.to_s, serialize(obj)) end |