| 1 | package Plucene::Index::FieldsWriter; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Plucene::Index::FieldsWriter - writes Fields to a Document | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | my $writer = Plucene::Index::FieldsWriter->new( | ||
| 10 | $dir_name, $segment, $field_infos); | ||
| 11 | |||
| 12 | $writer->add_document(Plucene::Document $doc); | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This class add documents to the appropriate files. | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | 17 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 17 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | 17 | ▶ | use Plucene::Store::OutputStream; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 17 | ▶ | use Plucene::Index::FieldInfos; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | =head2 new | ||
| 29 | |||
| 30 | my $writer = Plucene::Index::FieldsWriter->new( | ||
| 31 | $dir_name, $segment, $field_infos); | ||
| 32 | |||
| 33 | This will create a new Plucene::Index::FieldsWriter object with the passed | ||
| 34 | directory name, segment and field infos. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | # private FieldInfos fieldInfos; | ||
| 39 | # private OutputStream fieldsStream; | ||
| 40 | # private OutputStream indexStream; | ||
| 41 | |||
| 42 | # FieldsWriter(Directory d, String segment, FieldInfos fn) | ||
| 43 | # throws IOException { | ||
| 44 | # fieldInfos = fn; | ||
| 45 | # fieldsStream = d.createFile(segment + ".fdt"); | ||
| 46 | # indexStream = d.createFile(segment + ".fdx"); | ||
| 47 | # } | ||
| 48 | |||
| 49 | 296 | ▶ | sub new { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 50 | 296 | my ($self, $d, $segment, $fn) = @_; | |
| 51 | 296 | bless { | |
| 52 | field_infos => $fn, | ||
| 53 | segment => $segment, | ||
| 54 | fields_stream => Plucene::Store::OutputStream->new("$d/$segment.fdt"), | ||
| 55 | index_stream => Plucene::Store::OutputStream->new("$d/$segment.fdx"), | ||
| 56 | }, $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 close | ||
| 60 | |||
| 61 | $writer->close; | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | # final void close() throws IOException { | ||
| 66 | # fieldsStream.close(); | ||
| 67 | # indexStream.close(); | ||
| 68 | # } | ||
| 69 | |||
| 70 | 0 | ▶ | sub close { |
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 71 | 0 | my $self = shift; | |
| 72 | 0 | $self->{fields_stream}->close; | |
| 73 | 0 | $self->{index_stream}->close; | |
| 74 | } | ||
| 75 | |||
| 76 | =head2 add_document | ||
| 77 | |||
| 78 | $writer->add_document(Plucene::Document $doc); | ||
| 79 | |||
| 80 | This will add the passed Plucene::Document. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | # final void addDocument(Document doc) throws IOException { | ||
| 85 | # indexStream.writeLong(fieldsStream.getFilePointer()); | ||
| 86 | # | ||
| 87 | # int storedCount = 0; | ||
| 88 | # Enumeration fields = doc.fields(); | ||
| 89 | # while (fields.hasMoreElements()) { | ||
| 90 | # Field field = (Field)fields.nextElement(); | ||
| 91 | # if (field.isStored()) | ||
| 92 | # storedCount++; | ||
| 93 | # } | ||
| 94 | # fieldsStream.writeVInt(storedCount); | ||
| 95 | # | ||
| 96 | # fields = doc.fields(); | ||
| 97 | # while (fields.hasMoreElements()) { | ||
| 98 | # Field field = (Field)fields.nextElement(); | ||
| 99 | # if (field.isStored()) { | ||
| 100 | # fieldsStream.writeVInt(fieldInfos.fieldNumber(field.name())); | ||
| 101 | # | ||
| 102 | # byte bits = 0; | ||
| 103 | # if (field.isTokenized()) | ||
| 104 | # bits |= 1; | ||
| 105 | # fieldsStream.writeByte(bits); | ||
| 106 | # | ||
| 107 | # fieldsStream.writeString(field.stringValue()); | ||
| 108 | # } | ||
| 109 | # } | ||
| 110 | # } | ||
| 111 | |||
| 112 | 646 | ▶ | sub add_document { |
|
Subroutine
add_document: called CC=2 SCAR=6.9
Pod
documented
| |||
| 113 | 646 | my ($self, $doc) = @_; | |
| 114 | 646 | $self->{index_stream}->write_long($self->{fields_stream}->tell); | |
| 115 | 646 | my @stored = grep $_->is_stored, $doc->fields; | |
| 116 | 646 | $self->{fields_stream}->write_vint(scalar @stored); | |
| 117 | 646 | for my $field (@stored) { | |
| 118 | $self->{fields_stream} | ||
| 119 | 816 | ->write_vint($self->{field_infos}->field_number($field->name)); | |
| 120 | 816 | $self->{fields_stream}->print(chr($field->is_tokenized)); | |
| 121 | 816 | $self->{fields_stream}->write_string($field->string); | |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | 17 | 1; | |