/
leveldb.rb
102 lines (84 loc) · 2.49 KB
/
leveldb.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'leveldb/leveldb' # the c extension
module LevelDB
class DB
include Enumerable
class << self
## Loads or creates a LevelDB database as necessary, stored on disk at
## +pathname+.
##
## See #make for possible options.
def new pathname, options={}
make path_string(pathname),
options.merge(:create_if_missing => true,
:error_if_exists => false)
end
## Creates a new LevelDB database stored on disk at +pathname+. Throws an
## exception if the database already exists.
##
## See #make for possible options.
def create pathname, options={}
make path_string(pathname),
options.merge(:create_if_missing => true,
:error_if_exists => true)
end
## Loads a LevelDB database stored on disk at +pathname+. Throws an
## exception unless the database already exists.
def load pathname
make path_string(pathname),
{ :create_if_missing => false, :error_if_exists => false }
end
private
## Coerces the argument into a String for use as a filename/-path
def path_string pathname
File.respond_to?(:path) ? File.path(pathname) : pathname.to_str
end
end
attr_reader :pathname
attr_reader :options
alias :includes? :exists?
alias :contains? :exists?
alias :member? :exists?
alias :[] :get
alias :[]= :put
alias :close! :close
def each(*args, &block)
i = iterator(*args)
i.each(&block) if block
i
end
def iterator(*args); Iterator.new self, *args end
def keys; map { |k, v| k } end
def values; map { |k, v| v } end
def inspect
%(<#{self.class} #{@pathname.inspect}>)
end
end
class Iterator
include Enumerable
attr_reader :db, :from, :to
def self.new(db, opts={})
make db, opts
end
def reversed?; @reversed end
def inspect
%(<#{self.class} #{@db.inspect} @from=#{@from.inspect} @to=#{@to.inspect}#{' (reversed)' if @reversed}>)
end
end
class WriteBatch
class << self
private :new
end
end
class Options
DEFAULT_MAX_OPEN_FILES = 1000
DEFAULT_WRITE_BUFFER_SIZE = 4 * 1024 * 1024
DEFAULT_BLOCK_SIZE = 4 * 1024
DEFAULT_BLOCK_RESTART_INTERVAL = 16
DEFAULT_COMPRESSION = LevelDB::CompressionType::SnappyCompression
attr_reader :create_if_missing, :error_if_exists,
:block_cache_size, :paranoid_checks,
:write_buffer_size, :max_open_files,
:block_size, :block_restart_interval,
:compression
end
end # module LevelDB