blob: 605722e278f5dce4343661b4e55d428710b48665 [file] [log] [blame]
Akron5f9091c2017-03-24 20:37:35 +01001package Krawfish::Corpus::Negation;
2use Krawfish::Index::PostingsList;
3use Krawfish::Posting::Doc;
4use Krawfish::Query::Nothing;
5use Krawfish::Log;
6use strict;
7use warnings;
8
Akron2ea61aa2017-06-03 16:30:23 +02009
10# TODO: Remove in favor of WithOut!
11
12
13
Akron5f9091c2017-03-24 20:37:35 +010014use constant DEBUG => 0;
15
16# TODO: Support deleted docs
17# Use PostingsLive!
18
19sub new {
20 my ($class, $index, $query) = @_;
21
22 bless {
23 query => $query,
24 doc_id => -1,
25 last_doc_id => $index->last_doc
26 }, $class;
27};
28
29sub _init {
30 return if $_[0]->{init}++;
31 if (DEBUG) {
32 print_log('kq_neg', 'Initialize field') if DEBUG;
33 };
34 $_[0]->{query}->next;
35};
36
37sub next {
38 my $self = shift;
39 $self->_init;
40
41 return unless defined $self->{doc_id};
42
43 while (1) {
44 $self->{doc_id}++;
45
46 print_log('kq_neg', 'Check doc id ' . $self->{doc_id}) if DEBUG;
47
48 my $check = $self->_check;
49 return if $check == 0;
50 return 1 if $check == 1;
51
52 # Advance negative postings
53 $self->{query}->next;
54 };
55
56# print_log('vc_neg', 'Next "'.$self->term.'"') if DEBUG;
57#
58# my $return = $self->{postings}->next;
59# if (DEBUG) {
60# print_log('field', ' - current is ' . $self->current) if $return;
61# print_log('field', ' - no current');
62# };
63# return $return;
64};
65
66sub _check {
67 my $self = shift;
68
69 my $next_neg = $self->{query}->current;
70
71 # The current element is negated
72 if ($next_neg && $self->{doc_id} == $next_neg->doc_id) {
73 print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is negated') if DEBUG;
74 return -1
75 }
76
77 # Fine - and not at the end of the index
78 elsif ($self->{doc_id} < $self->{last_doc_id}) {
79 print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is fine') if DEBUG;
80 return 1;
81 }
82
83 # Reached the end of the index
84 else {
85 print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is beyond index') if DEBUG;
86 $self->{doc_id} = undef;
87 return 0;
88 };
89}
90
91sub current {
92 my $self = shift;
93 return if !defined $self->{doc_id} || $self->{doc_id} == -1;
94 Krawfish::Posting::Doc->new(
95 $self->{doc_id}
96 );
97}
98
99sub freq {
Akrond3355ba2017-05-17 21:16:35 +0200100 my $self = shift;
101 $self->{last_doc_id} - $self->{query}->freq;
Akron5f9091c2017-03-24 20:37:35 +0100102};
103
104sub to_string {
105 return "not(" . $_[0]->{query}->to_string . ")";
106};
107
1081;