hashtree 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -123,6 +123,45 @@ Benchmark.bm do |x|
123
123
  t.each(&:join)
124
124
  end
125
125
 
126
+ puts "Doing 40 non-indexed queries and 40 updates with 4 threads"
127
+ x.report do
128
+ t = []
129
+ 2.times do
130
+ t << Thread.new do
131
+ 20.times do |i|
132
+ query = proc{ |key,doc| doc[:email] == "john.doe.#{rand(100000)}@example.com" }
133
+ @htree.select{ |key, doc| query.call(key,doc) }
134
+ end
135
+ end
136
+ t << Thread.new do
137
+ 20.times do |i|
138
+ key = "jdoe#{rand(100_000)}"
139
+ @htree[key] = {}
140
+ end
141
+ end
142
+ end
143
+ t.each(&:join)
144
+ end
145
+
146
+ puts "Doing 100.000 random reads and 40 updates with 4 threads"
147
+ x.report do
148
+ t = []
149
+ 2.times do
150
+ t << Thread.new do
151
+ 20.times do |i|
152
+ 5_000.times{ key = "jdoe#{rand(100_000)}"; @htree[key] }
153
+ end
154
+ end
155
+ t << Thread.new do
156
+ 20.times do |i|
157
+ key = "jdoe#{rand(100_000)}"
158
+ @htree[key] = {}
159
+ end
160
+ end
161
+ end
162
+ t.each(&:join)
163
+ end
164
+
126
165
  # puts "Doing 100 non-indexed queries"
127
166
  # x.report do
128
167
  # 100.times do |i|
@@ -1,3 +1,3 @@
1
1
  class HashTree
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/hashtree.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'digest'
3
3
  require 'securerandom'
4
4
  require 'thread'
5
+ require 'monitor'
5
6
 
6
7
  require "hashtree/version"
7
8
 
@@ -28,19 +29,23 @@ class HashTree
28
29
  end
29
30
 
30
31
  def []=(key, value)
31
- id = @root[key]
32
- id ? update(id, value) : create(key, value)
32
+ @lock.synchronize do
33
+ id = @root[key]
34
+ id ? update(id, value) : create(key, value)
35
+ end
33
36
  end
34
37
 
35
38
  def delete(key)
36
- id = @root[key]
37
- if id
38
- doc = read(id)
39
- destroy(id)
40
- @root.delete(key)
41
- return doc
42
- else
43
- nil
39
+ @lock.synchronize do
40
+ id = @root[key]
41
+ if id
42
+ doc = read(id)
43
+ destroy(id)
44
+ @root.delete(key)
45
+ return doc
46
+ else
47
+ nil
48
+ end
44
49
  end
45
50
  end
46
51
 
@@ -59,18 +64,20 @@ class HashTree
59
64
 
60
65
  def select(&block)
61
66
  results = {}
62
- @root.select do |key, id|
63
- leaf = read(id)
64
- results[key] = leaf if yield(key, leaf)
67
+ keys = @root.keys
68
+ keys.each do |key|
69
+ leaf = self[key]
70
+ results[key] = leaf if yield(key, leaf)
65
71
  end
66
72
  return results
67
73
  end
68
74
 
69
75
  def each(&block)
70
76
  results = {}
71
- @root.each do |key, id|
72
- leaf = read(id)
73
- results[key] = leaf if yield(key, leaf)
77
+ keys = @root.keys
78
+ keys.each do |key|
79
+ leaf = self[key]
80
+ yield(key, leaf)
74
81
  end
75
82
  return results
76
83
  end
@@ -106,48 +113,42 @@ class HashTree
106
113
  end
107
114
 
108
115
  def create(key, value)
109
- @lock.synchronize do
110
- free_branch_id = @free.keys.sample
111
- if free_branch_id
112
- leaf_id = generate_id[0..31]
113
- id = free_branch_id + leaf_id
114
- @root[key] = id
115
- @branches[free_branch_id][leaf_id] = value
116
- @free.delete(branch_id) if branch.size >= @settings[:max_branch_size]
117
- else
118
- id = generate_id
119
- branch_id, leaf_id = translate_id(id)
120
- @root[key] = id
121
- @branches[branch_id] = {}
122
- @branches[branch_id][leaf_id] = value
123
- end
124
- return value
116
+ free_branch_id = @free.keys.sample
117
+ if free_branch_id
118
+ leaf_id = generate_id[0..31]
119
+ id = free_branch_id + leaf_id
120
+ @root[key] = id
121
+ @branches[free_branch_id][leaf_id] = value
122
+ @free.delete(branch_id) if branch.size >= @settings[:max_branch_size]
123
+ else
124
+ id = generate_id
125
+ branch_id, leaf_id = translate_id(id)
126
+ @root[key] = id
127
+ @branches[branch_id] = {}
128
+ @branches[branch_id][leaf_id] = value
125
129
  end
130
+ return value
126
131
  end
127
132
 
128
133
  def update(id, value)
129
- @lock.synchronize do
130
- branch_id, leaf_id = translate_id(id)
131
- begin
132
- @branches[branch_id][leaf_id] = value
133
- rescue
134
- nil
135
- end
134
+ branch_id, leaf_id = translate_id(id)
135
+ begin
136
+ @branches[branch_id][leaf_id] = value
137
+ rescue
138
+ nil
136
139
  end
137
140
  end
138
141
 
139
142
  def destroy(id)
140
- @lock.synchronize do
141
- branch_id, leaf_id = translate_id(id)
142
- value = @branches[branch_id].delete(leaf_id)
143
- if @branches[branch_id].size == 0
144
- @branches.delete(branch_id)
145
- @free.delete(branch_id)
146
- elsif branch.size > 0 and branch.size < @settings[:max_branch_size]
147
- @free[branch_id] = nil
148
- end
149
- return value
143
+ branch_id, leaf_id = translate_id(id)
144
+ value = @branches[branch_id].delete(leaf_id)
145
+ if @branches[branch_id].size == 0
146
+ @branches.delete(branch_id)
147
+ @free.delete(branch_id)
148
+ elsif branch.size > 0 and branch.size < @settings[:max_branch_size]
149
+ @free[branch_id] = nil
150
150
  end
151
+ return value
151
152
  end
152
153
 
153
154
  end
metadata CHANGED
@@ -1,64 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
5
- prerelease:
4
+ version: 0.0.8
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Marcelo Wiermann
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-09-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- version_requirements: !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
19
  - - ! '>='
19
20
  - !ruby/object:Gem::Version
20
21
  version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
21
25
  none: false
22
- requirement: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
- none: false
28
- prerelease: false
29
- type: :development
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: guard
32
- version_requirements: !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
33
34
  requirements:
34
35
  - - ! '>='
35
36
  - !ruby/object:Gem::Version
36
37
  version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
37
41
  none: false
38
- requirement: !ruby/object:Gem::Requirement
39
42
  requirements:
40
43
  - - ! '>='
41
44
  - !ruby/object:Gem::Version
42
45
  version: '0'
43
- none: false
44
- prerelease: false
45
- type: :development
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: factory_girl
48
- version_requirements: !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
49
50
  requirements:
50
51
  - - ! '>='
51
52
  - !ruby/object:Gem::Version
52
53
  version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
53
57
  none: false
54
- requirement: !ruby/object:Gem::Requirement
55
58
  requirements:
56
59
  - - ! '>='
57
60
  - !ruby/object:Gem::Version
58
61
  version: '0'
59
- none: false
60
- prerelease: false
61
- type: :development
62
62
  description: HashTree that has a Hash-like interface and stores data into blocks
63
63
  email:
64
64
  - marcelo.wiermann@gmail.com
@@ -79,28 +79,27 @@ files:
79
79
  - spec/spec_helper.rb
80
80
  homepage: ''
81
81
  licenses: []
82
- post_install_message:
82
+ post_install_message:
83
83
  rdoc_options: []
84
84
  require_paths:
85
85
  - lib
86
86
  required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
87
88
  requirements:
88
89
  - - ! '>='
89
90
  - !ruby/object:Gem::Version
90
91
  version: '0'
91
- none: false
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
93
94
  requirements:
94
95
  - - ! '>='
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
- none: false
98
98
  requirements: []
99
- rubyforge_project:
99
+ rubyforge_project:
100
100
  rubygems_version: 1.8.24
101
- signing_key:
101
+ signing_key:
102
102
  specification_version: 3
103
103
  summary: HashTree
104
104
  test_files:
105
105
  - spec/spec_helper.rb
106
- ...