~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to beagled/Lucene.Net/Index/FieldsReader.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
7
8
 * 
8
9
 * http://www.apache.org/licenses/LICENSE-2.0
9
10
 * 
15
16
 */
16
17
 
17
18
using System;
18
 
using System.Collections;
19
 
using Document = Lucene.Net.Documents.Document;
20
 
using Field = Lucene.Net.Documents.Field;
 
19
using Lucene.Net.Documents;
21
20
using Directory = Lucene.Net.Store.Directory;
22
21
using IndexInput = Lucene.Net.Store.IndexInput;
23
22
 
25
24
{
26
25
        
27
26
        /// <summary> Class responsible for access to stored document fields.
28
 
        /// 
 
27
        /// <p/>
29
28
        /// It uses &lt;segment&gt;.fdt and &lt;segment&gt;.fdx; files.
30
29
        /// 
31
30
        /// </summary>
32
 
        /// <version>  $Id: FieldsReader.cs,v 1.4 2006/10/02 17:08:52 joeshaw Exp $
 
31
        /// <version>  $Id: FieldsReader.java 507009 2007-02-13 14:06:52Z gsingers $
33
32
        /// </version>
34
33
        public sealed class FieldsReader
35
34
        {
36
35
                private FieldInfos fieldInfos;
 
36
                
 
37
                // The main fieldStream, used only for cloning.
 
38
                private IndexInput cloneableFieldsStream;
 
39
                
 
40
                // This is a clone of cloneableFieldsStream used for reading documents.
 
41
                // It should not be cloned outside of a synchronized context.
37
42
                private IndexInput fieldsStream;
 
43
                
38
44
                private IndexInput indexStream;
39
45
                private int size;
40
46
                
41
 
                public /*internal*/ FieldsReader(Directory d, System.String segment, FieldInfos fn)
 
47
                private System.LocalDataStoreSlot fieldsStreamTL = System.Threading.Thread.AllocateDataSlot();
 
48
                
 
49
                public FieldsReader(Directory d, System.String segment, FieldInfos fn)
42
50
                {
43
51
                        fieldInfos = fn;
44
52
                        
45
 
                        fieldsStream = d.OpenInput(segment + ".fdt");
 
53
                        cloneableFieldsStream = d.OpenInput(segment + ".fdt");
 
54
                        fieldsStream = (IndexInput) cloneableFieldsStream.Clone();
46
55
                        indexStream = d.OpenInput(segment + ".fdx");
47
 
                        
48
56
                        size = (int) (indexStream.Length() / 8);
49
57
                }
50
58
                
51
 
                public /*internal*/ void  Close()
 
59
                /// <summary> Closes the underlying {@link Lucene.Net.Store.IndexInput} streams, including any ones associated with a
 
60
                /// lazy implementation of a Field.  This means that the Fields values will not be accessible.
 
61
                /// 
 
62
                /// </summary>
 
63
                /// <throws>  IOException </throws>
 
64
                public void  Close()
52
65
                {
53
66
                        fieldsStream.Close();
 
67
                        cloneableFieldsStream.Close();
54
68
                        indexStream.Close();
 
69
                        IndexInput localFieldsStream = (IndexInput) System.Threading.Thread.GetData(fieldsStreamTL);
 
70
                        if (localFieldsStream != null)
 
71
                        {
 
72
                                localFieldsStream.Close();
 
73
                                System.Threading.Thread.SetData(fieldsStreamTL, null);
 
74
                        }
55
75
                }
56
76
                
57
 
                public /*internal*/ int Size()
 
77
                public int Size()
58
78
                {
59
79
                        return size;
60
80
                }
61
81
                
62
 
                public /*internal*/ Document Doc(int n)
 
82
                public Document Doc(int n, FieldSelector fieldSelector)
63
83
                {
64
84
                        indexStream.Seek(n * 8L);
65
85
                        long position = indexStream.ReadLong();
71
91
                        {
72
92
                                int fieldNumber = fieldsStream.ReadVInt();
73
93
                                FieldInfo fi = fieldInfos.FieldInfo(fieldNumber);
 
94
                                FieldSelectorResult acceptField = fieldSelector == null ? FieldSelectorResult.LOAD : fieldSelector.Accept(fi.name);
74
95
                                
75
96
                                byte bits = fieldsStream.ReadByte();
76
 
                                
77
97
                                bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;
78
98
                                bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;
79
 
                                
80
 
                                if ((bits & FieldsWriter.FIELD_IS_BINARY) != 0)
81
 
                                {
82
 
                                        byte[] b = new byte[fieldsStream.ReadVInt()];
 
99
                                bool binary = (bits & FieldsWriter.FIELD_IS_BINARY) != 0;
 
100
                                //TODO: Find an alternative approach here if this list continues to grow beyond the
 
101
                                //list of 5 or 6 currently here.  See Lucene 762 for discussion
 
102
                                if (acceptField.Equals(FieldSelectorResult.LOAD))
 
103
                                {
 
104
                                        AddField(doc, fi, binary, compressed, tokenize);
 
105
                                }
 
106
                                else if (acceptField.Equals(FieldSelectorResult.LOAD_FOR_MERGE))
 
107
                                {
 
108
                                        AddFieldForMerge(doc, fi, binary, compressed, tokenize);
 
109
                                }
 
110
                                else if (acceptField.Equals(FieldSelectorResult.LOAD_AND_BREAK))
 
111
                                {
 
112
                                        AddField(doc, fi, binary, compressed, tokenize);
 
113
                                        break; //Get out of this loop
 
114
                                }
 
115
                                else if (acceptField.Equals(FieldSelectorResult.LAZY_LOAD))
 
116
                                {
 
117
                                        AddFieldLazy(doc, fi, binary, compressed, tokenize);
 
118
                                }
 
119
                                else if (acceptField.Equals(FieldSelectorResult.SIZE))
 
120
                                {
 
121
                                        SkipField(binary, compressed, AddFieldSize(doc, fi, binary, compressed));
 
122
                                }
 
123
                                else if (acceptField.Equals(FieldSelectorResult.SIZE_AND_BREAK))
 
124
                                {
 
125
                                        AddFieldSize(doc, fi, binary, compressed);
 
126
                                        break;
 
127
                                }
 
128
                                else
 
129
                                {
 
130
                                        SkipField(binary, compressed);
 
131
                                }
 
132
                        }
 
133
                        
 
134
                        return doc;
 
135
                }
 
136
                
 
137
                /// <summary> Skip the field.  We still have to read some of the information about the field, but can skip past the actual content.
 
138
                /// This will have the most payoff on large fields.
 
139
                /// </summary>
 
140
                private void  SkipField(bool binary, bool compressed)
 
141
                {
 
142
                        SkipField(binary, compressed, fieldsStream.ReadVInt());
 
143
                }
 
144
                
 
145
                private void  SkipField(bool binary, bool compressed, int toRead)
 
146
                {
 
147
                        if (binary || compressed)
 
148
                        {
 
149
                                long pointer = fieldsStream.GetFilePointer();
 
150
                                fieldsStream.Seek(pointer + toRead);
 
151
                        }
 
152
                        else
 
153
                        {
 
154
                                //We need to skip chars.  This will slow us down, but still better
 
155
                                fieldsStream.SkipChars(toRead);
 
156
                        }
 
157
                }
 
158
                
 
159
                private void  AddFieldLazy(Document doc, FieldInfo fi, bool binary, bool compressed, bool tokenize)
 
160
                {
 
161
                        if (binary == true)
 
162
                        {
 
163
                                int toRead = fieldsStream.ReadVInt();
 
164
                                long pointer = fieldsStream.GetFilePointer();
 
165
                                if (compressed)
 
166
                                {
 
167
                                        //was: doc.add(new Fieldable(fi.name, uncompress(b), Fieldable.Store.COMPRESS));
 
168
                                        doc.Add(new LazyField(this, fi.name, Field.Store.COMPRESS, toRead, pointer));
 
169
                                }
 
170
                                else
 
171
                                {
 
172
                                        //was: doc.add(new Fieldable(fi.name, b, Fieldable.Store.YES));
 
173
                                        doc.Add(new LazyField(this, fi.name, Field.Store.YES, toRead, pointer));
 
174
                                }
 
175
                                //Need to move the pointer ahead by toRead positions
 
176
                                fieldsStream.Seek(pointer + toRead);
 
177
                        }
 
178
                        else
 
179
                        {
 
180
                                Field.Store store = Field.Store.YES;
 
181
                                Field.Index index = GetIndexType(fi, tokenize);
 
182
                                Field.TermVector termVector = GetTermVectorType(fi);
 
183
                                
 
184
                                Fieldable f;
 
185
                                if (compressed)
 
186
                                {
 
187
                                        store = Field.Store.COMPRESS;
 
188
                                        int toRead = fieldsStream.ReadVInt();
 
189
                                        long pointer = fieldsStream.GetFilePointer();
 
190
                                        f = new LazyField(this, fi.name, store, toRead, pointer);
 
191
                                        //skip over the part that we aren't loading
 
192
                                        fieldsStream.Seek(pointer + toRead);
 
193
                                        f.SetOmitNorms(fi.omitNorms);
 
194
                                }
 
195
                                else
 
196
                                {
 
197
                                        int length = fieldsStream.ReadVInt();
 
198
                                        long pointer = fieldsStream.GetFilePointer();
 
199
                                        //Skip ahead of where we are by the length of what is stored
 
200
                                        fieldsStream.SkipChars(length);
 
201
                                        f = new LazyField(this, fi.name, store, index, termVector, length, pointer);
 
202
                                        f.SetOmitNorms(fi.omitNorms);
 
203
                                }
 
204
                                doc.Add(f);
 
205
                        }
 
206
                }
 
207
                
 
208
                // in merge mode we don't uncompress the data of a compressed field
 
209
                private void  AddFieldForMerge(Document doc, FieldInfo fi, bool binary, bool compressed, bool tokenize)
 
210
                {
 
211
                        System.Object data;
 
212
                        
 
213
                        if (binary || compressed)
 
214
                        {
 
215
                                int toRead = fieldsStream.ReadVInt();
 
216
                                byte[] b = new byte[toRead];
 
217
                                fieldsStream.ReadBytes(b, 0, b.Length);
 
218
                                data = b;
 
219
                        }
 
220
                        else
 
221
                        {
 
222
                                data = fieldsStream.ReadString();
 
223
                        }
 
224
                        
 
225
                        doc.Add(new FieldForMerge(data, fi, binary, compressed, tokenize));
 
226
                }
 
227
                
 
228
                private void  AddField(Document doc, FieldInfo fi, bool binary, bool compressed, bool tokenize)
 
229
                {
 
230
                        
 
231
                        //we have a binary stored field, and it may be compressed
 
232
                        if (binary)
 
233
                        {
 
234
                                int toRead = fieldsStream.ReadVInt();
 
235
                                byte[] b = new byte[toRead];
 
236
                                fieldsStream.ReadBytes(b, 0, b.Length);
 
237
                                if (compressed)
 
238
                                        doc.Add(new Field(fi.name, Uncompress(b), Field.Store.COMPRESS));
 
239
                                else
 
240
                                        doc.Add(new Field(fi.name, b, Field.Store.YES));
 
241
                        }
 
242
                        else
 
243
                        {
 
244
                                Field.Store store = Field.Store.YES;
 
245
                                Field.Index index = GetIndexType(fi, tokenize);
 
246
                                Field.TermVector termVector = GetTermVectorType(fi);
 
247
                                
 
248
                                Fieldable f;
 
249
                                if (compressed)
 
250
                                {
 
251
                                        store = Field.Store.COMPRESS;
 
252
                                        int toRead = fieldsStream.ReadVInt();
 
253
                                        
 
254
                                        byte[] b = new byte[toRead];
83
255
                                        fieldsStream.ReadBytes(b, 0, b.Length);
84
 
                                        if (compressed)
85
 
                                                doc.Add(new Field(fi.name, Uncompress(b), Field.Store.COMPRESS));
86
 
                                        else
87
 
                                                doc.Add(new Field(fi.name, b, Field.Store.YES));
88
 
                                }
89
 
                                else
90
 
                                {
91
 
                                        Field.Index index;
92
 
                                        Field.Store store = Field.Store.YES;
93
 
                                        
94
 
                                        if (fi.isIndexed && tokenize)
95
 
                                                index = Field.Index.TOKENIZED;
96
 
                                        else if (fi.isIndexed && !tokenize)
97
 
                                                index = Field.Index.UN_TOKENIZED;
98
 
                                        else
99
 
                                                index = Field.Index.NO;
100
 
                                        
101
 
                                        Field.TermVector termVector = null;
102
 
                                        if (fi.storeTermVector)
103
 
                                        {
104
 
                                                if (fi.storeOffsetWithTermVector)
105
 
                                                {
106
 
                                                        if (fi.storePositionWithTermVector)
107
 
                                                        {
108
 
                                                                termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
109
 
                                                        }
110
 
                                                        else
111
 
                                                        {
112
 
                                                                termVector = Field.TermVector.WITH_OFFSETS;
113
 
                                                        }
114
 
                                                }
115
 
                                                else if (fi.storePositionWithTermVector)
116
 
                                                {
117
 
                                                        termVector = Field.TermVector.WITH_POSITIONS;
118
 
                                                }
119
 
                                                else
120
 
                                                {
121
 
                                                        termVector = Field.TermVector.YES;
122
 
                                                }
123
 
                                        }
124
 
                                        else
125
 
                                        {
126
 
                                                termVector = Field.TermVector.NO;
127
 
                                        }
128
 
                                        
129
 
                                        if (compressed)
130
 
                                        {
131
 
                                                store = Field.Store.COMPRESS;
132
 
                                                byte[] b = new byte[fieldsStream.ReadVInt()];
133
 
                                                fieldsStream.ReadBytes(b, 0, b.Length);
134
 
                                                Field f = new Field(fi.name, System.Text.Encoding.GetEncoding("UTF-8").GetString(Uncompress(b)), store, index, termVector);
135
 
                                                f.SetOmitNorms(fi.omitNorms);
136
 
                                                doc.Add(f);
137
 
                                        }
138
 
                                        else
139
 
                                        {
140
 
                                                Field f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
141
 
                                                f.SetOmitNorms(fi.omitNorms);
142
 
                                                doc.Add(f);
143
 
                                        }
144
 
                                }
145
 
                        }
146
 
                        
147
 
                        return doc;
148
 
                }
149
 
                
150
 
                public /*internal*/ Document Doc(int n, string[] fields)
151
 
                {
152
 
                        if (fields == null || fields.Length == 0)
153
 
                                return Doc (n);
154
 
 
155
 
                        // FIXME: use Hashset
156
 
                        ArrayList field_list = new ArrayList (fields);
157
 
                        int num_required_fields = field_list.Count;
158
 
 
159
 
                        indexStream.Seek(n * 8L);
160
 
                        long position = indexStream.ReadLong();
161
 
                        fieldsStream.Seek(position);
162
 
                        
163
 
                        Document doc = new Document();
164
 
                        int numFields = fieldsStream.ReadVInt();
165
 
                        for (int i = 0; i < numFields && num_required_fields > 0; i++)
166
 
                        {
167
 
                                int fieldNumber = fieldsStream.ReadVInt();
168
 
                                FieldInfo fi = fieldInfos.FieldInfo(fieldNumber);
169
 
                                if (field_list.Contains (fi.name)) {
170
 
                                        num_required_fields --; 
171
 
 
172
 
                                        byte bits = fieldsStream.ReadByte();
173
 
                                        
174
 
                                        bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;
175
 
                                        bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;
176
 
                                        
177
 
                                        if ((bits & FieldsWriter.FIELD_IS_BINARY) != 0)
178
 
                                        {
179
 
                                                byte[] b = new byte[fieldsStream.ReadVInt()];
180
 
                                                fieldsStream.ReadBytes(b, 0, b.Length);
181
 
                                                if (compressed)
182
 
                                                        doc.Add(new Field(fi.name, Uncompress(b), Field.Store.COMPRESS));
183
 
                                                else
184
 
                                                        doc.Add(new Field(fi.name, b, Field.Store.YES));
185
 
                                        }
186
 
                                        else
187
 
                                        {
188
 
                                                Field.Index index;
189
 
                                                Field.Store store = Field.Store.YES;
190
 
                                                
191
 
                                                if (fi.isIndexed && tokenize)
192
 
                                                        index = Field.Index.TOKENIZED;
193
 
                                                else if (fi.isIndexed && !tokenize)
194
 
                                                        index = Field.Index.UN_TOKENIZED;
195
 
                                                else
196
 
                                                        index = Field.Index.NO;
197
 
                                                
198
 
                                                Field.TermVector termVector = null;
199
 
                                                if (fi.storeTermVector)
200
 
                                                {
201
 
                                                        if (fi.storeOffsetWithTermVector)
202
 
                                                        {
203
 
                                                                if (fi.storePositionWithTermVector)
204
 
                                                                {
205
 
                                                                        termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
206
 
                                                                }
207
 
                                                                else
208
 
                                                                {
209
 
                                                                        termVector = Field.TermVector.WITH_OFFSETS;
210
 
                                                                }
211
 
                                                        }
212
 
                                                        else if (fi.storePositionWithTermVector)
213
 
                                                        {
214
 
                                                                termVector = Field.TermVector.WITH_POSITIONS;
215
 
                                                        }
216
 
                                                        else
217
 
                                                        {
218
 
                                                                termVector = Field.TermVector.YES;
219
 
                                                        }
220
 
                                                }
221
 
                                                else
222
 
                                                {
223
 
                                                        termVector = Field.TermVector.NO;
224
 
                                                }
225
 
                                                
226
 
                                                if (compressed)
227
 
                                                {
228
 
                                                        store = Field.Store.COMPRESS;
229
 
                                                        byte[] b = new byte[fieldsStream.ReadVInt()];
230
 
                                                        fieldsStream.ReadBytes(b, 0, b.Length);
231
 
                                                        Field f = new Field(fi.name, System.Text.Encoding.GetEncoding("UTF-8").GetString(Uncompress(b)), store, index, termVector);
232
 
                                                        f.SetOmitNorms(fi.omitNorms);
233
 
                                                        doc.Add(f);
234
 
                                                }
235
 
                                                else
236
 
                                                {
237
 
                                                        Field f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
238
 
                                                        f.SetOmitNorms(fi.omitNorms);
239
 
                                                        doc.Add(f);
240
 
                                                }
241
 
                                        }
242
 
                                } else {
243
 
                                        byte bits = fieldsStream.ReadByte();
244
 
                                        
245
 
                                        bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;
246
 
                                        bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;
247
 
                                        
248
 
                                        if ((bits & FieldsWriter.FIELD_IS_BINARY) != 0)
249
 
                                        {
250
 
                                                //byte[] b = new byte[fieldsStream.ReadVInt()];
251
 
                                                //fieldsStream.ReadBytes(b, 0, b.Length);
252
 
                                                int length = fieldsStream.ReadVInt();
253
 
                                                for (int j = 0; j < length; j++)
254
 
                                                        fieldsStream.ReadByte ();
255
 
                                        }
256
 
                                        else
257
 
                                        {
258
 
                                                if (compressed)
259
 
                                                {
260
 
                                                        //byte[] b = new byte[fieldsStream.ReadVInt()];
261
 
                                                        //fieldsStream.ReadBytes(b, 0, b.Length);
262
 
                                                        int length = fieldsStream.ReadVInt();
263
 
                                                        for (int j = 0; j < length; j++)
264
 
                                                                fieldsStream.ReadByte ();
265
 
                                                }
266
 
                                                else
267
 
                                                {
268
 
                                                        //fieldsStream.ReadString ();
269
 
                                                        int length = fieldsStream.ReadVInt();
270
 
                                                        for (int j = 0; j < length; j++)
271
 
                                                        {
272
 
                                                                byte b = fieldsStream.ReadByte ();
273
 
                                                                if ((b & 0x80) == 0)
274
 
                                                                        continue;
275
 
                                                                else if ((b & 0xE0) != 0xE0) {
276
 
                                                                        fieldsStream.ReadByte ();
277
 
                                                                } else {
278
 
                                                                        fieldsStream.ReadByte ();
279
 
                                                                        fieldsStream.ReadByte ();
280
 
                                                                }
281
 
                                                        }
282
 
                                                }
283
 
                                        }
284
 
                                }
285
 
                        }
286
 
                        
287
 
                        return doc;
 
256
                                        f = new Field(fi.name, System.Text.Encoding.GetEncoding("UTF-8").GetString(Uncompress(b)), store, index, termVector);
 
257
                                        f.SetOmitNorms(fi.omitNorms);
 
258
                                }
 
259
                                else
 
260
                                {
 
261
                                        f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
 
262
                                        f.SetOmitNorms(fi.omitNorms);
 
263
                                }
 
264
                                doc.Add(f);
 
265
                        }
 
266
                }
 
267
                
 
268
                // Add the size of field as a byte[] containing the 4 bytes of the integer byte size (high order byte first; char = 2 bytes)
 
269
                // Read just the size -- caller must skip the field content to continue reading fields
 
270
                // Return the size in bytes or chars, depending on field type
 
271
                private int AddFieldSize(Document doc, FieldInfo fi, bool binary, bool compressed)
 
272
                {
 
273
                        int size = fieldsStream.ReadVInt(), bytesize = binary || compressed ? size : 2 * size;
 
274
                        byte[] sizebytes = new byte[4];
 
275
                        sizebytes[0] = (byte) (SupportClass.Number.URShift(bytesize, 24));
 
276
                        sizebytes[1] = (byte) (SupportClass.Number.URShift(bytesize, 16));
 
277
                        sizebytes[2] = (byte) (SupportClass.Number.URShift(bytesize, 8));
 
278
                        sizebytes[3] = (byte) bytesize;
 
279
                        doc.Add(new Field(fi.name, sizebytes, Field.Store.YES));
 
280
                        return size;
 
281
                }
 
282
                
 
283
                private Field.TermVector GetTermVectorType(FieldInfo fi)
 
284
                {
 
285
                        Field.TermVector termVector = null;
 
286
                        if (fi.storeTermVector)
 
287
                        {
 
288
                                if (fi.storeOffsetWithTermVector)
 
289
                                {
 
290
                                        if (fi.storePositionWithTermVector)
 
291
                                        {
 
292
                                                termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
 
293
                                        }
 
294
                                        else
 
295
                                        {
 
296
                                                termVector = Field.TermVector.WITH_OFFSETS;
 
297
                                        }
 
298
                                }
 
299
                                else if (fi.storePositionWithTermVector)
 
300
                                {
 
301
                                        termVector = Field.TermVector.WITH_POSITIONS;
 
302
                                }
 
303
                                else
 
304
                                {
 
305
                                        termVector = Field.TermVector.YES;
 
306
                                }
 
307
                        }
 
308
                        else
 
309
                        {
 
310
                                termVector = Field.TermVector.NO;
 
311
                        }
 
312
                        return termVector;
 
313
                }
 
314
                
 
315
                private Field.Index GetIndexType(FieldInfo fi, bool tokenize)
 
316
                {
 
317
                        Field.Index index;
 
318
                        if (fi.isIndexed && tokenize)
 
319
                                index = Field.Index.TOKENIZED;
 
320
                        else if (fi.isIndexed && !tokenize)
 
321
                                index = Field.Index.UN_TOKENIZED;
 
322
                        else
 
323
                                index = Field.Index.NO;
 
324
                        return index;
 
325
                }
 
326
                
 
327
                /// <summary> A Lazy implementation of Fieldable that differs loading of fields until asked for, instead of when the Document is
 
328
                /// loaded.
 
329
                /// </summary>
 
330
                [Serializable]
 
331
                private class LazyField:AbstractField, Fieldable
 
332
                {
 
333
                        private void  InitBlock(FieldsReader enclosingInstance)
 
334
                        {
 
335
                                this.enclosingInstance = enclosingInstance;
 
336
                        }
 
337
                        private FieldsReader enclosingInstance;
 
338
                        public FieldsReader Enclosing_Instance
 
339
                        {
 
340
                                get
 
341
                                {
 
342
                                        return enclosingInstance;
 
343
                                }
 
344
                                
 
345
                        }
 
346
                        private int toRead;
 
347
                        private long pointer;
 
348
                        
 
349
                        public LazyField(FieldsReader enclosingInstance, System.String name, Field.Store store, int toRead, long pointer):base(name, store, Field.Index.NO, Field.TermVector.NO)
 
350
                        {
 
351
                                InitBlock(enclosingInstance);
 
352
                                this.toRead = toRead;
 
353
                                this.pointer = pointer;
 
354
                                lazy = true;
 
355
                        }
 
356
                        
 
357
                        public LazyField(FieldsReader enclosingInstance, System.String name, Field.Store store, Field.Index index, Field.TermVector termVector, int toRead, long pointer):base(name, store, index, termVector)
 
358
                        {
 
359
                                InitBlock(enclosingInstance);
 
360
                                this.toRead = toRead;
 
361
                                this.pointer = pointer;
 
362
                                lazy = true;
 
363
                        }
 
364
                        
 
365
                        private IndexInput GetFieldStream()
 
366
                        {
 
367
                                IndexInput localFieldsStream = (IndexInput) System.Threading.Thread.GetData(Enclosing_Instance.fieldsStreamTL);
 
368
                                if (localFieldsStream == null)
 
369
                                {
 
370
                                        localFieldsStream = (IndexInput) Enclosing_Instance.cloneableFieldsStream.Clone();
 
371
                                        System.Threading.Thread.SetData(Enclosing_Instance.fieldsStreamTL, localFieldsStream);
 
372
                                }
 
373
                                return localFieldsStream;
 
374
                        }
 
375
                        
 
376
                        /// <summary> The value of the field in Binary, or null.  If null, the Reader or
 
377
                        /// String value is used.  Exactly one of stringValue(), readerValue() and
 
378
                        /// binaryValue() must be set.
 
379
                        /// </summary>
 
380
                        public override byte[] BinaryValue()
 
381
                        {
 
382
                                if (fieldsData == null)
 
383
                                {
 
384
                                        byte[] b = new byte[toRead];
 
385
                                        IndexInput localFieldsStream = GetFieldStream();
 
386
                                        //Throw this IO Exception since IndexREader.document does so anyway, so probably not that big of a change for people
 
387
                                        //since they are already handling this exception when getting the document
 
388
                                        try
 
389
                                        {
 
390
                                                localFieldsStream.Seek(pointer);
 
391
                                                localFieldsStream.ReadBytes(b, 0, b.Length);
 
392
                                                if (isCompressed == true)
 
393
                                                {
 
394
                                                        fieldsData = Enclosing_Instance.Uncompress(b);
 
395
                                                }
 
396
                                                else
 
397
                                                {
 
398
                                                        fieldsData = b;
 
399
                                                }
 
400
                                        }
 
401
                                        catch (System.IO.IOException e)
 
402
                                        {
 
403
                                                throw new FieldReaderException(e);
 
404
                                        }
 
405
                                }
 
406
                                return fieldsData is byte[] ? (byte[]) fieldsData : null;
 
407
                        }
 
408
                        
 
409
                        /// <summary> The value of the field as a Reader, or null.  If null, the String value
 
410
                        /// or binary value is  used.  Exactly one of stringValue(), readerValue(),
 
411
                        /// and binaryValue() must be set.
 
412
                        /// </summary>
 
413
                        public override System.IO.TextReader ReaderValue()
 
414
                        {
 
415
                                return fieldsData is System.IO.TextReader ? (System.IO.TextReader) fieldsData : null;
 
416
                        }
 
417
                        
 
418
                        /// <summary> The value of the field as a String, or null.  If null, the Reader value
 
419
                        /// or binary value is used.  Exactly one of stringValue(), readerValue(), and
 
420
                        /// binaryValue() must be set.
 
421
                        /// </summary>
 
422
                        public override System.String StringValue()
 
423
                        {
 
424
                                if (fieldsData == null)
 
425
                                {
 
426
                                        IndexInput localFieldsStream = GetFieldStream();
 
427
                                        try
 
428
                                        {
 
429
                                                localFieldsStream.Seek(pointer);
 
430
                                                if (isCompressed)
 
431
                                                {
 
432
                                                        byte[] b = new byte[toRead];
 
433
                                                        localFieldsStream.ReadBytes(b, 0, b.Length);
 
434
                                                        fieldsData = System.Text.Encoding.GetEncoding("UTF-8").GetString(Enclosing_Instance.Uncompress(b));
 
435
                                                }
 
436
                                                else
 
437
                                                {
 
438
                                                        //read in chars b/c we already know the length we need to read
 
439
                                                        char[] chars = new char[toRead];
 
440
                                                        localFieldsStream.ReadChars(chars, 0, toRead);
 
441
                                                        fieldsData = new System.String(chars);
 
442
                                                }
 
443
                                        }
 
444
                                        catch (System.IO.IOException e)
 
445
                                        {
 
446
                                                throw new FieldReaderException(e);
 
447
                                        }
 
448
                                }
 
449
                                return fieldsData is System.String ? (System.String) fieldsData : null;
 
450
                        }
 
451
                        
 
452
                        public long GetPointer()
 
453
                        {
 
454
                                return pointer;
 
455
                        }
 
456
                        
 
457
                        public void  SetPointer(long pointer)
 
458
                        {
 
459
                                this.pointer = pointer;
 
460
                        }
 
461
                        
 
462
                        public int GetToRead()
 
463
                        {
 
464
                                return toRead;
 
465
                        }
 
466
                        
 
467
                        public void  SetToRead(int toRead)
 
468
                        {
 
469
                                this.toRead = toRead;
 
470
                        }
288
471
                }
289
472
                
290
473
                private byte[] Uncompress(byte[] input)
291
474
                {
292
 
            return SupportClass.CompressionSupport.Uncompress(input);
 
475
                        return SupportClass.CompressionSupport.Uncompress(input);
 
476
        }
 
477
                
 
478
                // Instances of this class hold field properties and data
 
479
                // for merge
 
480
                [Serializable]
 
481
                public sealed class FieldForMerge : AbstractField
 
482
                {
 
483
                        public override System.String StringValue()
 
484
                        {
 
485
                                return (System.String) this.fieldsData;
 
486
                        }
 
487
                        
 
488
                        public override System.IO.TextReader ReaderValue()
 
489
                        {
 
490
                                // not needed for merge
 
491
                                return null;
 
492
                        }
 
493
                        
 
494
                        public override byte[] BinaryValue()
 
495
                        {
 
496
                                return (byte[]) this.fieldsData;
 
497
                        }
 
498
                        
 
499
                        public FieldForMerge(System.Object value_Renamed, FieldInfo fi, bool binary, bool compressed, bool tokenize)
 
500
                        {
 
501
                                this.isStored = true;
 
502
                                this.fieldsData = value_Renamed;
 
503
                                this.isCompressed = compressed;
 
504
                                this.isBinary = binary;
 
505
                                this.isTokenized = tokenize;
 
506
                                
 
507
                                this.name = String.Intern(fi.name);
 
508
                                this.isIndexed = fi.isIndexed;
 
509
                                this.omitNorms = fi.omitNorms;
 
510
                                this.storeOffsetWithTermVector = fi.storeOffsetWithTermVector;
 
511
                                this.storePositionWithTermVector = fi.storePositionWithTermVector;
 
512
                                this.storeTermVector = fi.storeTermVector;
 
513
                        }
293
514
                }
294
515
        }
295
 
}
 
516
}
 
 
b'\\ No newline at end of file'