clindex 1.2.1 → 2.0.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.
- checksums.yaml +7 -0
- data/src/index.rb +63 -98
- metadata +42 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f19c672983f0cb3dca28cc030abb2b67629f254f0c1f7ff9d28eed1d4e5a6658
|
4
|
+
data.tar.gz: 600dc6c4592d6ba37123f2e4f6335100c6fff84b4389cc090678567ffe5f9e8d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff793d34f4cc2e549319c224bd92dee260742a9e99190212a4a557bb95429eadc1225b571490da4e8eb7f9543b18c9fc0f2ce935cb588088ffdf7cfd58f99dc5
|
7
|
+
data.tar.gz: 5e2e63bfbc5ccc279b7e7b06b429b51857f953d19eaab327e5c799a8a52354270ecca671e337191764327bb77e63e24049eb901795853fb37cf70caaeba7b99c
|
data/src/index.rb
CHANGED
@@ -1,47 +1,12 @@
|
|
1
|
-
#
|
2
|
-
=begin
|
3
|
-
----------------------------------------------------------------------------
|
4
|
-
Copyright (c) 2002, Chris Morris
|
5
|
-
All rights reserved.
|
1
|
+
# frozen_string_literal: true
|
6
2
|
|
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
3
|
require 'drb'
|
38
4
|
require 'rubygems'
|
39
5
|
gem 'clutil'
|
40
6
|
require 'cl/util/console'
|
41
7
|
|
42
|
-
# create a batch_add method? This would allow a whole
|
43
|
-
#
|
44
|
-
# locked for the duration to prevent a concurrent
|
8
|
+
# TODO: create a batch_add method? This would allow a whole page full of terms
|
9
|
+
# to be queued and the index locked for the duration to prevent a concurrent
|
45
10
|
# query from getting a partial result
|
46
11
|
class ClIndex
|
47
12
|
attr_accessor :index
|
@@ -49,10 +14,10 @@ class ClIndex
|
|
49
14
|
WAIT = true
|
50
15
|
NO_WAIT = false
|
51
16
|
|
52
|
-
def initialize(
|
17
|
+
def initialize(verbose_server = false)
|
53
18
|
@index = {}
|
54
|
-
@
|
55
|
-
@
|
19
|
+
@lock_mgr = ClIndexLockMgr.new
|
20
|
+
@verbose_server = verbose_server
|
56
21
|
end
|
57
22
|
|
58
23
|
def assign(src)
|
@@ -62,16 +27,16 @@ class ClIndex
|
|
62
27
|
# refactor with do_read?
|
63
28
|
def do_edit(wait)
|
64
29
|
locked = lock(ClIndexLockMgr::EDIT, wait)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
30
|
+
return unless locked
|
31
|
+
|
32
|
+
begin
|
33
|
+
yield if block_given?
|
34
|
+
ensure
|
35
|
+
unlock(ClIndexLockMgr::EDIT)
|
71
36
|
end
|
72
37
|
end
|
73
38
|
|
74
|
-
def add(term, reference, wait=NO_WAIT)
|
39
|
+
def add(term, reference, wait = NO_WAIT)
|
75
40
|
success = false
|
76
41
|
do_edit(wait) {
|
77
42
|
@index[term] = [] if @index[term].nil?
|
@@ -82,11 +47,11 @@ class ClIndex
|
|
82
47
|
success
|
83
48
|
end
|
84
49
|
|
85
|
-
def remove(reference, wait=NO_WAIT)
|
50
|
+
def remove(reference, wait = NO_WAIT)
|
86
51
|
success = false
|
87
52
|
do_edit(wait) {
|
88
|
-
@index.each_pair do |term,
|
89
|
-
@index[term].delete(reference) if
|
53
|
+
@index.each_pair do |term, ref_array|
|
54
|
+
@index[term].delete(reference) if ref_array.include?(reference)
|
90
55
|
@index.delete(term) if @index[term].empty?
|
91
56
|
end
|
92
57
|
success = true
|
@@ -98,12 +63,12 @@ class ClIndex
|
|
98
63
|
# wait (defaults to false). If wait is false and a blocking action
|
99
64
|
# is preventing saving, the call returns immediately. If wait is true,
|
100
65
|
# save waits for the blocking action to complete before continuing.
|
101
|
-
def save(filename='index.dat', wait=NO_WAIT)
|
66
|
+
def save(filename = 'index.dat', wait = NO_WAIT)
|
102
67
|
locked = lock(ClIndexLockMgr::SAVE, wait)
|
103
68
|
if locked
|
104
69
|
begin
|
105
|
-
File.open(filename, File::CREAT|File::TRUNC|File::RDWR) do |f|
|
106
|
-
Marshal.dump(
|
70
|
+
File.open(filename, File::CREAT | File::TRUNC | File::RDWR) do |f|
|
71
|
+
Marshal.dump(@index, f)
|
107
72
|
end
|
108
73
|
ensure
|
109
74
|
unlock(ClIndexLockMgr::SAVE)
|
@@ -112,15 +77,13 @@ class ClIndex
|
|
112
77
|
locked
|
113
78
|
end
|
114
79
|
|
115
|
-
def load(filename='index.dat', wait=NO_WAIT)
|
80
|
+
def load(filename = 'index.dat', wait = NO_WAIT)
|
116
81
|
locked = lock(ClIndexLockMgr::LOAD, wait)
|
117
82
|
if locked
|
118
83
|
begin
|
119
|
-
src = nil
|
120
84
|
File.open(filename) do |f|
|
121
|
-
|
85
|
+
@index = Marshal.load(f)
|
122
86
|
end
|
123
|
-
assign(src)
|
124
87
|
ensure
|
125
88
|
unlock(ClIndexLockMgr::LOAD)
|
126
89
|
end
|
@@ -128,14 +91,14 @@ class ClIndex
|
|
128
91
|
locked
|
129
92
|
end
|
130
93
|
|
131
|
-
def search(term, hits, wait=NO_WAIT)
|
132
|
-
puts 'searching...' if @
|
94
|
+
def search(term, hits, wait = NO_WAIT)
|
95
|
+
puts 'searching...' if @verbose_server
|
133
96
|
success = false
|
134
97
|
do_read(wait) {
|
135
98
|
success = true
|
136
99
|
terms = @index.keys.grep(/#{term}/i)
|
137
|
-
terms.each do |
|
138
|
-
hits << @index[
|
100
|
+
terms.each do |this_term|
|
101
|
+
hits << @index[this_term]
|
139
102
|
end
|
140
103
|
hits = hits.flatten.uniq.sort
|
141
104
|
}
|
@@ -144,30 +107,30 @@ class ClIndex
|
|
144
107
|
|
145
108
|
def do_read(wait)
|
146
109
|
locked = lock(ClIndexLockMgr::READ, wait)
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
110
|
+
return unless locked
|
111
|
+
|
112
|
+
begin
|
113
|
+
yield if block_given?
|
114
|
+
ensure
|
115
|
+
unlock(ClIndexLockMgr::READ)
|
153
116
|
end
|
154
117
|
end
|
155
118
|
|
156
|
-
def all_terms(reference, wait=NO_WAIT)
|
119
|
+
def all_terms(reference, wait = NO_WAIT)
|
157
120
|
all = []
|
158
121
|
do_read(wait) {
|
159
|
-
@index.each do |term,
|
160
|
-
all << term if
|
122
|
+
@index.each do |term, ref_array|
|
123
|
+
all << term if ref_array.include?(reference)
|
161
124
|
end
|
162
125
|
}
|
163
126
|
all
|
164
127
|
end
|
165
128
|
|
166
|
-
def reference_exists?(reference, wait=NO_WAIT)
|
129
|
+
def reference_exists?(reference, wait = NO_WAIT)
|
167
130
|
exists = false
|
168
131
|
do_read(wait) {
|
169
|
-
@index.each do |
|
170
|
-
if
|
132
|
+
@index.each do |_, ref_array|
|
133
|
+
if ref_array.include? reference
|
171
134
|
exists = true
|
172
135
|
break
|
173
136
|
end
|
@@ -176,36 +139,36 @@ class ClIndex
|
|
176
139
|
exists
|
177
140
|
end
|
178
141
|
|
179
|
-
def term_exists?(term, wait=NO_WAIT)
|
142
|
+
def term_exists?(term, wait = NO_WAIT)
|
180
143
|
exists = false
|
181
144
|
do_read(wait) {
|
182
|
-
exists = @index.
|
145
|
+
exists = @index.key?(term)
|
183
146
|
}
|
184
147
|
exists
|
185
148
|
end
|
186
149
|
|
187
|
-
def lock(
|
188
|
-
@
|
150
|
+
def lock(lock_type, wait = NO_WAIT)
|
151
|
+
@lock_mgr.lock(lock_type, wait)
|
189
152
|
end
|
190
153
|
|
191
|
-
def unlock(
|
192
|
-
@
|
154
|
+
def unlock(lock_type)
|
155
|
+
@lock_mgr.unlock(lock_type)
|
193
156
|
end
|
194
157
|
end
|
195
158
|
|
196
159
|
class ThreadSafeArray
|
197
160
|
def initialize
|
198
161
|
@mutex = Mutex.new
|
199
|
-
@
|
162
|
+
@internal_array = []
|
200
163
|
end
|
201
164
|
|
202
165
|
def to_ary
|
203
|
-
@
|
166
|
+
@internal_array
|
204
167
|
end
|
205
168
|
|
206
169
|
def method_missing(method, *args, &block)
|
207
170
|
@mutex.synchronize do
|
208
|
-
@
|
171
|
+
@internal_array.send(method, *args, &block)
|
209
172
|
end
|
210
173
|
end
|
211
174
|
end
|
@@ -229,42 +192,44 @@ class ClIndexLockMgr
|
|
229
192
|
@mutex = Mutex.new
|
230
193
|
end
|
231
194
|
|
232
|
-
def lock_approved(
|
195
|
+
def lock_approved(lock_type)
|
233
196
|
result = true
|
234
197
|
@allowable.each_pair do |locked, allowable|
|
235
|
-
if @current.include?(locked) && !allowable.include?(
|
198
|
+
if @current.include?(locked) && !allowable.include?(lock_type)
|
236
199
|
result = false
|
237
200
|
end
|
238
|
-
break
|
201
|
+
break unless result
|
239
202
|
end
|
240
203
|
result
|
241
204
|
end
|
242
205
|
|
243
|
-
def lock(
|
206
|
+
def lock(lock_type, wait = false)
|
207
|
+
approved = nil
|
244
208
|
if wait
|
245
|
-
|
246
|
-
approved = lock_approved(
|
247
|
-
|
209
|
+
loop do
|
210
|
+
approved = lock_approved(lock_type)
|
211
|
+
break if approved
|
212
|
+
end
|
248
213
|
else
|
249
|
-
approved = lock_approved(
|
214
|
+
approved = lock_approved(lock_type)
|
250
215
|
end
|
251
|
-
@current <<
|
216
|
+
@current << lock_type if approved
|
252
217
|
approved
|
253
218
|
end
|
254
219
|
|
255
|
-
def unlock(
|
256
|
-
@current.delete(
|
220
|
+
def unlock(lock_type)
|
221
|
+
@current.delete(lock_type)
|
257
222
|
end
|
258
223
|
end
|
259
224
|
|
260
|
-
def launch_server(port='9110')
|
261
|
-
|
225
|
+
def launch_server(port = '9110')
|
226
|
+
idx_server = ClIndex.new(true)
|
262
227
|
puts "ClIndex launching on localhost:#{port}..."
|
263
|
-
DRb.start_service("druby://localhost:#{port}",
|
228
|
+
DRb.start_service("druby://localhost:#{port}", idx_server)
|
264
229
|
DRb.thread.join
|
265
230
|
end
|
266
231
|
|
267
|
-
if
|
232
|
+
if $0 == __FILE__
|
268
233
|
if if_switch('-s')
|
269
234
|
port = get_switch('-p')
|
270
235
|
if port
|
metadata
CHANGED
@@ -1,32 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clindex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- chrismo
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2023-11-22 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: clutil
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 2011.138.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 2011.138.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
30
55
|
description: A generic index DRb server. The core index is a hash, each key is an
|
31
56
|
individual term, each value is an array of references for that term. Searches the
|
32
57
|
index with a simple regexp grep against the hash keys to return a single array of
|
@@ -41,26 +66,24 @@ files:
|
|
41
66
|
- src/index.rb
|
42
67
|
homepage: https://github.com/chrismo/clindex
|
43
68
|
licenses: []
|
44
|
-
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
45
71
|
rdoc_options: []
|
46
72
|
require_paths:
|
47
73
|
- src
|
48
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
75
|
requirements:
|
51
|
-
- -
|
76
|
+
- - ">="
|
52
77
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
78
|
+
version: '2.4'
|
54
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
80
|
requirements:
|
57
|
-
- -
|
81
|
+
- - ">="
|
58
82
|
- !ruby/object:Gem::Version
|
59
|
-
version:
|
83
|
+
version: '0'
|
60
84
|
requirements: []
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
specification_version: 3
|
85
|
+
rubygems_version: 3.3.26
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
65
88
|
summary: cLabs Index
|
66
89
|
test_files: []
|