Class: LevelDb::Db

Inherits:
Object
  • Object
show all
Includes:
Crud
Defined in:
lib/level_db.rb

Constant Summary collapse

NUM_FILES_AT_LEVEL_FORMAT =
'leveldb.num-files-at-level%d'.freeze
MAX_ITERATIONS =
10
ConvergenceError =
Class.new(Error)

Instance Method Summary collapse

Methods included from Crud

#delete, #get, #put

Methods included from Encoding

#decode_key, #decode_value, #encode_key, #encode_value

Constructor Details

#initialize(db) ⇒ Db

Returns a new instance of Db.



89
90
91
# File 'lib/level_db.rb', line 89

def initialize(db)
  @db = db
end

Instance Method Details

#batch(&block) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/level_db.rb', line 97

def batch(&block)
  batch = @db.create_write_batch
  begin
    yield Batch.new(batch)
    @db.write(batch)
  ensure
    batch.close
  end
end

#closeObject



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

def close
  @db.close
end

#compact_range(options = {}) ⇒ Object



117
118
119
120
121
# File 'lib/level_db.rb', line 117

def compact_range(options={})
  from = options[:from]
  to = options[:to]
  @db.compact_range(encode_key(from), encode_key(to))
end

#each(options = {}, &block) ⇒ Object



107
108
109
110
111
# File 'lib/level_db.rb', line 107

def each(options={}, &block)
  cursor = Cursor.new(@db.iterator, options)
  cursor.each(&block) if block_given?
  cursor
end

#full_compaction(options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/level_db.rb', line 123

def full_compaction(options={})
  pre_compaction_files_per_level = nil
  i = 0
  until pre_compaction_files_per_level == files_per_level
    if i < options.fetch(:max_iterations, MAX_ITERATIONS)
      pre_compaction_files_per_level = files_per_level
      compact_range
      i += 1
    else
      raise ConvergenceError, "Failed to reach convergence in #{i} iterations"
    end
  end
end

#snapshotObject



113
114
115
# File 'lib/level_db.rb', line 113

def snapshot
  Snapshot.new(@db)
end