hashtree 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/benchmarks.rb +8 -0
- data/lib/hashtree/version.rb +1 -1
- data/lib/hashtree.rb +27 -34
- metadata +1 -1
data/examples/benchmarks.rb
CHANGED
@@ -80,6 +80,14 @@ Benchmark.bm do |x|
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
puts "Doing 100 non-indexed queries"
|
84
|
+
x.report do
|
85
|
+
100.times do |i|
|
86
|
+
query = proc{ |key,doc| doc[:email] == "john.doe.#{rand(100000)}@example.com" }
|
87
|
+
@htree.select{ |key, doc| query.call(key,doc) }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
83
91
|
puts "Deleting 1.000 random documents"
|
84
92
|
x.report do
|
85
93
|
lhtree = @htree.clone
|
data/lib/hashtree/version.rb
CHANGED
data/lib/hashtree.rb
CHANGED
@@ -18,30 +18,21 @@ class HashTree
|
|
18
18
|
}
|
19
19
|
end
|
20
20
|
|
21
|
-
# Public API
|
22
21
|
def [](key)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
else
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
rescue
|
22
|
+
id = @index[key]
|
23
|
+
if id
|
24
|
+
read(id)
|
25
|
+
else
|
31
26
|
nil
|
32
27
|
end
|
33
28
|
end
|
34
29
|
|
35
30
|
def []=(key, value)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
create(key, value)
|
42
|
-
end
|
43
|
-
rescue
|
44
|
-
nil
|
31
|
+
id = @index[key]
|
32
|
+
if id
|
33
|
+
update(id, value)
|
34
|
+
else
|
35
|
+
create(key, value)
|
45
36
|
end
|
46
37
|
end
|
47
38
|
|
@@ -107,38 +98,40 @@ class HashTree
|
|
107
98
|
end
|
108
99
|
end
|
109
100
|
|
110
|
-
# Private API
|
111
101
|
def translate_id(id)
|
112
|
-
branch_id, leaf_id = id[0..31], id[
|
102
|
+
branch_id, leaf_id = id[0..31], id[32..64]
|
113
103
|
return branch_id, leaf_id
|
114
104
|
end
|
115
105
|
|
106
|
+
def generate_id
|
107
|
+
Digest::SHA256.hexdigest(SecureRandom.uuid)
|
108
|
+
end
|
109
|
+
|
116
110
|
def read(id)
|
117
111
|
branch_id, leaf_id = translate_id(id)
|
118
|
-
|
119
|
-
leaf = branch ? branch[leaf_id] : nil
|
112
|
+
@branches[branch_id][leaf_id]
|
120
113
|
end
|
121
114
|
|
122
115
|
def update(id, value)
|
123
|
-
|
124
|
-
|
125
|
-
return
|
116
|
+
branch_id, leaf_id = translate_id(id)
|
117
|
+
@branches[branch_id][leaf_id] = value
|
118
|
+
return value
|
126
119
|
end
|
127
120
|
|
128
121
|
def create(key, value)
|
129
122
|
free_branch_id = @branches[:free].keys.sample
|
130
123
|
if free_branch_id
|
131
|
-
leaf_id =
|
132
|
-
|
133
|
-
|
134
|
-
|
124
|
+
leaf_id = generate_id[0..31]
|
125
|
+
id = free_branch_id + leaf_id
|
126
|
+
@index[key] = id
|
127
|
+
@branches[free_branch_id][leaf_id] = value
|
135
128
|
@branches[:free].delete(branch_id) if branch.size >= @settings[:max_branch_size]
|
136
129
|
else
|
137
|
-
id =
|
138
|
-
@index[key] = id
|
130
|
+
id = generate_id
|
139
131
|
branch_id, leaf_id = translate_id(id)
|
140
|
-
|
141
|
-
@branches[branch_id] =
|
132
|
+
@index[key] = id
|
133
|
+
@branches[branch_id] = {}
|
134
|
+
@branches[branch_id][leaf_id] = value
|
142
135
|
end
|
143
136
|
return value
|
144
137
|
end
|
@@ -150,7 +143,7 @@ class HashTree
|
|
150
143
|
if branch.size == 0
|
151
144
|
@branches.delete(branch_id)
|
152
145
|
@branches[:free].delete(branch_id)
|
153
|
-
elsif branch.size > 0 and branch.size <
|
146
|
+
elsif branch.size > 0 and branch.size < @settings[:max_branch_size]
|
154
147
|
@branches[:free][branch_id] = nil
|
155
148
|
end
|
156
149
|
end
|