hashtree 0.0.2 → 0.0.3
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 +20 -3
- data/lib/hashtree/version.rb +1 -1
- data/lib/hashtree.rb +33 -9
- metadata +1 -1
data/examples/benchmarks.rb
CHANGED
@@ -64,9 +64,25 @@ Benchmark.bm do |x|
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
puts "Doing 1 non-indexed query"
|
68
|
+
x.report do
|
69
|
+
1.times do |i|
|
70
|
+
query = proc{ |key,doc| doc[:email] == "john.doe.#{rand(100000)}@example.com" }
|
71
|
+
@htree.select{ |key, doc| query.call(key,doc) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
puts "Doing 10 non-indexed queries"
|
76
|
+
x.report do
|
77
|
+
10.times do |i|
|
78
|
+
query = proc{ |key,doc| doc[:email] == "john.doe.#{rand(100000)}@example.com" }
|
79
|
+
@htree.select{ |key, doc| query.call(key,doc) }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
67
83
|
puts "Deleting 1.000 random documents"
|
68
84
|
x.report do
|
69
|
-
lhtree = @htree
|
85
|
+
lhtree = @htree.clone
|
70
86
|
1_000.times do |i|
|
71
87
|
key = "jdoe#{rand(100_000)}"
|
72
88
|
lhtree.delete(key)
|
@@ -75,7 +91,7 @@ Benchmark.bm do |x|
|
|
75
91
|
|
76
92
|
puts "Deleting 10.000 random documents"
|
77
93
|
x.report do
|
78
|
-
lhtree = @htree
|
94
|
+
lhtree = @htree.clone
|
79
95
|
10_000.times do |i|
|
80
96
|
key = "jdoe#{rand(100_000)}"
|
81
97
|
lhtree.delete(key)
|
@@ -84,10 +100,11 @@ Benchmark.bm do |x|
|
|
84
100
|
|
85
101
|
puts "Deleting 100.000 random documents"
|
86
102
|
x.report do
|
87
|
-
lhtree = @htree
|
103
|
+
lhtree = @htree.clone
|
88
104
|
100_000.times do |i|
|
89
105
|
key = "jdoe#{rand(100_000)}"
|
90
106
|
lhtree.delete(key)
|
91
107
|
end
|
92
108
|
end
|
109
|
+
|
93
110
|
end
|
data/lib/hashtree/version.rb
CHANGED
data/lib/hashtree.rb
CHANGED
@@ -74,6 +74,24 @@ class HashTree
|
|
74
74
|
@index.has_key?(key)
|
75
75
|
end
|
76
76
|
|
77
|
+
def select(&block)
|
78
|
+
results = {}
|
79
|
+
@index.select do |key, id|
|
80
|
+
leaf = read(id)
|
81
|
+
results[key] = leaf if yield(key, leaf)
|
82
|
+
end
|
83
|
+
return results
|
84
|
+
end
|
85
|
+
|
86
|
+
def each(&block)
|
87
|
+
results = {}
|
88
|
+
@index.each do |key, id|
|
89
|
+
leaf = read(id)
|
90
|
+
results[key] = leaf if yield(key, leaf)
|
91
|
+
end
|
92
|
+
return results
|
93
|
+
end
|
94
|
+
|
77
95
|
def branch(key)
|
78
96
|
branch_id = get_branch_id(key)
|
79
97
|
@branches[branch_id] if branch_id
|
@@ -90,15 +108,21 @@ class HashTree
|
|
90
108
|
end
|
91
109
|
|
92
110
|
# Private API
|
93
|
-
def
|
111
|
+
def translate_id(id)
|
94
112
|
branch_id, leaf_id = id[0..15], id[16..32]
|
95
|
-
|
113
|
+
return branch_id, leaf_id
|
114
|
+
end
|
115
|
+
|
116
|
+
def read(id)
|
117
|
+
branch_id, leaf_id = translate_id(id)
|
118
|
+
branch = @branches[branch_id]
|
119
|
+
leaf = branch ? branch[leaf_id] : nil
|
96
120
|
end
|
97
121
|
|
98
122
|
def update(id, value)
|
99
|
-
|
100
|
-
|
101
|
-
return value
|
123
|
+
leaf = read(id)
|
124
|
+
leaf = value if leaf
|
125
|
+
return ( leaf ? value : nil )
|
102
126
|
end
|
103
127
|
|
104
128
|
def create(key, value)
|
@@ -112,15 +136,15 @@ class HashTree
|
|
112
136
|
else
|
113
137
|
id = Digest::MD5.hexdigest(SecureRandom.uuid)
|
114
138
|
@index[key] = id
|
115
|
-
branch_id, leaf_id = id
|
116
|
-
|
117
|
-
@branches[branch_id]
|
139
|
+
branch_id, leaf_id = translate_id(id)
|
140
|
+
branch = { leaf_id => value }
|
141
|
+
@branches[branch_id] = branch
|
118
142
|
end
|
119
143
|
return value
|
120
144
|
end
|
121
145
|
|
122
146
|
def destroy(id)
|
123
|
-
branch_id, leaf_id = id
|
147
|
+
branch_id, leaf_id = translate_id(id)
|
124
148
|
branch = @branches[branch_id]
|
125
149
|
destroyed_element = branch.delete(leaf_id)
|
126
150
|
if branch.size == 0
|