Class: FakeDynamo::Storage
- Inherits:
-
Object
- Object
- FakeDynamo::Storage
- Defined in:
- lib/fake_dynamo/storage.rb
Instance Attribute Summary collapse
-
#compacted ⇒ Object
Returns the value of attribute compacted.
-
#loaded ⇒ Object
Returns the value of attribute loaded.
Class Method Summary collapse
Instance Method Summary collapse
- #compact! ⇒ Object
- #compact_if_necessary ⇒ Object
- #compact_threshold ⇒ Object
- #db ⇒ Object
- #db_aof ⇒ Object
- #db_path ⇒ Object
- #delete_db ⇒ Object
- #init_db ⇒ Object
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
- #load_aof ⇒ Object
- #persist(operation, data) ⇒ Object
- #shutdown ⇒ Object
- #write_command?(command) ⇒ Boolean
- #write_commands ⇒ Object
Constructor Details
#initialize ⇒ Storage
Returns a new instance of Storage.
15 16 17 |
# File 'lib/fake_dynamo/storage.rb', line 15 def initialize init_db end |
Instance Attribute Details
#compacted ⇒ Object
Returns the value of attribute compacted.
7 8 9 |
# File 'lib/fake_dynamo/storage.rb', line 7 def compacted @compacted end |
#loaded ⇒ Object
Returns the value of attribute loaded.
7 8 9 |
# File 'lib/fake_dynamo/storage.rb', line 7 def loaded @loaded end |
Class Method Details
Instance Method Details
#compact! ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/fake_dynamo/storage.rb', line 91 def compact! return if @compacted @aof = Tempfile.new('compact') puts "Compacting db ..." db.tables.each do |_, table| persist('CreateTable', table.create_table_data) table.items.each do |_, item| persist('PutItem', table.put_item_data(item)) end end @aof.close FileUtils.mv(@aof.path, db_path) @aof = nil @compacted = true end |
#compact_if_necessary ⇒ Object
84 85 86 87 88 89 |
# File 'lib/fake_dynamo/storage.rb', line 84 def compact_if_necessary return unless File.exists? db_path if File.stat(db_path).size > compact_threshold compact! end end |
#compact_threshold ⇒ Object
80 81 82 |
# File 'lib/fake_dynamo/storage.rb', line 80 def compact_threshold 100 * 1024 * 1024 # 100mb end |
#db_aof ⇒ Object
46 47 48 |
# File 'lib/fake_dynamo/storage.rb', line 46 def db_aof @aof ||= File.new(db_path, 'a') end |
#db_path ⇒ Object
27 28 29 |
# File 'lib/fake_dynamo/storage.rb', line 27 def db_path '/usr/local/var/fake_dynamo/db.fdb' end |
#delete_db ⇒ Object
37 38 39 40 |
# File 'lib/fake_dynamo/storage.rb', line 37 def delete_db return unless File.exists? db_path FileUtils.rm(db_path) end |
#init_db ⇒ Object
31 32 33 34 35 |
# File 'lib/fake_dynamo/storage.rb', line 31 def init_db return if File.exists? db_path FileUtils.mkdir_p(File.dirname(db_path)) FileUtils.touch(db_path) end |
#load_aof ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fake_dynamo/storage.rb', line 64 def load_aof return if @loaded file = File.new(db_path, 'r') puts "Loading fake_dynamo data ..." loop do operation = file.readline.chomp size = Integer(file.readline.chomp) data = file.read(size) db.process(operation, JSON.parse(data)) end rescue EOFError file.close compact_if_necessary @loaded = true end |
#persist(operation, data) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/fake_dynamo/storage.rb', line 55 def persist(operation, data) return unless write_command?(operation) db_aof.puts(operation) data = data.to_json db_aof.puts(data.bytesize + "\n".bytesize) db_aof.puts(data) db_aof.flush end |
#shutdown ⇒ Object
50 51 52 53 |
# File 'lib/fake_dynamo/storage.rb', line 50 def shutdown puts "shutting down fake_dynamo ..." @aof.close if @aof end |
#write_command?(command) ⇒ Boolean
23 24 25 |
# File 'lib/fake_dynamo/storage.rb', line 23 def write_command?(command) write_commands.include?(command) end |
#write_commands ⇒ Object
19 20 21 |
# File 'lib/fake_dynamo/storage.rb', line 19 def write_commands %w[CreateTable DeleteItem DeleteTable PutItem UpdateItem UpdateTable] end |