Class: PEROBS::DynamoDB

Inherits:
DataBase show all
Defined in:
lib/perobs/DynamoDB.rb

Overview

This class implements an Amazon DynamoDB storage engine for PEROBS.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataBase

#check_option, #deserialize, #serialize

Constructor Details

#initialize(db_name, options = {}) ⇒ DynamoDB

Create a new DynamoDB object.

Parameters:

  • db_name (String)

    name of the DB directory

  • options (Hash) (defaults to: {})

    options to customize the behavior. Currently only the following options are supported: :serializer : Can be :json and :yaml :aws_id : AWS credentials ID :aws_key : AWS credentials key :aws_region : AWS region to host the data



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, options = {})
  # :marshal serialization results in a binary format that cannot easily
  # be stored in DynamoDB. We fall back to :yaml.
  if options[:serializer] == :marshal
    options[:serializer] = :yaml
  end

  super(options[:serializer] || :json)

  if options.include?(:aws_id) && options.include?(:aws_key)
    Aws.config[:credentials] = Aws::Credentials.new(options[:aws_id],
                                                    options[:aws_key])
  end
  if options.include?(:aws_region)
    Aws.config[:region] = options[: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

.delete_db(table_name) ⇒ Object



81
82
83
84
85
# File 'lib/perobs/DynamoDB.rb', line 81

def DynamoDB::delete_db(table_name)
  dynamodb = Aws::DynamoDB::Client.new
  dynamodb.delete_table(:table_name => table_name)
  dynamodb.wait_until(:table_not_exists, table_name: table_name)
end

Instance Method Details

#check(id, repair) ⇒ TrueClass/FalseClass

Check if the stored object is syntactically correct.

Parameters:

  • id (Fixnum/Bignum)

    Object ID

  • repair (TrueClass/FalseClass)

    True if an repair attempt should be made.

Returns:

  • (TrueClass/FalseClass)

    True if the object is OK, otherwise false.



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.message}"
    return false
  end

  true
end

#check_db(repair = false) ⇒ Object

Basic consistency check.

Parameters:

  • repair (TrueClass/FalseClass) (defaults to: false)

    True if found errors should be repaired.



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_marksObject

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_databaseObject

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_objectsArray

Permanently delete all objects that have not been marked. Those are orphaned and are no longer referenced by any actively used object.

Returns:

  • (Array)

    List of object IDs of the deleted objects.



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.

Parameters:

  • name (String)

    Name of the hash.

Returns:

  • (Hash)

    A Hash that maps String objects to strings or numbers.



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.

Parameters:

  • id (Fixnum or Bignum)

    object ID

Returns:

  • (Hash)

    Object as defined by PEROBS::ObjectBase or nil if ID does not exist



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

Parameters:

  • id (Fixnum or Bignum)

Returns:

  • (Boolean)


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.

Parameters:

  • id (Fixnum or Bignum)

    ID of the object to check

Returns:

  • (Boolean)


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.

Parameters:

  • id (Fixnum or Bignum)

    ID of the object to mark



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.

Parameters:

  • name (String)

    Name of the hash. Will be used as file name.

  • hash (Hash)

    A Hash that maps String objects to strings or



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.

Parameters:

  • obj (Hash)

    Object as defined by PEROBS::ObjectBase



114
115
116
# File 'lib/perobs/DynamoDB.rb', line 114

def put_object(obj, id)
  dynamo_put_item(id.to_s, serialize(obj))
end