KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > FieldsWriter


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
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, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations under
16  * the License.
17  */

18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.zip.Deflater JavaDoc;
23
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.store.IndexOutput;
28
29 final class FieldsWriter
30 {
31   static final byte FIELD_IS_TOKENIZED = 0x1;
32   static final byte FIELD_IS_BINARY = 0x2;
33   static final byte FIELD_IS_COMPRESSED = 0x4;
34   
35     private FieldInfos fieldInfos;
36
37     private IndexOutput fieldsStream;
38
39     private IndexOutput indexStream;
40
41     FieldsWriter(Directory d, String JavaDoc segment, FieldInfos fn) throws IOException JavaDoc {
42         fieldInfos = fn;
43         fieldsStream = d.createOutput(segment + ".fdt");
44         indexStream = d.createOutput(segment + ".fdx");
45     }
46
47     final void close() throws IOException JavaDoc {
48         fieldsStream.close();
49         indexStream.close();
50     }
51
52     final void addDocument(Document doc) throws IOException JavaDoc {
53         indexStream.writeLong(fieldsStream.getFilePointer());
54
55         int storedCount = 0;
56         Enumeration JavaDoc fields = doc.fields();
57         while (fields.hasMoreElements()) {
58             Field field = (Field) fields.nextElement();
59             if (field.isStored())
60                 storedCount++;
61         }
62         fieldsStream.writeVInt(storedCount);
63
64         fields = doc.fields();
65         while (fields.hasMoreElements()) {
66             Field field = (Field) fields.nextElement();
67             if (field.isStored()) {
68                 fieldsStream.writeVInt(fieldInfos.fieldNumber(field.name()));
69
70                 byte bits = 0;
71                 if (field.isTokenized())
72                     bits |= FieldsWriter.FIELD_IS_TOKENIZED;
73                 if (field.isBinary())
74                     bits |= FieldsWriter.FIELD_IS_BINARY;
75                 if (field.isCompressed())
76                     bits |= FieldsWriter.FIELD_IS_COMPRESSED;
77                 
78                 fieldsStream.writeByte(bits);
79                 
80                 if (field.isCompressed()) {
81                   // compression is enabled for the current field
82
byte[] data = null;
83                   // check if it is a binary field
84
if (field.isBinary()) {
85                     data = compress(field.binaryValue());
86                   }
87                   else {
88                     data = compress(field.stringValue().getBytes("UTF-8"));
89                   }
90                   final int len = data.length;
91                   fieldsStream.writeVInt(len);
92                   fieldsStream.writeBytes(data, len);
93                 }
94                 else {
95                   // compression is disabled for the current field
96
if (field.isBinary()) {
97                     byte[] data = field.binaryValue();
98                     final int len = data.length;
99                     fieldsStream.writeVInt(len);
100                     fieldsStream.writeBytes(data, len);
101                   }
102                   else {
103                     fieldsStream.writeString(field.stringValue());
104                   }
105                 }
106             }
107         }
108     }
109
110     private final byte[] compress (byte[] input) {
111
112       // Create the compressor with highest level of compression
113
Deflater JavaDoc compressor = new Deflater JavaDoc();
114       compressor.setLevel(Deflater.BEST_COMPRESSION);
115
116       // Give the compressor the data to compress
117
compressor.setInput(input);
118       compressor.finish();
119
120       /*
121        * Create an expandable byte array to hold the compressed data.
122        * You cannot use an array that's the same size as the orginal because
123        * there is no guarantee that the compressed data will be smaller than
124        * the uncompressed data.
125        */

126       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(input.length);
127
128       // Compress the data
129
byte[] buf = new byte[1024];
130       while (!compressor.finished()) {
131         int count = compressor.deflate(buf);
132         bos.write(buf, 0, count);
133       }
134       
135       compressor.end();
136
137       // Get the compressed data
138
return bos.toByteArray();
139     }
140 }
141
Popular Tags