cvsimport
[beagle.git] / beagled / Lucene.Net / Index / FieldsWriter.cs
blob779f93aa3e52b2b8eb571624e45d4a38b12a4b22
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using Document = Lucene.Net.Documents.Document;
19 using Field = Lucene.Net.Documents.Field;
20 using Directory = Lucene.Net.Store.Directory;
21 using IndexOutput = Lucene.Net.Store.IndexOutput;
23 namespace Lucene.Net.Index
26 sealed class FieldsWriter
28 internal const byte FIELD_IS_TOKENIZED = (byte) (0x1);
29 internal const byte FIELD_IS_BINARY = (byte) (0x2);
30 internal const byte FIELD_IS_COMPRESSED = (byte) (0x4);
32 private FieldInfos fieldInfos;
34 private IndexOutput fieldsStream;
36 private IndexOutput indexStream;
38 internal FieldsWriter(Directory d, System.String segment, FieldInfos fn)
40 fieldInfos = fn;
41 fieldsStream = d.CreateOutput(segment + ".fdt");
42 indexStream = d.CreateOutput(segment + ".fdx");
45 internal void Close()
47 fieldsStream.Close();
48 indexStream.Close();
51 internal void AddDocument(Document doc)
53 indexStream.WriteLong(fieldsStream.GetFilePointer());
55 int storedCount = 0;
56 foreach(Field field in doc.Fields())
58 if (field.IsStored())
59 storedCount++;
61 fieldsStream.WriteVInt(storedCount);
63 foreach(Field field in doc.Fields())
65 if (field.IsStored())
67 fieldsStream.WriteVInt(fieldInfos.FieldNumber(field.Name()));
69 byte bits = 0;
70 if (field.IsTokenized())
71 bits |= FieldsWriter.FIELD_IS_TOKENIZED;
72 if (field.IsBinary())
73 bits |= FieldsWriter.FIELD_IS_BINARY;
74 if (field.IsCompressed())
75 bits |= FieldsWriter.FIELD_IS_COMPRESSED;
77 fieldsStream.WriteByte(bits);
79 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())
86 data = Compress(field.BinaryValue());
88 else
90 data = Compress(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(field.StringValue()));
92 int len = data.Length;
93 fieldsStream.WriteVInt(len);
94 fieldsStream.WriteBytes(data, len);
96 else
98 // compression is disabled for the current field
99 if (field.IsBinary())
101 byte[] data = field.BinaryValue();
102 int len = data.Length;
103 fieldsStream.WriteVInt(len);
104 fieldsStream.WriteBytes(data, len);
106 else
108 fieldsStream.WriteString(field.StringValue());
115 private byte[] Compress(byte[] input)
117 return SupportClass.CompressionSupport.Compress(input);