CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/anemone/storage/tokyo_cabinet.rb
Views: 11778
1
begin
2
require 'tokyocabinet'
3
rescue LoadError
4
puts "You need the tokyocabinet gem to use Anemone::Storage::TokyoCabinet"
5
exit
6
end
7
8
require 'forwardable'
9
10
module Anemone
11
module Storage
12
class TokyoCabinet
13
extend Forwardable
14
15
def_delegators :@db, :close, :size, :keys, :has_key?
16
17
def initialize(file)
18
raise "TokyoCabinet filename must have .tch extension" if File.extname(file) != '.tch'
19
@db = ::TokyoCabinet::HDB::new
20
@db.open(file, ::TokyoCabinet::HDB::OWRITER | ::TokyoCabinet::HDB::OCREAT)
21
@db.clear
22
end
23
24
def [](key)
25
if value = @db[key]
26
load_value(value)
27
end
28
end
29
30
def []=(key, value)
31
@db[key] = [Marshal.dump(value)].pack("m")
32
end
33
34
def delete(key)
35
value = self[key]
36
@db.delete(key)
37
value
38
end
39
40
def each
41
@db.each { |k, v| yield k, load_value(v) }
42
end
43
44
def merge!(hash)
45
hash.each { |key, value| self[key] = value }
46
self
47
end
48
49
private
50
51
def load_value(value)
52
Marshal.load(value.unpack("m")[0])
53
end
54
55
end
56
end
57
end
58
59