hashtree 0.0.5 → 0.0.6
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/lib/hashtree/version.rb +1 -1
- data/lib/hashtree.rb +40 -38
- metadata +1 -1
data/lib/hashtree/version.rb
CHANGED
data/lib/hashtree.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'digest'
|
3
3
|
require 'securerandom'
|
4
|
+
require 'thread'
|
4
5
|
|
5
6
|
require "hashtree/version"
|
6
7
|
|
@@ -13,9 +14,10 @@ class HashTree
|
|
13
14
|
max_branch_size: 16
|
14
15
|
}.merge(settings)
|
15
16
|
@index = {}
|
16
|
-
@branches = {
|
17
|
-
|
18
|
-
|
17
|
+
@branches = {}
|
18
|
+
@free = {}
|
19
|
+
@lock = Mutex.new
|
20
|
+
@cv = ConditionVariable.new
|
19
21
|
end
|
20
22
|
|
21
23
|
def [](key)
|
@@ -37,17 +39,13 @@ class HashTree
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def delete(key)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
else
|
48
|
-
nil
|
49
|
-
end
|
50
|
-
rescue
|
42
|
+
id = @index[key]
|
43
|
+
if id
|
44
|
+
doc = read(id)
|
45
|
+
destroy(id)
|
46
|
+
@index.delete(key)
|
47
|
+
return doc
|
48
|
+
else
|
51
49
|
nil
|
52
50
|
end
|
53
51
|
end
|
@@ -84,20 +82,17 @@ class HashTree
|
|
84
82
|
end
|
85
83
|
|
86
84
|
def branch(key)
|
87
|
-
branch_id = get_branch_id(key)
|
88
|
-
@branches[branch_id] if branch_id
|
89
|
-
end
|
90
|
-
|
91
|
-
private
|
92
|
-
def get_branch_id(key)
|
93
85
|
id = @index[key]
|
94
86
|
if id
|
95
87
|
branch_id = id[0..31]
|
88
|
+
@branches[branch_id]
|
96
89
|
else
|
97
90
|
nil
|
98
91
|
end
|
99
92
|
end
|
100
93
|
|
94
|
+
private
|
95
|
+
|
101
96
|
def translate_id(id)
|
102
97
|
branch_id, leaf_id = id[0..31], id[32..64]
|
103
98
|
return branch_id, leaf_id
|
@@ -107,25 +102,14 @@ class HashTree
|
|
107
102
|
Digest::SHA256.hexdigest(SecureRandom.uuid)
|
108
103
|
end
|
109
104
|
|
110
|
-
def read(id)
|
111
|
-
branch_id, leaf_id = translate_id(id)
|
112
|
-
@branches[branch_id][leaf_id]
|
113
|
-
end
|
114
|
-
|
115
|
-
def update(id, value)
|
116
|
-
branch_id, leaf_id = translate_id(id)
|
117
|
-
@branches[branch_id][leaf_id] = value
|
118
|
-
return value
|
119
|
-
end
|
120
|
-
|
121
105
|
def create(key, value)
|
122
|
-
free_branch_id = @
|
106
|
+
free_branch_id = @free.keys.sample
|
123
107
|
if free_branch_id
|
124
108
|
leaf_id = generate_id[0..31]
|
125
109
|
id = free_branch_id + leaf_id
|
126
110
|
@index[key] = id
|
127
111
|
@branches[free_branch_id][leaf_id] = value
|
128
|
-
@
|
112
|
+
@free.delete(branch_id) if branch.size >= @settings[:max_branch_size]
|
129
113
|
else
|
130
114
|
id = generate_id
|
131
115
|
branch_id, leaf_id = translate_id(id)
|
@@ -136,16 +120,34 @@ class HashTree
|
|
136
120
|
return value
|
137
121
|
end
|
138
122
|
|
123
|
+
def read(id)
|
124
|
+
branch_id, leaf_id = translate_id(id)
|
125
|
+
begin
|
126
|
+
@branches[branch_id][leaf_id]
|
127
|
+
rescue
|
128
|
+
nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def update(id, value)
|
133
|
+
branch_id, leaf_id = translate_id(id)
|
134
|
+
begin
|
135
|
+
@branches[branch_id][leaf_id] = value
|
136
|
+
rescue
|
137
|
+
nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
139
141
|
def destroy(id)
|
140
142
|
branch_id, leaf_id = translate_id(id)
|
141
|
-
|
142
|
-
|
143
|
-
if branch.size == 0
|
143
|
+
value = @branches[branch_id].delete(leaf_id)
|
144
|
+
if @branches[branch_id].size == 0
|
144
145
|
@branches.delete(branch_id)
|
145
|
-
@
|
146
|
+
@free.delete(branch_id)
|
146
147
|
elsif branch.size > 0 and branch.size < @settings[:max_branch_size]
|
147
|
-
@
|
148
|
+
@free[branch_id] = nil
|
148
149
|
end
|
150
|
+
return value
|
149
151
|
end
|
150
152
|
|
151
153
|
end
|