hashtree 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/hashtree/version.rb +1 -1
  2. data/lib/hashtree.rb +40 -38
  3. metadata +1 -1
@@ -1,3 +1,3 @@
1
1
  class HashTree
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
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
- free: {}
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
- begin
41
- id = @index[key]
42
- if id
43
- doc = read(id)
44
- destroy(id)
45
- @index.delete(key)
46
- return doc
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 = @branches[:free].keys.sample
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
- @branches[:free].delete(branch_id) if branch.size >= @settings[:max_branch_size]
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
- branch = @branches[branch_id]
142
- destroyed_element = branch.delete(leaf_id)
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
- @branches[:free].delete(branch_id)
146
+ @free.delete(branch_id)
146
147
  elsif branch.size > 0 and branch.size < @settings[:max_branch_size]
147
- @branches[:free][branch_id] = nil
148
+ @free[branch_id] = nil
148
149
  end
150
+ return value
149
151
  end
150
152
 
151
153
  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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: