FieldsReader.java
01 package gate.creole.annic.apache.lucene.index;
02 
03 /**
04  * Copyright 2004 The Apache Software Foundation
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 import java.io.IOException;
20 
21 import gate.creole.annic.apache.lucene.store.Directory;
22 import gate.creole.annic.apache.lucene.store.InputStream;
23 import gate.creole.annic.apache.lucene.document.Document;
24 import gate.creole.annic.apache.lucene.document.Field;
25 
26 /**
27  * Class responsible for access to stored document fields.
28  *
29  * It uses <segment>.fdt and <segment>.fdx; files.
30  *
31  @version $Id: FieldsReader.java 529 2004-10-05 11:55:26Z niraj $
32  */
33 final class FieldsReader {
34   private FieldInfos fieldInfos;
35   private InputStream fieldsStream;
36   private InputStream indexStream;
37   private int size;
38 
39   FieldsReader(Directory d, String segment, FieldInfos fnthrows IOException {
40     fieldInfos = fn;
41 
42     fieldsStream = d.openFile(segment + ".fdt");
43     indexStream = d.openFile(segment + ".fdx");
44 
45     size = (int)(indexStream.length() 8);
46   }
47 
48   final void close() throws IOException {
49     fieldsStream.close();
50     indexStream.close();
51   }
52 
53   final int size() {
54     return size;
55   }
56 
57   final Document doc(int nthrows IOException {
58     indexStream.seek(n * 8L);
59     long position = indexStream.readLong();
60     fieldsStream.seek(position);
61 
62     Document doc = new Document();
63     int numFields = fieldsStream.readVInt();
64     for (int i = 0; i < numFields; i++) {
65       int fieldNumber = fieldsStream.readVInt();
66       FieldInfo fi = fieldInfos.fieldInfo(fieldNumber);
67 
68       byte bits = fieldsStream.readByte();
69 
70       doc.add(new Field(fi.name,      // name
71       fieldsStream.readString()// read value
72       true,        // stored
73       fi.isIndexed,      // indexed
74       (bits & 1!= 0, fi.storeTermVector))// vector
75     }
76 
77     return doc;
78   }
79 }