Class: FakeDynamo::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_dynamo/storage.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



21
22
23
# File 'lib/fake_dynamo/storage.rb', line 21

def initialize
  init_db
end

Class Attribute Details

.db_pathObject

Returns the value of attribute db_path.



10
11
12
# File 'lib/fake_dynamo/storage.rb', line 10

def db_path
  @db_path
end

Instance Attribute Details

#compactedObject

Returns the value of attribute compacted.



7
8
9
# File 'lib/fake_dynamo/storage.rb', line 7

def compacted
  @compacted
end

#loadedObject

Returns the value of attribute loaded.



7
8
9
# File 'lib/fake_dynamo/storage.rb', line 7

def loaded
  @loaded
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/fake_dynamo/storage.rb', line 12

def instance
  @storage ||= Storage.new
end

Instance Method Details

#compact!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fake_dynamo/storage.rb', line 104

def compact!
  return if @compacted
  @aof = Tempfile.new('compact')
  log.warn "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_necessaryObject



97
98
99
100
101
102
# File 'lib/fake_dynamo/storage.rb', line 97

def compact_if_necessary
  return unless File.exists? db_path
  if File.stat(db_path).size > compact_threshold
    compact!
  end
end

#compact_thresholdObject



93
94
95
# File 'lib/fake_dynamo/storage.rb', line 93

def compact_threshold
  100 * 1024 * 1024 # 100mb
end

#dbObject



55
56
57
# File 'lib/fake_dynamo/storage.rb', line 55

def db
  DB.instance
end

#db_aofObject



59
60
61
# File 'lib/fake_dynamo/storage.rb', line 59

def db_aof
  @aof ||= File.new(db_path, 'a')
end

#db_pathObject



33
34
35
# File 'lib/fake_dynamo/storage.rb', line 33

def db_path
  self.class.db_path
end

#delete_dbObject



43
44
45
46
# File 'lib/fake_dynamo/storage.rb', line 43

def delete_db
  return unless File.exists? db_path
  FileUtils.rm(db_path)
end

#init_dbObject



37
38
39
40
41
# File 'lib/fake_dynamo/storage.rb', line 37

def init_db
  return if File.exists? db_path
  FileUtils.mkdir_p(File.dirname(db_path))
  FileUtils.touch(db_path)
end

#load_aofObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fake_dynamo/storage.rb', line 77

def load_aof
  return if @loaded
  file = File.new(db_path, 'r')
  log.warn "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

#logObject



17
18
19
# File 'lib/fake_dynamo/storage.rb', line 17

def log
  Logger.log
end

#persist(operation, data) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/fake_dynamo/storage.rb', line 68

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

#resetObject



48
49
50
51
52
53
# File 'lib/fake_dynamo/storage.rb', line 48

def reset
  log.warn "resetting database ..."
  @aof.close if @aof
  @aof = nil
  delete_db
end

#shutdownObject



63
64
65
66
# File 'lib/fake_dynamo/storage.rb', line 63

def shutdown
  log.warn "shutting down fake_dynamo ..."
  @aof.close if @aof
end

#write_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/fake_dynamo/storage.rb', line 29

def write_command?(command)
  write_commands.include?(command)
end

#write_commandsObject



25
26
27
# File 'lib/fake_dynamo/storage.rb', line 25

def write_commands
  %w[CreateTable DeleteItem DeleteTable PutItem UpdateItem UpdateTable BatchWriteItem]
end