FieldsWriter.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.util.Enumeration;
20 import java.io.IOException;
21 
22 import gate.creole.annic.apache.lucene.store.Directory;
23 import gate.creole.annic.apache.lucene.store.OutputStream;
24 import gate.creole.annic.apache.lucene.document.Document;
25 import gate.creole.annic.apache.lucene.document.Field;
26 
27 final class FieldsWriter {
28   private FieldInfos fieldInfos;
29   private OutputStream fieldsStream;
30   private OutputStream indexStream;
31 
32   FieldsWriter(Directory d, String segment, FieldInfos fn)
33        throws IOException {
34     fieldInfos = fn;
35     fieldsStream = d.createFile(segment + ".fdt");
36     indexStream = d.createFile(segment + ".fdx");
37   }
38 
39   final void close() throws IOException {
40     fieldsStream.close();
41     indexStream.close();
42   }
43 
44   final void addDocument(Document docthrows IOException {
45     indexStream.writeLong(fieldsStream.getFilePointer());
46 
47     int storedCount = 0;
48     Enumeration fields  = doc.fields();
49     while (fields.hasMoreElements()) {
50       Field field = (Field)fields.nextElement();
51       if (field.isStored())
52   storedCount++;
53     }
54     fieldsStream.writeVInt(storedCount);
55 
56     fields  = doc.fields();
57     while (fields.hasMoreElements()) {
58       Field field = (Field)fields.nextElement();
59       if (field.isStored()) {
60   fieldsStream.writeVInt(fieldInfos.fieldNumber(field.name()));
61 
62   byte bits = 0;
63   if (field.isTokenized())
64     bits |= 1;
65   fieldsStream.writeByte(bits);
66 
67   fieldsStream.writeString(field.stringValue());
68       }
69     }
70   }
71 }