This file is indexed.

/usr/lib/perl5/KinoSearch1/Index/SegTermEnum.pm is in libkinosearch1-perl 1.00-1build3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package KinoSearch1::Index::SegTermEnum;
use strict;
use warnings;
use KinoSearch1::Util::ToolSet;
use base qw( KinoSearch1::Util::Class );

BEGIN {
    __PACKAGE__->init_instance_vars(
        # constructor params
        finfos   => undef,
        instream => undef,
        is_index => 0,
    );
}
our %instance_vars;

use KinoSearch1::Index::Term;
use KinoSearch1::Index::TermInfo;
use KinoSearch1::Index::TermBuffer;

sub new {
    # verify params
    my $ignore = shift;
    my %args = ( %instance_vars, @_ );
    confess kerror() unless verify_args( \%instance_vars, %args );

    # get a TermBuffer helper object
    my $term_buffer
        = KinoSearch1::Index::TermBuffer->new( finfos => $args{finfos}, );

    return _new_helper( @args{ 'instream', 'is_index', 'finfos', },
        $term_buffer );
}

sub clone_enum {
    my $self = shift;

    # dupe instream and seek it to the start of the file, so init works right
    my $instream   = $self->_get_instream;
    my $new_stream = $instream->clone_stream;
    $new_stream->seek(0);

    # create a new object and seek it to the right term/terminfo
    my $evil_twin = __PACKAGE__->new(
        finfos   => $self->_get_finfos,
        instream => $new_stream,
        is_index => $self->is_index,
    );
    $evil_twin->seek(
        $instream->tell,       $self->_get_position,
        $self->get_termstring, $self->get_term_info
    );
    return $evil_twin;
}

# Locate the Enum to a particular spot.
sub seek {
    my ( $self, $pointer, $position, $termstring, $tinfo ) = @_;

    # seek the filehandle
    my $instream = $self->_get_instream;
    $instream->seek($pointer);

    # set values as if we'd scanned here from the start of the Enum
    $self->_set_position($position);
    $self->_set_termstring($termstring);
    $self->_set_term_info($tinfo);
}

sub close {
    my $instream = $_[0]->_get_instream;
    $instream->close;
}

# return a Term, if the Enum is currently valid.
sub get_term {
    my $self       = shift;
    my $termstring = $self->get_termstring;
    return unless defined $termstring;
    return KinoSearch1::Index::Term->new_from_string( $termstring,
        $self->_get_finfos );
}

1;

__END__

__XS__

MODULE = KinoSearch1   PACKAGE = KinoSearch1::Index::SegTermEnum 


SegTermEnum*
_new_helper(instream_sv, is_index, finfos_sv, term_buffer_sv)
    SV         *instream_sv;
    I32         is_index;
    SV         *finfos_sv
    SV         *term_buffer_sv;
CODE:
    RETVAL = Kino1_SegTermEnum_new_helper(instream_sv, is_index, finfos_sv,
        term_buffer_sv);
OUTPUT: RETVAL


=for comment

fill_cache() loads the entire Enum into memory.  This should only be called
for index Enums -- never for primary Enums.

=cut

void
fill_cache(obj)
    SegTermEnum *obj;
PPCODE:
    Kino1_SegTermEnum_fill_cache(obj);


=begin comment

scan_to() iterates through the Enum until the Enum's state is ge the target.
This is called on the main Enum, after seek() has gotten it close.  You don't
want to scan through the entire main Enum, just through a small part.

Scanning through an Enum is an involved process, due to the heavy data
compression.  See the Java Lucene File Format definition for details.

=end comment
=cut

void
scan_to(obj, target_termstring_sv)
    SegTermEnum *obj;
    SV          *target_termstring_sv;
PREINIT:
    char *ptr;
    STRLEN len;
PPCODE:
    ptr = SvPV(target_termstring_sv, len);
    if (len < 2)
        Kino1_confess("length of termstring < 2: %"UVuf, (UV)len);
    Kino1_SegTermEnum_scan_to(obj, ptr, len);


=for comment

Reset the Enum to the top, so that after next() is called, the Enum is located
at the first term in the segment.

=cut

void
reset(obj)
    SegTermEnum *obj;
PPCODE:
    Kino1_SegTermEnum_reset(obj);


=for comment

next() advances the state of the Enum one term.  If the current position of
the Enum is valid, it returns 1; when the Enum is exhausted, it returns 0.

=cut

IV
next(obj)
    SegTermEnum *obj;
CODE:
    RETVAL = Kino1_SegTermEnum_next(obj);
OUTPUT: RETVAL


=for comment

For an Enum which has been loaded into memory, scan to the target as quickly
as possible.

=cut

I32
scan_cache(obj, target_termstring_sv)
    SegTermEnum  *obj;
    SV           *target_termstring_sv;
PREINIT:
    char *ptr;
    STRLEN len;
CODE:
    ptr = SvPV(target_termstring_sv, len);
    if (len < 2)
        Kino1_confess("length of termstring < 2: %"UVuf, (UV)len);
    RETVAL = Kino1_SegTermEnum_scan_cache(obj, ptr, len);
OUTPUT: RETVAL


=for comment

Setters and getters for members in the SegTermEnum struct. Not all of these 
are useful.

=cut

SV*
_set_or_get(obj, ...)
    SegTermEnum *obj;
ALIAS:
        _set_instream        = 1
        _get_instream        = 2
        _set_finfos          = 3
        _get_finfos          = 4
        _set_size            = 5
    get_size                 = 6  
        _set_termstring      = 7
    get_termstring           = 8
        _set_term_info       = 9
    get_term_info            = 10
        _set_index_interval  = 11
    get_index_interval       = 12
        _set_skip_interval   = 13
    get_skip_interval        = 14
        _set_position        = 15
        _get_position        = 16
        _set_is_index        = 17
    is_index                 = 18
CODE:
{
    KINO_START_SET_OR_GET_SWITCH

    case 0:  croak("can't call _get_or_set on it's own");
             break; /* probably unreachable */

    case 1:  SvREFCNT_dec(obj->instream_sv);
             obj->instream_sv = newSVsv( ST(1) );
             /* fall through */
    case 2:  RETVAL = newSVsv(obj->instream_sv); 
             break;

    case 3:  SvREFCNT_dec(obj->finfos);
             obj->finfos = newSVsv( ST(1) );
             /* fall through */
    case 4:  RETVAL = newSVsv(obj->finfos); 
             break;

    case 5:  obj->enum_size = (I32)SvIV( ST(1) ); 
             /* fall through */
    case 6:  RETVAL = newSViv(obj->enum_size); 
             break;

    case 7:  if ( SvOK( ST(1) ) ) {
                 STRLEN len = SvCUR( ST(1) );
                 if (len < KINO_FIELD_NUM_LEN)
                    Kino1_confess("Internal error: termstring too short");
                 Kino1_TermBuf_set_termstring(obj->term_buf, 
                    SvPVX(ST(1)), len);
             }
             else {
                 Kino1_TermBuf_reset(obj->term_buf);
             }
             /* fall through */
    case 8:  RETVAL = (obj->term_buf->termstring == NULL) 
                 ? &PL_sv_undef
                 : newSVpv( obj->term_buf->termstring->ptr,
                     obj->term_buf->termstring->size ); 
             break;

    case 9:  {
                TermInfo* new_tinfo;
                Kino1_extract_struct( ST(1), new_tinfo, TermInfo*, 
                    "KinoSearch1::Index::TermInfo");
                Kino1_TInfo_destroy(obj->tinfo);
                obj->tinfo = Kino1_TInfo_dupe(new_tinfo);
             }
             /* fall through */
    case 10: {
                TermInfo* new_tinfo;
                RETVAL = newSV(0);
                new_tinfo = Kino1_TInfo_dupe(obj->tinfo);
                sv_setref_pv(RETVAL, "KinoSearch1::Index::TermInfo", 
                              (void*)new_tinfo);
             }
             break;

    case 11: obj->index_interval = SvIV( ST(1) );
             /* fall through */
    case 12: RETVAL = newSViv(obj->index_interval);
             break;

    case 13: obj->skip_interval = SvIV( ST(1) );
             /* fall through */
    case 14: RETVAL = newSViv(obj->skip_interval);
             break;

    case 15: obj->position = SvIV( ST(1) );
             /* fall through */
    case 16: RETVAL = newSViv(obj->position);
             break;

    case 17: Kino1_confess("can't set is_index");
             /* fall through */
    case 18: RETVAL = newSViv(obj->is_index);
             break;
    
    KINO_END_SET_OR_GET_SWITCH
}
OUTPUT: RETVAL


void
DESTROY(obj)
    SegTermEnum* obj;
PPCODE:
    Kino1_SegTermEnum_destroy(obj);

__H__

#ifndef H_KINOSEARCH_INDEX_SEG_TERM_ENUM
#define H_KINOSEARCH_INDEX_SEG_TERM_ENUM 1

#include "EXTERN.h"
#include "perl.h"
#include "KinoSearch1IndexTermBuffer.h"
#include "KinoSearch1IndexTermInfo.h"
#include "KinoSearch1StoreInStream.h"
#include "KinoSearch1UtilByteBuf.h"
#include "KinoSearch1UtilCarp.h"
#include "KinoSearch1UtilCClass.h"
#include "KinoSearch1UtilMemManager.h"
#include "KinoSearch1UtilStringHelper.h"

typedef struct segtermenum {
    SV         *finfos;
    SV         *instream_sv;
    SV         *term_buf_ref;
    TermBuffer *term_buf;
    TermInfo   *tinfo;
    InStream   *instream;
    I32         is_index;
    I32         enum_size;
    I32         position;
    I32         index_interval;
    I32         skip_interval;
    ByteBuf   **termstring_cache;
    TermInfo  **tinfos_cache;
} SegTermEnum;


SegTermEnum* Kino1_SegTermEnum_new_helper(SV*, I32, SV*, SV*);
void Kino1_SegTermEnum_reset(SegTermEnum*);
I32  Kino1_SegTermEnum_next(SegTermEnum*);
void Kino1_SegTermEnum_fill_cache(SegTermEnum*);
void Kino1_SegTermEnum_scan_to(SegTermEnum*, char*, I32);
I32  Kino1_SegTermEnum_scan_cache(SegTermEnum*, char*, I32);
void Kino1_SegTermEnum_destroy(SegTermEnum*);

#endif /* include guard */

__C__

#include "KinoSearch1IndexSegTermEnum.h"

SegTermEnum*
Kino1_SegTermEnum_new_helper(SV *instream_sv, I32 is_index, SV *finfos_sv,
                            SV *term_buffer_sv) {
    I32           format;
    InStream     *instream;
    SegTermEnum  *obj;

    /* allocate */
    Kino1_New(0, obj, 1, SegTermEnum);
    obj->tinfo = Kino1_TInfo_new();

    /* init */
    obj->tinfos_cache     = NULL;
    obj->termstring_cache = NULL;

    /* save instream, finfos, and term_buffer, incrementing refcounts */
    obj->instream_sv  = newSVsv(instream_sv);
    obj->finfos       = newSVsv(finfos_sv);
    obj->term_buf_ref = newSVsv(term_buffer_sv);
    Kino1_extract_struct(term_buffer_sv, obj->term_buf, TermBuffer*, 
        "KinoSearch1::Index::TermBuffer");
    Kino1_extract_struct(instream_sv, obj->instream, InStream*, 
        "KinoSearch1::Store::InStream");
    instream = obj->instream;

    /* determine whether this is a primary or index enum */
    obj->is_index = is_index;

    /* reject older or newer index formats */
    format = (I32)instream->read_int(instream);
    if (format != -2)
        Kino1_confess("Unsupported index format: %d", format);

    /* read in some vars */
    obj->enum_size      = instream->read_long(instream);
    obj->index_interval = instream->read_int(instream);
    obj->skip_interval  = instream->read_int(instream);

    /* define the position of the Enum as "not yet started" */
    obj->position = -1;
    
    return obj;
}

#define KINO_SEG_TERM_ENUM_HEADER_LEN 20 

void
Kino1_SegTermEnum_reset(SegTermEnum* obj) {
    obj->position = -1;
    obj->instream->seek(obj->instream, KINO_SEG_TERM_ENUM_HEADER_LEN);
    Kino1_TermBuf_reset(obj->term_buf);
    Kino1_TInfo_reset(obj->tinfo);
}

I32 
Kino1_SegTermEnum_next(SegTermEnum *obj) {
    InStream *instream;
    TermInfo *tinfo;

    /* make some local copies for clarity of code */
    instream = obj->instream;
    tinfo    = obj->tinfo;

    /* if we've run out of terms, null out the termstring and return */
    if (++obj->position >= obj->enum_size) {
        Kino1_TermBuf_reset(obj->term_buf);
        return 0;
    }

    /* read in the term */
    Kino1_TermBuf_read(obj->term_buf, instream);

    /* read doc freq */
    tinfo->doc_freq = instream->read_vint(instream);

    /* adjust file pointers. */
    tinfo->frq_fileptr += instream->read_vlong(instream);
    tinfo->prx_fileptr += instream->read_vlong(instream);

    /* read skip data (which doesn't do anything right now) */
    if (tinfo->doc_freq >= obj->skip_interval)
        tinfo->skip_offset = instream->read_vint(instream);
    else
        tinfo->skip_offset = 0;

    /* read filepointer to main enum if this is an index enum */
    if (obj->is_index)
        tinfo->index_fileptr += instream->read_vlong(instream);

    return 1;
}

void
Kino1_SegTermEnum_fill_cache(SegTermEnum* obj) {
    TermBuffer  *term_buf;
    TermInfo    *tinfo;
    TermInfo   **tinfos_cache;
    ByteBuf    **termstring_cache;

    /* allocate caches */
    if (obj->tinfos_cache != NULL)
        Kino1_confess("Internal error: cache already filled");
    Kino1_New(0, obj->termstring_cache, obj->enum_size, ByteBuf*); 
    Kino1_New(0, obj->tinfos_cache, obj->enum_size, TermInfo*);

    /* make some local copies */
    tinfo                = obj->tinfo;
    term_buf             = obj->term_buf;
    tinfos_cache         = obj->tinfos_cache;
    termstring_cache     = obj->termstring_cache;

    while (Kino1_SegTermEnum_next(obj)) {
        /* copy tinfo and termstring into caches */
        *tinfos_cache++     = Kino1_TInfo_dupe(tinfo);
        *termstring_cache++ = Kino1_BB_clone(term_buf->termstring);
    }
}

void
Kino1_SegTermEnum_scan_to(SegTermEnum *obj, char *target_termstring, 
                         I32 target_termstring_len) {
    TermBuffer *term_buf = obj->term_buf;
    ByteBuf     target;

    /* make convenience copies */
    target.ptr  = target_termstring;
    target.size = target_termstring_len;

    /* keep looping until the termstring is lexically ge target */
    do {
        const I32 comparison = Kino1_BB_compare(term_buf->termstring, &target);

        if ( comparison >= 0 &&  obj->position != -1) {
            break;
        }
    } while (Kino1_SegTermEnum_next(obj));
}

I32
Kino1_SegTermEnum_scan_cache(SegTermEnum *obj, char *target_termstring, 
                            I32 target_len) {
    TermBuffer  *term_buf = obj->term_buf;
    ByteBuf    **termstrings = obj->termstring_cache;
    ByteBuf      target;
    I32          lo       = 0;
    I32          hi       = obj->enum_size - 1;
    I32          result   = -100;
    I32          mid, comparison;

    /* make convenience copies */
    target.ptr  = target_termstring;
    target.size = target_len;
    if (obj->tinfos_cache == NULL)
        Kino1_confess("Internal Error: fill_cache hasn't been called yet"); 
    
    /* divide and conquer */
    while (hi >= lo) {
        mid        = (lo + hi) >> 1;
        comparison = Kino1_BB_compare(&target, termstrings[mid]);
        if (comparison < 0) 
            hi = mid - 1;
        else if (comparison > 0)
            lo = mid + 1;
        else {
            result = mid;
            break;
        }
    }
    result = hi     == -1   ? 0  /* indicating that target lt first entry */
           : result == -100 ? hi /* if result is still -100, it wasn't set */
           : result;
    
    /* set the state of the Enum/TermBuffer as if we'd called scan_to */
    obj->position  = result;
    Kino1_TermBuf_set_termstring(term_buf, termstrings[result]->ptr,
        termstrings[result]->size);
    Kino1_TInfo_destroy(obj->tinfo);
    obj->tinfo = Kino1_TInfo_dupe( obj->tinfos_cache[result] );

    return result;
}

void
Kino1_SegTermEnum_destroy(SegTermEnum *obj) {
    /* put out the garbage for collection */
    SvREFCNT_dec(obj->finfos);
    SvREFCNT_dec(obj->instream_sv);
    SvREFCNT_dec(obj->term_buf_ref);

    Kino1_TInfo_destroy(obj->tinfo);

    /* if fill_cache was called, free all of that... */
    if (obj->tinfos_cache != NULL) {
        I32         iter;
        ByteBuf   **termstring_cache = obj->termstring_cache;
        TermInfo  **tinfos_cache     = obj->tinfos_cache;
        for (iter = 0; iter < obj->enum_size; iter++) {
            Kino1_BB_destroy(*termstring_cache++);
            Kino1_TInfo_destroy(*tinfos_cache++);
        }
        Kino1_Safefree(obj->tinfos_cache);
        Kino1_Safefree(obj->termstring_cache);
    }

    /* last, the SegTermEnum object itself */
    Kino1_Safefree(obj);
}


__POD__

=begin devdocs

=head1 NAME

KinoSearch1::Index::SegTermEnum - single-segment TermEnum

=head1 DESCRIPTION

Single-segment implementation of KinoSearch1::Index::TermEnum.

=head1 COPYRIGHT

Copyright 2005-2010 Marvin Humphrey

=head1 LICENSE, DISCLAIMER, BUGS, etc.

See L<KinoSearch1> version 1.00.

=end devdocs
=cut