clindex 1.2.0 → 1.2.1

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.
Files changed (2) hide show
  1. data/src/index.rb +276 -276
  2. metadata +38 -57
@@ -1,276 +1,276 @@
1
- # $Id: index.rb,v 1.14 2003/05/28 22:57:28 chrismo Exp $
2
- =begin
3
- ----------------------------------------------------------------------------
4
- Copyright (c) 2002, Chris Morris
5
- All rights reserved.
6
-
7
- Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are met:
9
-
10
- 1. Redistributions of source code must retain the above copyright notice,
11
- this list of conditions and the following disclaimer.
12
-
13
- 2. Redistributions in binary form must reproduce the above copyright notice,
14
- this list of conditions and the following disclaimer in the documentation
15
- and/or other materials provided with the distribution.
16
-
17
- 3. Neither the names Chris Morris, cLabs nor the names of contributors to
18
- this software may be used to endorse or promote products derived from this
19
- software without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22
- IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23
- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
25
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28
- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
- ----------------------------------------------------------------------------
33
- (based on BSD Open Source License)
34
- =end
35
-
36
- require 'thread'
37
- require 'drb'
38
- require 'rubygems'
39
- gem 'clutil'
40
- require 'cl/util/console'
41
-
42
- # create a batch_add method? This would allow a whole
43
- # page full of terms to be queued and the index
44
- # locked for the duration to prevent a concurrent
45
- # query from getting a partial result
46
- class ClIndex
47
- attr_accessor :index
48
-
49
- WAIT = true
50
- NO_WAIT = false
51
-
52
- def initialize(verboseServer=false)
53
- @index = {}
54
- @lockMgr = ClIndexLockMgr.new
55
- @verboseServer = verboseServer
56
- end
57
-
58
- def assign(src)
59
- @index = src.index
60
- end
61
-
62
- # refactor with do_read?
63
- def do_edit(wait)
64
- locked = lock(ClIndexLockMgr::EDIT, wait)
65
- if locked
66
- begin
67
- yield if block_given?
68
- ensure
69
- unlock(ClIndexLockMgr::EDIT)
70
- end
71
- end
72
- end
73
-
74
- def add(term, reference, wait=NO_WAIT)
75
- success = false
76
- do_edit(wait) {
77
- @index[term] = [] if @index[term].nil?
78
- @index[term] << reference
79
- @index[term].uniq!
80
- success = true
81
- }
82
- success
83
- end
84
-
85
- def remove(reference, wait=NO_WAIT)
86
- success = false
87
- do_edit(wait) {
88
- @index.each_pair do |term, refArray|
89
- @index[term].delete(reference) if refArray.include?(reference)
90
- @index.delete(term) if @index[term].empty?
91
- end
92
- success = true
93
- }
94
- success
95
- end
96
-
97
- # two optional parameters, filename (defaults to index.dat in pwd) and
98
- # wait (defaults to false). If wait is false and a blocking action
99
- # is preventing saving, the call returns immediately. If wait is true,
100
- # save waits for the blocking action to complete before continuing.
101
- def save(filename='index.dat', wait=NO_WAIT)
102
- locked = lock(ClIndexLockMgr::SAVE, wait)
103
- if locked
104
- begin
105
- File.open(filename, File::CREAT|File::TRUNC|File::RDWR) do |f|
106
- Marshal.dump(self, f)
107
- end
108
- ensure
109
- unlock(ClIndexLockMgr::SAVE)
110
- end
111
- end
112
- locked
113
- end
114
-
115
- def load(filename='index.dat', wait=NO_WAIT)
116
- locked = lock(ClIndexLockMgr::LOAD, wait)
117
- if locked
118
- begin
119
- src = nil
120
- File.open(filename) do |f|
121
- src = Marshal.load(f)
122
- end
123
- assign(src)
124
- ensure
125
- unlock(ClIndexLockMgr::LOAD)
126
- end
127
- end
128
- locked
129
- end
130
-
131
- def search(term, hits, wait=NO_WAIT)
132
- puts 'searching...' if @verboseServer
133
- success = false
134
- do_read(wait) {
135
- success = true
136
- terms = @index.keys.grep(/#{term}/i)
137
- terms.each do |thisTerm|
138
- hits << @index[thisTerm]
139
- end
140
- hits = hits.flatten.uniq.sort
141
- }
142
- success
143
- end
144
-
145
- def do_read(wait)
146
- locked = lock(ClIndexLockMgr::READ, wait)
147
- if locked
148
- begin
149
- yield if block_given?
150
- ensure
151
- unlock(ClIndexLockMgr::READ)
152
- end
153
- end
154
- end
155
-
156
- def all_terms(reference, wait=NO_WAIT)
157
- all = []
158
- do_read(wait) {
159
- @index.each do |term, refArray|
160
- all << term if refArray.include?(reference)
161
- end
162
- }
163
- all
164
- end
165
-
166
- def reference_exists?(reference, wait=NO_WAIT)
167
- exists = false
168
- do_read(wait) {
169
- @index.each do |term, refArray|
170
- if refArray.include? reference
171
- exists = true
172
- break
173
- end
174
- end
175
- }
176
- exists
177
- end
178
-
179
- def term_exists?(term, wait=NO_WAIT)
180
- exists = false
181
- do_read(wait) {
182
- exists = @index.keys.include?(term)
183
- }
184
- exists
185
- end
186
-
187
- def lock(lockType, wait=NO_WAIT)
188
- @lockMgr.lock(lockType, wait)
189
- end
190
-
191
- def unlock(lockType)
192
- @lockMgr.unlock(lockType)
193
- end
194
- end
195
-
196
- class ThreadSafeArray
197
- def initialize
198
- @mutex = Mutex.new
199
- @internalArray = []
200
- end
201
-
202
- def to_ary
203
- @internalArray
204
- end
205
-
206
- def method_missing(method, *args, &block)
207
- @mutex.synchronize do
208
- @internalArray.send(method, *args, &block)
209
- end
210
- end
211
- end
212
-
213
- class ClIndexLockMgr
214
- LOAD = 'load'
215
- SAVE = 'save'
216
- EDIT = 'edit'
217
- READ = 'read'
218
- WAIT = true
219
-
220
- def initialize
221
- @allowable = {
222
- LOAD => [],
223
- SAVE => [READ],
224
- EDIT => [],
225
- READ => [READ, SAVE]
226
- }
227
-
228
- @current = ThreadSafeArray.new
229
- @mutex = Mutex.new
230
- end
231
-
232
- def lock_approved(lockType)
233
- result = true
234
- @allowable.each_pair do |locked, allowable|
235
- if @current.include?(locked) && !allowable.include?(lockType)
236
- result = false
237
- end
238
- break if !result
239
- end
240
- result
241
- end
242
-
243
- def lock(lockType, wait=false)
244
- if wait
245
- begin
246
- approved = lock_approved(lockType)
247
- end until approved
248
- else
249
- approved = lock_approved(lockType)
250
- end
251
- @current << lockType if approved
252
- approved
253
- end
254
-
255
- def unlock(lockType)
256
- @current.delete(lockType)
257
- end
258
- end
259
-
260
- def launch_server(port='9110')
261
- idxServer = ClIndex.new(true)
262
- puts "ClIndex launching on localhost:#{port}..."
263
- DRb.start_service("druby://localhost:#{port}", idxServer)
264
- DRb.thread.join
265
- end
266
-
267
- if __FILE__ == $0
268
- if if_switch('-s')
269
- port = get_switch('-p')
270
- if port
271
- launch_server(port)
272
- else
273
- launch_server
274
- end
275
- end
276
- end
1
+ # $Id: index.rb,v 1.14 2003/05/28 22:57:28 chrismo Exp $
2
+ =begin
3
+ ----------------------------------------------------------------------------
4
+ Copyright (c) 2002, Chris Morris
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright notice,
11
+ this list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation
15
+ and/or other materials provided with the distribution.
16
+
17
+ 3. Neither the names Chris Morris, cLabs nor the names of contributors to
18
+ this software may be used to endorse or promote products derived from this
19
+ software without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22
+ IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
25
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ ----------------------------------------------------------------------------
33
+ (based on BSD Open Source License)
34
+ =end
35
+
36
+ require 'thread'
37
+ require 'drb'
38
+ require 'rubygems'
39
+ gem 'clutil'
40
+ require 'cl/util/console'
41
+
42
+ # create a batch_add method? This would allow a whole
43
+ # page full of terms to be queued and the index
44
+ # locked for the duration to prevent a concurrent
45
+ # query from getting a partial result
46
+ class ClIndex
47
+ attr_accessor :index
48
+
49
+ WAIT = true
50
+ NO_WAIT = false
51
+
52
+ def initialize(verboseServer=false)
53
+ @index = {}
54
+ @lockMgr = ClIndexLockMgr.new
55
+ @verboseServer = verboseServer
56
+ end
57
+
58
+ def assign(src)
59
+ @index = src.index
60
+ end
61
+
62
+ # refactor with do_read?
63
+ def do_edit(wait)
64
+ locked = lock(ClIndexLockMgr::EDIT, wait)
65
+ if locked
66
+ begin
67
+ yield if block_given?
68
+ ensure
69
+ unlock(ClIndexLockMgr::EDIT)
70
+ end
71
+ end
72
+ end
73
+
74
+ def add(term, reference, wait=NO_WAIT)
75
+ success = false
76
+ do_edit(wait) {
77
+ @index[term] = [] if @index[term].nil?
78
+ @index[term] << reference
79
+ @index[term].uniq!
80
+ success = true
81
+ }
82
+ success
83
+ end
84
+
85
+ def remove(reference, wait=NO_WAIT)
86
+ success = false
87
+ do_edit(wait) {
88
+ @index.each_pair do |term, refArray|
89
+ @index[term].delete(reference) if refArray.include?(reference)
90
+ @index.delete(term) if @index[term].empty?
91
+ end
92
+ success = true
93
+ }
94
+ success
95
+ end
96
+
97
+ # two optional parameters, filename (defaults to index.dat in pwd) and
98
+ # wait (defaults to false). If wait is false and a blocking action
99
+ # is preventing saving, the call returns immediately. If wait is true,
100
+ # save waits for the blocking action to complete before continuing.
101
+ def save(filename='index.dat', wait=NO_WAIT)
102
+ locked = lock(ClIndexLockMgr::SAVE, wait)
103
+ if locked
104
+ begin
105
+ File.open(filename, File::CREAT|File::TRUNC|File::RDWR) do |f|
106
+ Marshal.dump(self, f)
107
+ end
108
+ ensure
109
+ unlock(ClIndexLockMgr::SAVE)
110
+ end
111
+ end
112
+ locked
113
+ end
114
+
115
+ def load(filename='index.dat', wait=NO_WAIT)
116
+ locked = lock(ClIndexLockMgr::LOAD, wait)
117
+ if locked
118
+ begin
119
+ src = nil
120
+ File.open(filename) do |f|
121
+ src = Marshal.load(f)
122
+ end
123
+ assign(src)
124
+ ensure
125
+ unlock(ClIndexLockMgr::LOAD)
126
+ end
127
+ end
128
+ locked
129
+ end
130
+
131
+ def search(term, hits, wait=NO_WAIT)
132
+ puts 'searching...' if @verboseServer
133
+ success = false
134
+ do_read(wait) {
135
+ success = true
136
+ terms = @index.keys.grep(/#{term}/i)
137
+ terms.each do |thisTerm|
138
+ hits << @index[thisTerm]
139
+ end
140
+ hits = hits.flatten.uniq.sort
141
+ }
142
+ success
143
+ end
144
+
145
+ def do_read(wait)
146
+ locked = lock(ClIndexLockMgr::READ, wait)
147
+ if locked
148
+ begin
149
+ yield if block_given?
150
+ ensure
151
+ unlock(ClIndexLockMgr::READ)
152
+ end
153
+ end
154
+ end
155
+
156
+ def all_terms(reference, wait=NO_WAIT)
157
+ all = []
158
+ do_read(wait) {
159
+ @index.each do |term, refArray|
160
+ all << term if refArray.include?(reference)
161
+ end
162
+ }
163
+ all
164
+ end
165
+
166
+ def reference_exists?(reference, wait=NO_WAIT)
167
+ exists = false
168
+ do_read(wait) {
169
+ @index.each do |term, refArray|
170
+ if refArray.include? reference
171
+ exists = true
172
+ break
173
+ end
174
+ end
175
+ }
176
+ exists
177
+ end
178
+
179
+ def term_exists?(term, wait=NO_WAIT)
180
+ exists = false
181
+ do_read(wait) {
182
+ exists = @index.keys.include?(term)
183
+ }
184
+ exists
185
+ end
186
+
187
+ def lock(lockType, wait=NO_WAIT)
188
+ @lockMgr.lock(lockType, wait)
189
+ end
190
+
191
+ def unlock(lockType)
192
+ @lockMgr.unlock(lockType)
193
+ end
194
+ end
195
+
196
+ class ThreadSafeArray
197
+ def initialize
198
+ @mutex = Mutex.new
199
+ @internalArray = []
200
+ end
201
+
202
+ def to_ary
203
+ @internalArray
204
+ end
205
+
206
+ def method_missing(method, *args, &block)
207
+ @mutex.synchronize do
208
+ @internalArray.send(method, *args, &block)
209
+ end
210
+ end
211
+ end
212
+
213
+ class ClIndexLockMgr
214
+ LOAD = 'load'
215
+ SAVE = 'save'
216
+ EDIT = 'edit'
217
+ READ = 'read'
218
+ WAIT = true
219
+
220
+ def initialize
221
+ @allowable = {
222
+ LOAD => [],
223
+ SAVE => [READ],
224
+ EDIT => [],
225
+ READ => [READ, SAVE]
226
+ }
227
+
228
+ @current = ThreadSafeArray.new
229
+ @mutex = Mutex.new
230
+ end
231
+
232
+ def lock_approved(lockType)
233
+ result = true
234
+ @allowable.each_pair do |locked, allowable|
235
+ if @current.include?(locked) && !allowable.include?(lockType)
236
+ result = false
237
+ end
238
+ break if !result
239
+ end
240
+ result
241
+ end
242
+
243
+ def lock(lockType, wait=false)
244
+ if wait
245
+ begin
246
+ approved = lock_approved(lockType)
247
+ end until approved
248
+ else
249
+ approved = lock_approved(lockType)
250
+ end
251
+ @current << lockType if approved
252
+ approved
253
+ end
254
+
255
+ def unlock(lockType)
256
+ @current.delete(lockType)
257
+ end
258
+ end
259
+
260
+ def launch_server(port='9110')
261
+ idxServer = ClIndex.new(true)
262
+ puts "ClIndex launching on localhost:#{port}..."
263
+ DRb.start_service("druby://localhost:#{port}", idxServer)
264
+ DRb.thread.join
265
+ end
266
+
267
+ if __FILE__ == $0
268
+ if if_switch('-s')
269
+ port = get_switch('-p')
270
+ if port
271
+ launch_server(port)
272
+ else
273
+ launch_server
274
+ end
275
+ end
276
+ end
metadata CHANGED
@@ -1,85 +1,66 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: clindex
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 0
10
- version: 1.2.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - chrismo
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-05-22 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: clutil
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 15599
29
- segments:
30
- - 2011
31
- - 138
32
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 2011.138.0
34
22
  type: :runtime
35
- version_requirements: *id001
36
- description: A generic index DRb server. The core index is a hash, each key is an individual term, each value is an array of references for that term. Searches the index with a simple regexp grep against the hash keys to return a single array of all references on matching terms. Multi-user ready via a simple locking mechanism that probably doesn't scale too well. BSD License.
37
- email:
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2011.138.0
30
+ description: A generic index DRb server. The core index is a hash, each key is an
31
+ individual term, each value is an array of references for that term. Searches the
32
+ index with a simple regexp grep against the hash keys to return a single array of
33
+ all references on matching terms. Multi-user ready via a simple locking mechanism
34
+ that probably doesn't scale too well. BSD License.
35
+ email:
38
36
  - chrismo@clabs.org
39
37
  executables: []
40
-
41
38
  extensions: []
42
-
43
39
  extra_rdoc_files: []
44
-
45
- files:
40
+ files:
46
41
  - src/index.rb
47
- homepage: http://clabs.org/ruby.htm
42
+ homepage: https://github.com/chrismo/clindex
48
43
  licenses: []
49
-
50
44
  post_install_message:
51
45
  rdoc_options: []
52
-
53
- require_paths:
46
+ require_paths:
54
47
  - src
55
- required_ruby_version: !ruby/object:Gem::Requirement
48
+ required_ruby_version: !ruby/object:Gem::Requirement
56
49
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 59
61
- segments:
62
- - 1
63
- - 8
64
- - 6
65
- version: 1.8.6
66
- required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
55
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 23
72
- segments:
73
- - 1
74
- - 3
75
- - 6
76
- version: 1.3.6
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.0
77
60
  requirements: []
78
-
79
61
  rubyforge_project:
80
- rubygems_version: 1.8.2
62
+ rubygems_version: 1.8.23
81
63
  signing_key:
82
64
  specification_version: 3
83
65
  summary: cLabs Index
84
66
  test_files: []
85
-