hashtree 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,3 +1,3 @@
1
1
  class HashTree
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
- begin
24
- id = @index[key]
25
- if id
26
- read(id)
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
- begin
37
- id = @index[key]
38
- if id
39
- update(id, value)
40
- else
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[33..64]
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
- branch = @branches[branch_id]
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
- leaf = read(id)
124
- leaf = value if leaf
125
- return ( leaf ? value : nil )
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 = Digest::SHA2.hexdigest(SecureRandom.uuid)[0..31]
132
- @index[key] = free_branch_id + leaf_id
133
- branch = @branches[free_branch_id]
134
- branch[leaf_id] = value
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 = Digest::SHA2.hexdigest(SecureRandom.uuid)
138
- @index[key] = id
130
+ id = generate_id
139
131
  branch_id, leaf_id = translate_id(id)
140
- branch = { leaf_id => value }
141
- @branches[branch_id] = branch
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 < 16
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: