cvsimport
[beagle.git] / beagled / Lucene.Net / Index / FieldsReader.cs
blobb30547d09bd636075873f74688399bf196aa8eea
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 IndexInput = Lucene.Net.Store.IndexInput;
23 namespace Lucene.Net.Index
26 /// <summary> Class responsible for access to stored document fields.
27 ///
28 /// It uses &lt;segment&gt;.fdt and &lt;segment&gt;.fdx; files.
29 ///
30 /// </summary>
31 /// <version> $Id: FieldsReader.cs,v 1.4 2006/10/02 17:08:52 joeshaw Exp $
32 /// </version>
33 public sealed class FieldsReader
35 private FieldInfos fieldInfos;
36 private IndexInput fieldsStream;
37 private IndexInput indexStream;
38 private int size;
40 public /*internal*/ FieldsReader(Directory d, System.String segment, FieldInfos fn)
42 fieldInfos = fn;
44 fieldsStream = d.OpenInput(segment + ".fdt");
45 indexStream = d.OpenInput(segment + ".fdx");
47 size = (int) (indexStream.Length() / 8);
50 public /*internal*/ void Close()
52 fieldsStream.Close();
53 indexStream.Close();
56 public /*internal*/ int Size()
58 return size;
61 public /*internal*/ Document Doc(int n)
63 indexStream.Seek(n * 8L);
64 long position = indexStream.ReadLong();
65 fieldsStream.Seek(position);
67 Document doc = new Document();
68 int numFields = fieldsStream.ReadVInt();
69 for (int i = 0; i < numFields; i++)
71 int fieldNumber = fieldsStream.ReadVInt();
72 FieldInfo fi = fieldInfos.FieldInfo(fieldNumber);
74 byte bits = fieldsStream.ReadByte();
76 bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;
77 bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;
79 if ((bits & FieldsWriter.FIELD_IS_BINARY) != 0)
81 byte[] b = new byte[fieldsStream.ReadVInt()];
82 fieldsStream.ReadBytes(b, 0, b.Length);
83 if (compressed)
84 doc.Add(new Field(fi.name, Uncompress(b), Field.Store.COMPRESS));
85 else
86 doc.Add(new Field(fi.name, b, Field.Store.YES));
88 else
90 Field.Index index;
91 Field.Store store = Field.Store.YES;
93 if (fi.isIndexed && tokenize)
94 index = Field.Index.TOKENIZED;
95 else if (fi.isIndexed && !tokenize)
96 index = Field.Index.UN_TOKENIZED;
97 else
98 index = Field.Index.NO;
100 Field.TermVector termVector = null;
101 if (fi.storeTermVector)
103 if (fi.storeOffsetWithTermVector)
105 if (fi.storePositionWithTermVector)
107 termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
109 else
111 termVector = Field.TermVector.WITH_OFFSETS;
114 else if (fi.storePositionWithTermVector)
116 termVector = Field.TermVector.WITH_POSITIONS;
118 else
120 termVector = Field.TermVector.YES;
123 else
125 termVector = Field.TermVector.NO;
128 if (compressed)
130 store = Field.Store.COMPRESS;
131 byte[] b = new byte[fieldsStream.ReadVInt()];
132 fieldsStream.ReadBytes(b, 0, b.Length);
133 Field f = new Field(fi.name, System.Text.Encoding.GetEncoding("UTF-8").GetString(Uncompress(b)), store, index, termVector);
134 f.SetOmitNorms(fi.omitNorms);
135 doc.Add(f);
137 else
139 Field f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
140 f.SetOmitNorms(fi.omitNorms);
141 doc.Add(f);
146 return doc;
149 private byte[] Uncompress(byte[] input)
151 return SupportClass.CompressionSupport.Uncompress(input);