Class: PEROBS::FlatFileDB

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

Overview

The FlatFileDB is a storage backend that uses a single flat file to store the value blobs.

Constant Summary collapse

VERSION =

This version number increases whenever the on-disk format changes in a way that requires conversion actions after an update.

1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataBase

#check_option, #deserialize, #serialize

Constructor Details

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

Create a new FlatFileDB object.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/perobs/FlatFileDB.rb', line 52

def initialize(db_name, options = {})
  super(options[:serializer] || :json)

  @db_dir = db_name
  # Create the database directory if it doesn't exist yet.
  ensure_dir_exists(@db_dir)
  PEROBS.log.open(File.join(@db_dir, 'log'))
  check_version

  # Read the existing DB config.
  @config = get_hash('config')
  check_option('serializer')

  put_hash('config', @config)
end

Instance Attribute Details

#max_blob_sizeObject (readonly)

Returns the value of attribute max_blob_size.



45
46
47
# File 'lib/perobs/FlatFileDB.rb', line 45

def max_blob_size
  @max_blob_size
end

Class Method Details

.delete_db(db_name) ⇒ Object



88
89
90
# File 'lib/perobs/FlatFileDB.rb', line 88

def FlatFileDB::delete_db(db_name)
  FileUtils.rm_rf(db_name)
end

Instance Method Details

#check(id, repair) ⇒ TrueClass/FalseClass

Check if the stored object is syntactically correct.



194
195
196
197
198
199
200
201
202
203
# File 'lib/perobs/FlatFileDB.rb', line 194

def check(id, repair)
  begin
    get_object(id)
  rescue => e
    PEROBS.log.warn "Cannot read object with ID #{id}: #{e.message}"
    return false
  end

  true
end

#check_db(repair = false) ⇒ Object

Basic consistency check.



181
182
183
184
185
186
# File 'lib/perobs/FlatFileDB.rb', line 181

def check_db(repair = false)
  t = Time.now
  PEROBS.log.info "check_db started"
  @flat_file.check(repair)
  PEROBS.log.info "check_db completed in #{Time.now - t} seconds"
end

#clear_marksObject

This method must be called to initiate the marking process.



146
147
148
149
150
151
# File 'lib/perobs/FlatFileDB.rb', line 146

def clear_marks
  t = Time.now
  PEROBS.log.info "Clearing all marks"
  @flat_file.clear_all_marks
  PEROBS.log.info "All marks cleared in #{Time.now - t} seconds"
end

#closeObject

Close the FlatFileDB.



76
77
78
79
80
# File 'lib/perobs/FlatFileDB.rb', line 76

def close
  @flat_file.close
  @flat_file = nil
  PEROBS.log.info "FlatFile closed"
end

#delete_databaseObject

Delete the entire database. The database is no longer usable after this method was called.



84
85
86
# File 'lib/perobs/FlatFileDB.rb', line 84

def delete_database
  FileUtils.rm_rf(@db_dir)
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.



156
157
158
159
160
161
162
# File 'lib/perobs/FlatFileDB.rb', line 156

def delete_unmarked_objects
  t = Time.now
  PEROBS.log.info "Deleting unmarked objects"
  retval = @flat_file.delete_unmarked_objects
  PEROBS.log.info "Unmarked objects deleted in #{Time.now - t} seconds"
  retval
end

#get_hash(name) ⇒ Hash

Load the Hash with the given name.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/perobs/FlatFileDB.rb', line 114

def get_hash(name)
  file_name = File.join(@db_dir, name + '.json')
  return ::Hash.new unless File.exist?(file_name)

  begin
    json = File.read(file_name)
  rescue => e
    PEROBS.log.fatal "Cannot read hash file '#{file_name}': #{e.message}"
  end
  JSON.parse(json, :create_additions => true)
end

#get_object(id) ⇒ Hash

Load the given object from the filesystem.



137
138
139
140
141
142
143
# File 'lib/perobs/FlatFileDB.rb', line 137

def get_object(id)
  if (raw_obj = @flat_file.read_obj_by_id(id))
    return deserialize(raw_obj)
  else
    nil
  end
end

#include?(id) ⇒ Boolean

Return true if the object with given ID exists



94
95
96
# File 'lib/perobs/FlatFileDB.rb', line 94

def include?(id)
  !@flat_file.find_obj_addr_by_id(id).nil?
end

#is_marked?(id, ignore_errors = false) ⇒ Boolean

Check if the object is marked.



174
175
176
# File 'lib/perobs/FlatFileDB.rb', line 174

def is_marked?(id, ignore_errors = false)
  @flat_file.is_marked_by_id?(id)
end

#mark(id) ⇒ Object

Mark an object.



166
167
168
# File 'lib/perobs/FlatFileDB.rb', line 166

def mark(id)
  @flat_file.mark_obj_by_id(id)
end

#openObject

Open the FlatFileDB for transactions.



69
70
71
72
73
# File 'lib/perobs/FlatFileDB.rb', line 69

def open
  @flat_file = FlatFile.new(@db_dir)
  @flat_file.open
  PEROBS.log.info "FlatFile opened"
end

#put_hash(name, hash) ⇒ Object

Store a simple Hash as a JSON encoded file into the DB directory. numbers.



102
103
104
105
106
107
108
109
# File 'lib/perobs/FlatFileDB.rb', line 102

def put_hash(name, hash)
  file_name = File.join(@db_dir, name + '.json')
  begin
    File.write(file_name, hash.to_json)
  rescue => e
    PEROBS.log.fatal "Cannot write hash file '#{file_name}': #{e.message}"
  end
end

#put_object(obj, id) ⇒ Object

Store the given object into the cluster files.



128
129
130
131
# File 'lib/perobs/FlatFileDB.rb', line 128

def put_object(obj, id)
  @flat_file.delete_obj_by_id(id)
  @flat_file.write_obj_by_id(id, serialize(obj))
end

#put_raw_object(raw, id) ⇒ Object

Store the given serialized object into the cluster files. This method is for internal use only!



209
210
211
212
# File 'lib/perobs/FlatFileDB.rb', line 209

def put_raw_object(raw, id)
  @flat_file.delete_obj_(id)
  @flat_file.write_obj_by_id(id, raw)
end