poliqarpr 0.1.1 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/changelog.txt +13 -0
- data/lib/poliqarpr/client.rb +35 -10
- data/lib/poliqarpr/config.rb +2 -2
- data/lib/poliqarpr/connector.rb +6 -8
- data/lib/poliqarpr/exceptions.rb +14 -2
- data/poliqarpr.gemspec +1 -1
- data/spec/client.rb +32 -6
- metadata +4 -12
data/changelog.txt
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
0.1.5
|
2
|
+
- Raises exception if failed to open corpus
|
3
|
+
- New PoliqarpException class as a base for all Poliqarp exception
|
4
|
+
|
5
|
+
0.1.4
|
6
|
+
- The logger data is automatically flushed.
|
7
|
+
|
8
|
+
0.1.3
|
9
|
+
- Alternative logger for debug
|
10
|
+
|
11
|
+
0.1.2
|
12
|
+
- IndexOutOfBounds includes info about index
|
13
|
+
|
1
14
|
0.1.1
|
2
15
|
- remove messy debug message
|
3
16
|
- raise IndexOutOfBounds exception when the excerpt index is larger than the
|
data/lib/poliqarpr/client.rb
CHANGED
@@ -6,9 +6,12 @@ module Poliqarp
|
|
6
6
|
# This class is the implementation of the Poliqarp server client.
|
7
7
|
class Client
|
8
8
|
# If debug is turned on, the communication between server and client
|
9
|
-
# is logged to
|
9
|
+
# is logged to logger.
|
10
10
|
attr_writer :debug
|
11
11
|
|
12
|
+
# Logger used for debugging. STDOUT by default.
|
13
|
+
attr_accessor :logger
|
14
|
+
|
12
15
|
# The configuration of the client.
|
13
16
|
attr_reader :config
|
14
17
|
|
@@ -21,7 +24,8 @@ module Poliqarp
|
|
21
24
|
def initialize(session_name="RUBY", debug=false)
|
22
25
|
@session_name = session_name
|
23
26
|
@debug = debug
|
24
|
-
@
|
27
|
+
@logger = STDOUT
|
28
|
+
@connector = Connector.new(self)
|
25
29
|
@config = Config.new(self,500000)
|
26
30
|
@answer_queue = Queue.new
|
27
31
|
@waiting_mutext = Mutex.new
|
@@ -64,6 +68,19 @@ module Poliqarp
|
|
64
68
|
talk "CLOSE"
|
65
69
|
end
|
66
70
|
|
71
|
+
# Prints the debug +msg+ to the logger if debugging is turned on.
|
72
|
+
# Accepts both regular message and block with message. The second
|
73
|
+
# form is provided for messages which aren't cheep to build.
|
74
|
+
def debug(msg=nil)
|
75
|
+
if @debug
|
76
|
+
if block_given?
|
77
|
+
msg = yield
|
78
|
+
end
|
79
|
+
logger.puts msg
|
80
|
+
logger.flush
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
67
84
|
# *Asynchronous* Opens the corpus given as +path+. To open the default
|
68
85
|
# corpus pass +:default+ as the argument.
|
69
86
|
#
|
@@ -73,7 +90,12 @@ module Poliqarp
|
|
73
90
|
if path == :default
|
74
91
|
open_corpus(DEFAULT_CORPUS, &handler)
|
75
92
|
else
|
76
|
-
talk("OPEN #{path}", :async, &handler)
|
93
|
+
result = talk("OPEN #{path}", :async, &handler)
|
94
|
+
if result == "OPENED"
|
95
|
+
result
|
96
|
+
else
|
97
|
+
raise PoliqarpException.new(result)
|
98
|
+
end
|
77
99
|
end
|
78
100
|
end
|
79
101
|
|
@@ -210,7 +232,7 @@ protected
|
|
210
232
|
def left_context=(value)
|
211
233
|
result = talk("SET left-context-width #{value}")
|
212
234
|
unless result =~ /^OK/
|
213
|
-
raise "Failed to set left context to #{value}: #{result}"
|
235
|
+
raise PoliqarpException.new("Failed to set left context to #{value}: #{result}")
|
214
236
|
end
|
215
237
|
end
|
216
238
|
|
@@ -218,7 +240,7 @@ protected
|
|
218
240
|
def right_context=(value)
|
219
241
|
result = talk("SET right-context-width #{value}")
|
220
242
|
unless result =~ /^OK/
|
221
|
-
raise "Failed to set right context to #{value}: #{result}"
|
243
|
+
raise PoliqarpException.new("Failed to set right context to #{value}: #{result}")
|
222
244
|
end
|
223
245
|
end
|
224
246
|
|
@@ -241,7 +263,6 @@ protected
|
|
241
263
|
# * +handler+ the handler of the assynchronous message.
|
242
264
|
# It is ignored when the mode is set to :sync.
|
243
265
|
def talk(msg, mode = :sync, &handler)
|
244
|
-
puts msg if @debug
|
245
266
|
if mode == :sync
|
246
267
|
@connector.send_message(msg, mode, &handler)
|
247
268
|
else
|
@@ -297,6 +318,10 @@ protected
|
|
297
318
|
# * +index+ the index of the answer to be retrieved
|
298
319
|
def find_one(query,index)
|
299
320
|
make_async_query(query,index)
|
321
|
+
loop do
|
322
|
+
break unless should_wait?
|
323
|
+
sleep 0.01
|
324
|
+
end
|
300
325
|
talk("GET-RESULTS #{index} #{index}")
|
301
326
|
fetch_result(index,query)
|
302
327
|
end
|
@@ -383,7 +408,7 @@ protected
|
|
383
408
|
def do_wait
|
384
409
|
loop {
|
385
410
|
break unless should_wait?
|
386
|
-
|
411
|
+
debug("WAITING")
|
387
412
|
sleep 0.1
|
388
413
|
}
|
389
414
|
@answer_queue.shift
|
@@ -394,7 +419,7 @@ protected
|
|
394
419
|
@waiting_mutext.synchronize {
|
395
420
|
@should_wait = false
|
396
421
|
}
|
397
|
-
|
422
|
+
debug("WAITING stopped")
|
398
423
|
end
|
399
424
|
|
400
425
|
# Check if the thread should still wait for the answer.
|
@@ -411,11 +436,11 @@ protected
|
|
411
436
|
@waiting_mutext.synchronize {
|
412
437
|
@should_wait = true
|
413
438
|
}
|
414
|
-
|
439
|
+
debug("WAITING started")
|
415
440
|
end
|
416
441
|
|
417
442
|
def make_async_query(query,answer_offset)
|
418
|
-
raise IndexOutOfBounds if answer_offset > config.buffer_size
|
443
|
+
raise IndexOutOfBounds.new(answer_offset) if answer_offset > config.buffer_size
|
419
444
|
start_waiting
|
420
445
|
# we access the result count through BUFFER-STATE call
|
421
446
|
make_query(query){|msg| stop_waiting}
|
data/lib/poliqarpr/config.rb
CHANGED
@@ -25,7 +25,7 @@ module Poliqarp
|
|
25
25
|
@client.send(:left_context=,value)
|
26
26
|
@left_context_size = value
|
27
27
|
else
|
28
|
-
raise "Invalid argument: #{value}. It must be fixnum greater than 0."
|
28
|
+
raise PoliqarpException.new("Invalid argument: #{value}. It must be fixnum greater than 0.")
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -39,7 +39,7 @@ module Poliqarp
|
|
39
39
|
@client.send(:right_context=,value)
|
40
40
|
@right_context_size = value
|
41
41
|
else
|
42
|
-
raise "Invalid argument: #{value}. It must be fixnum greater than 0."
|
42
|
+
raise PoliqarpException.new("Invalid argument: #{value}. It must be fixnum greater than 0.")
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
data/lib/poliqarpr/connector.rb
CHANGED
@@ -37,11 +37,11 @@ module Poliqarp
|
|
37
37
|
UTF8 = "utf-8"
|
38
38
|
|
39
39
|
# Creates new connector
|
40
|
-
def initialize(
|
40
|
+
def initialize(client)
|
41
41
|
@message_queue = Queue.new
|
42
42
|
@socket_mutex = Mutex.new
|
43
43
|
@loop_mutex = Mutex.new
|
44
|
-
@
|
44
|
+
@client = client
|
45
45
|
end
|
46
46
|
|
47
47
|
# Opens connection with poliqarp server which runs
|
@@ -66,7 +66,7 @@ module Poliqarp
|
|
66
66
|
# * +mode+ synchronous (+:sync:) or asynchronous (+:async+)
|
67
67
|
# * +handler+ the handler of the asynchronous message
|
68
68
|
def send_message(message, mode, &handler)
|
69
|
-
|
69
|
+
@client.debug{"send #{mode} #{message}"}
|
70
70
|
if ruby19?
|
71
71
|
massage = message.encode(UTF8)
|
72
72
|
end
|
@@ -87,10 +87,8 @@ module Poliqarp
|
|
87
87
|
case code
|
88
88
|
when 15
|
89
89
|
raise JobInProgress.new()
|
90
|
-
when 17
|
91
|
-
raise IndexOutOfBounds.new()
|
92
90
|
else
|
93
|
-
raise
|
91
|
+
raise PoliqarpException.new("Poliqarp Error: "+ERRORS[code])
|
94
92
|
end
|
95
93
|
else
|
96
94
|
message
|
@@ -123,12 +121,12 @@ private
|
|
123
121
|
end
|
124
122
|
|
125
123
|
def receive_sync(message)
|
126
|
-
|
124
|
+
@client.debug{"receive sync: #{message}"}
|
127
125
|
@message_queue << message
|
128
126
|
end
|
129
127
|
|
130
128
|
def receive_async(message)
|
131
|
-
|
129
|
+
@client.debug{"receive async: #{message}"}
|
132
130
|
Thread.new{
|
133
131
|
@handler.call(message)
|
134
132
|
}
|
data/lib/poliqarpr/exceptions.rb
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
module Poliqarp
|
2
2
|
# Author:: Aleksander Pohl (mailto:apohllo@o2.pl)
|
3
3
|
# License:: MIT License
|
4
|
+
|
5
|
+
# Base for all poliqarp exceptions.
|
6
|
+
class PoliqarpException < Exception; end
|
4
7
|
|
5
8
|
# The JobInProgress exception is raised if there was asynchronous call
|
6
9
|
# to the server which haven't finished, which is interrupted by another
|
7
10
|
# asynchronous call.
|
8
|
-
class JobInProgress <
|
11
|
+
class JobInProgress < PoliqarpException; end
|
9
12
|
|
10
13
|
# The IndexOutOfBounds exception is raised if the index of given excerpt
|
11
14
|
# is larger than the size of query buffer.
|
12
|
-
class IndexOutOfBounds <
|
15
|
+
class IndexOutOfBounds < PoliqarpException
|
16
|
+
def initialize(index)
|
17
|
+
super
|
18
|
+
@index = index
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"Poliqarp::IndexOutOfBounds(#{@index})"
|
23
|
+
end
|
24
|
+
end
|
13
25
|
end
|
data/poliqarpr.gemspec
CHANGED
data/spec/client.rb
CHANGED
@@ -28,6 +28,32 @@ describe Poliqarp::Client do
|
|
28
28
|
@client.version.should_not == nil
|
29
29
|
end
|
30
30
|
|
31
|
+
it "should work with debug turned on" do
|
32
|
+
@client.debug = true
|
33
|
+
(proc do
|
34
|
+
@client.ping
|
35
|
+
end).should_not raise_error(Exception)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should work with alternative logger" do
|
39
|
+
logger = ""
|
40
|
+
def logger.puts(str)
|
41
|
+
self << str
|
42
|
+
end
|
43
|
+
def logger.flush
|
44
|
+
end
|
45
|
+
@client.debug = true
|
46
|
+
@client.logger = logger
|
47
|
+
@client.ping
|
48
|
+
logger.size.should > 0
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should raise error if failed to open corpus" do
|
52
|
+
(proc do
|
53
|
+
@client.open_corpus("")
|
54
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
55
|
+
end
|
56
|
+
|
31
57
|
end
|
32
58
|
|
33
59
|
describe "(with 'sample' corpus)" do
|
@@ -48,13 +74,13 @@ describe Poliqarp::Client do
|
|
48
74
|
it "should raise error if the size of right context is not number" do
|
49
75
|
(proc do
|
50
76
|
@client.config.right_context_size = "a"
|
51
|
-
end).should raise_error(
|
77
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
52
78
|
end
|
53
79
|
|
54
80
|
it "should rais error if the size of right context is less or equal 0" do
|
55
81
|
(proc do
|
56
82
|
@client.config.right_context_size = 0
|
57
|
-
end).should raise_error(
|
83
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
58
84
|
end
|
59
85
|
|
60
86
|
it "should allow to set and get the left context size" do
|
@@ -65,13 +91,13 @@ describe Poliqarp::Client do
|
|
65
91
|
it "should raise error if the size of left context is not number" do
|
66
92
|
(lambda do
|
67
93
|
@client.config.left_context_size = "a"
|
68
|
-
end).should raise_error(
|
94
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
69
95
|
end
|
70
96
|
|
71
97
|
it "should rais error if the size of left context is less or equal 0" do
|
72
98
|
(lambda do
|
73
99
|
@client.config.left_context_size = 0
|
74
|
-
end).should raise_error(
|
100
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
75
101
|
end
|
76
102
|
|
77
103
|
it "should return corpus statistics" do
|
@@ -147,11 +173,11 @@ describe Poliqarp::Client do
|
|
147
173
|
@result.to_s.should == @client.find("nachalny")[0].to_s
|
148
174
|
end
|
149
175
|
|
150
|
-
it "should raise
|
176
|
+
it "should raise poliqarp error if the index is larger than the results count" do
|
151
177
|
count = @client.count("nachalny")
|
152
178
|
(proc do
|
153
179
|
@client.find("nachalny",:index => count+1)
|
154
|
-
end).should raise_error(Poliqarp::
|
180
|
+
end).should raise_error(Poliqarp::PoliqarpException)
|
155
181
|
end
|
156
182
|
|
157
183
|
it "should raise IndexOutOfBounds error if the index is larger than the buffer size" do
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poliqarpr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.6
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Aleksander Pohl
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
13
|
+
date: 2011-02-21 00:00:00 +01:00
|
18
14
|
default_executable:
|
19
15
|
dependencies: []
|
20
16
|
|
@@ -59,21 +55,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
55
|
requirements:
|
60
56
|
- - ">="
|
61
57
|
- !ruby/object:Gem::Version
|
62
|
-
segments:
|
63
|
-
- 0
|
64
58
|
version: "0"
|
65
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
60
|
none: false
|
67
61
|
requirements:
|
68
62
|
- - ">="
|
69
63
|
- !ruby/object:Gem::Version
|
70
|
-
segments:
|
71
|
-
- 0
|
72
64
|
version: "0"
|
73
65
|
requirements: []
|
74
66
|
|
75
67
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
68
|
+
rubygems_version: 1.5.2
|
77
69
|
signing_key:
|
78
70
|
specification_version: 3
|
79
71
|
summary: Ruby client for Poliqarp
|