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 IndexInput
= Lucene
.Net
.Store
.IndexInput
;
23 namespace Lucene
.Net
.Index
26 /// <summary> Class responsible for access to stored document fields.
28 /// It uses <segment>.fdt and <segment>.fdx; files.
31 /// <version> $Id: FieldsReader.cs,v 1.4 2006/10/02 17:08:52 joeshaw Exp $
33 public sealed class FieldsReader
35 private FieldInfos fieldInfos
;
36 private IndexInput fieldsStream
;
37 private IndexInput indexStream
;
40 public /*internal*/ FieldsReader(Directory d
, System
.String segment
, 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()
56 public /*internal*/ int 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
);
84 doc
.Add(new Field(fi
.name
, Uncompress(b
), Field
.Store
.COMPRESS
));
86 doc
.Add(new Field(fi
.name
, b
, Field
.Store
.YES
));
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
;
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
;
111 termVector
= Field
.TermVector
.WITH_OFFSETS
;
114 else if (fi
.storePositionWithTermVector
)
116 termVector
= Field
.TermVector
.WITH_POSITIONS
;
120 termVector
= Field
.TermVector
.YES
;
125 termVector
= Field
.TermVector
.NO
;
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
);
139 Field f
= new Field(fi
.name
, fieldsStream
.ReadString(), store
, index
, termVector
);
140 f
.SetOmitNorms(fi
.omitNorms
);
149 private byte[] Uncompress(byte[] input
)
151 return SupportClass
.CompressionSupport
.Uncompress(input
);