poliqarpr 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/changelog.txt CHANGED
@@ -1,3 +1,7 @@
1
+ 0.1.8
2
+ - fix subsequent single result queries
3
+ - always cancel previous job before starting a new one
4
+
1
5
  0.1.7
2
6
  - speed up querying of specific results
3
7
 
@@ -28,7 +28,8 @@ module Poliqarp
28
28
  @connector = Connector.new(self)
29
29
  @config = Config.new(self,500000)
30
30
  @answer_queue = Queue.new
31
- @waiting_mutext = Mutex.new
31
+ @waiting_mutex = Mutex.new
32
+ @query_mutex = Mutex.new
32
33
  new_session
33
34
  config.left_context_size = 5
34
35
  config.right_context_size = 5
@@ -380,24 +381,34 @@ protected
380
381
 
381
382
  # *Asynchronous* Sends the query to the server
382
383
  # * +query+ query to send
384
+ # * +should_wait+ flag indicating that before sending the
385
+ # query to the server, the receiver should start wating
386
+ # (i.e. the call is async).
383
387
  # * +handler+ if given, the method returns immediately,
384
388
  # and the answer is sent to the handler. In this case
385
389
  # the result returned by make_query should be IGNORED!
386
- def make_query(query, &handler)
390
+ def make_query(query, should_wait=false,&handler)
387
391
  if @last_query != query
388
- @last_query = query
389
- begin
390
- talk("MAKE-QUERY #{query}")
391
- rescue JobInProgress
392
- talk("CANCEL") rescue nil
393
- talk("MAKE-QUERY #{query}")
392
+ if @last_query != nil
393
+ begin
394
+ talk("CANCEL")
395
+ stop_waiting
396
+ finish_query
397
+ rescue InvalidJobId
398
+ # this is ok - there might be no job running
399
+ end
394
400
  end
401
+ @last_query = query
402
+ talk("MAKE-QUERY #{query}")
403
+ start_waiting if should_wait
404
+ run_query
395
405
  result = talk("RUN-QUERY #{config.buffer_size}", :async, &handler)
396
406
  if handler.nil?
397
407
  @last_result = result
398
408
  end
399
409
  else
400
- stop_waiting
410
+ start_waiting if should_wait
411
+ stop_waiting if query_done?
401
412
  end
402
413
  @last_result
403
414
  end
@@ -416,7 +427,7 @@ protected
416
427
 
417
428
  # Stop waiting for the ansynchonous answer.
418
429
  def stop_waiting
419
- @waiting_mutext.synchronize {
430
+ @waiting_mutex.synchronize {
420
431
  @should_wait = false
421
432
  }
422
433
  debug("WAITING stopped")
@@ -425,7 +436,7 @@ protected
425
436
  # Check if the thread should still wait for the answer.
426
437
  def should_wait?
427
438
  should_wait = nil
428
- @waiting_mutext.synchronize {
439
+ @waiting_mutex.synchronize {
429
440
  should_wait = @should_wait
430
441
  }
431
442
  should_wait
@@ -433,23 +444,49 @@ protected
433
444
 
434
445
  # Start waiting for the answer.
435
446
  def start_waiting
436
- @waiting_mutext.synchronize {
447
+ @waiting_mutex.synchronize {
437
448
  @should_wait = true
438
449
  }
439
450
  debug("WAITING started")
440
451
  end
441
452
 
453
+ def run_query
454
+ @query_mutex.synchronize {
455
+ @query_done = false
456
+ }
457
+ debug("QUERY started")
458
+ end
459
+
460
+ def query_done?
461
+ done = nil
462
+ @query_mutex.synchronize {
463
+ done = @query_done
464
+ }
465
+ done
466
+ end
467
+
468
+ def finish_query
469
+ @query_mutex.synchronize {
470
+ @query_done = true
471
+ }
472
+ debug("QUERY finished")
473
+ end
474
+
442
475
  def make_async_query(query,answer_offset)
443
- raise IndexOutOfBounds.new(answer_offset) if answer_offset > config.buffer_size
444
- start_waiting
476
+ raise IndexOutOfBounds.new(answer_offset) if answer_offset >= config.buffer_size
445
477
  # we access the result count through BUFFER-STATE call
446
- make_query(query){|msg| stop_waiting}
478
+ make_query(query,true) do |msg|
479
+ if msg =~ /QUERY-DONE/
480
+ stop_waiting
481
+ finish_query
482
+ end
483
+ end
447
484
  result_count = 0
448
485
  begin
449
486
  # the result count might be not exact!
450
487
  result_count = talk("BUFFER-STATE").split(" ")[2].to_i
451
488
  break unless should_wait?
452
- end while result_count < answer_offset
489
+ end while result_count <= answer_offset
453
490
  # force stop waiting if the result offset is reached before the query has finished
454
491
  stop_waiting
455
492
  @last_result = "OK #{result_count}"
@@ -85,6 +85,8 @@ module Poliqarp
85
85
  if message =~ /^ERR/
86
86
  code = message.match(/\d+/)[0].to_i
87
87
  case code
88
+ when 14
89
+ raise InvalidJobId.new()
88
90
  when 15
89
91
  raise JobInProgress.new()
90
92
  else
@@ -1,7 +1,7 @@
1
1
  module Poliqarp
2
2
  # Author:: Aleksander Pohl (mailto:apohllo@o2.pl)
3
3
  # License:: MIT License
4
-
4
+
5
5
  # Base for all poliqarp exceptions.
6
6
  class PoliqarpException < Exception; end
7
7
 
@@ -10,6 +10,9 @@ module Poliqarp
10
10
  # asynchronous call.
11
11
  class JobInProgress < PoliqarpException; end
12
12
 
13
+ # The InvalidJobId exceptions is raised when there is no job to be cancelled.
14
+ class InvalidJobId < PoliqarpException; end
15
+
13
16
  # The IndexOutOfBounds exception is raised if the index of given excerpt
14
17
  # is larger than the size of query buffer.
15
18
  class IndexOutOfBounds < PoliqarpException
data/poliqarpr.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "poliqarpr"
3
- s.version = "0.1.7"
3
+ s.version = "0.1.8"
4
4
  s.date = Time.now.strftime("%Y-%m-%d")
5
5
  s.summary = "Ruby client for Poliqarp"
6
6
  s.email = "apohllo@o2.pl"
data/spec/client.rb CHANGED
@@ -30,7 +30,7 @@ describe Poliqarp::Client do
30
30
 
31
31
  it "should work with debug turned on" do
32
32
  @client.debug = true
33
- (proc do
33
+ (proc do
34
34
  @client.ping
35
35
  end).should_not raise_error(Exception)
36
36
  end
@@ -49,7 +49,7 @@ describe Poliqarp::Client do
49
49
  end
50
50
 
51
51
  it "should raise error if failed to open corpus" do
52
- (proc do
52
+ (proc do
53
53
  @client.open_corpus("")
54
54
  end).should raise_error(Poliqarp::PoliqarpException)
55
55
  end
@@ -186,6 +186,18 @@ describe Poliqarp::Client do
186
186
  @client.find("nachalny",:index => 2)
187
187
  end).should raise_error(Poliqarp::IndexOutOfBounds)
188
188
  end
189
+
190
+ it "should work for examples with distant index" do
191
+ @client.config.buffer_size = 10000
192
+ @client.find("i",:index => 100)
193
+ end
194
+
195
+ it "should work for subseqent examples with distant indices" do
196
+ @client.config.buffer_size = 10001
197
+ @client.find("a",:index => 1)
198
+ @client.find("a",:index => 10000)
199
+ end
200
+
189
201
  end
190
202
 
191
203
  describe("(with lemmata flags set to true)") do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: poliqarpr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.7
5
+ version: 0.1.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Aleksander Pohl
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-22 00:00:00 +01:00
13
+ date: 2011-03-08 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16