2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
)
41 fieldsStream
= d
.CreateOutput(segment
+ ".fdt");
42 indexStream
= d
.CreateOutput(segment
+ ".fdx");
51 internal void AddDocument(Document doc
)
53 indexStream
.WriteLong(fieldsStream
.GetFilePointer());
56 foreach(Field field
in doc
.Fields())
61 fieldsStream
.WriteVInt(storedCount
);
63 foreach(Field field
in doc
.Fields())
67 fieldsStream
.WriteVInt(fieldInfos
.FieldNumber(field
.Name()));
70 if (field
.IsTokenized())
71 bits
|= FieldsWriter
.FIELD_IS_TOKENIZED
;
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
83 // check if it is a binary field
86 data
= Compress(field
.BinaryValue());
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
);
98 // compression is disabled for the current field
101 byte[] data
= field
.BinaryValue();
102 int len
= data
.Length
;
103 fieldsStream
.WriteVInt(len
);
104 fieldsStream
.WriteBytes(data
, len
);
108 fieldsStream
.WriteString(field
.StringValue());
115 private byte[] Compress(byte[] input
)
117 return SupportClass
.CompressionSupport
.Compress(input
);