FieldsReader.cs :  » Search-Engines » dotLucene » Lucene » Net » Index » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Search Engines » dotLucene 
dotLucene » Lucene » Net » Index » FieldsReader.cs
/*
 * Copyright 2004 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using DocumentLucene.Net.Documents.Document;
using FieldLucene.Net.Documents.Field;
using DirectoryLucene.Net.Store.Directory;
using IndexInputLucene.Net.Store.IndexInput;

namespace Lucene.Net.Index{
  
  /// <summary> Class responsible for access to stored document fields.
  /// 
  /// It uses &lt;segment&gt;.fdt and &lt;segment&gt;.fdx; files.
  /// 
  /// </summary>
  /// <version>  $Id: FieldsReader.java 329524 2005-10-30 05:38:46Z yonik $
  /// </version>
  public sealed class FieldsReader
  {
    private FieldInfos fieldInfos;
    private IndexInput fieldsStream;
    private IndexInput indexStream;
    private int size;
    
    public /*internal*/ FieldsReader(Directory d, System.String segment, FieldInfos fn)
    {
      fieldInfos = fn;
      
      fieldsStream = d.OpenInput(segment + ".fdt");
      indexStream = d.OpenInput(segment + ".fdx");
      
      size = (int) (indexStream.Length() / 8);
    }
    
    public /*internal*/ void  Close()
    {
      fieldsStream.Close();
      indexStream.Close();
    }
    
    public /*internal*/ int Size()
    {
      return size;
    }
    
    public /*internal*/ Document Doc(int n)
    {
      indexStream.Seek(n * 8L);
      long position = indexStream.ReadLong();
      fieldsStream.Seek(position);
      
      Document doc = new Document();
      int numFields = fieldsStream.ReadVInt();
      for (int i = 0; i < numFields; i++)
      {
        int fieldNumber = fieldsStream.ReadVInt();
        FieldInfo fi = fieldInfos.FieldInfo(fieldNumber);
        
        byte bits = fieldsStream.ReadByte();
        
        bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;
        bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;
        
        if ((bits & FieldsWriter.FIELD_IS_BINARY) != 0)
        {
          byte[] b = new byte[fieldsStream.ReadVInt()];
          fieldsStream.ReadBytes(b, 0, b.Length);
          if (compressed)
            doc.Add(new Field(fi.name, Uncompress(b), Field.Store.COMPRESS));
          else
            doc.Add(new Field(fi.name, b, Field.Store.YES));
        }
        else
        {
          Field.Index index;
          Field.Store store = Field.Store.YES;
          
          if (fi.isIndexed && tokenize)
            index = Field.Index.TOKENIZED;
          else if (fi.isIndexed && !tokenize)
            index = Field.Index.UN_TOKENIZED;
          else
            index = Field.Index.NO;
          
          Field.TermVector termVector = null;
          if (fi.storeTermVector)
          {
            if (fi.storeOffsetWithTermVector)
            {
              if (fi.storePositionWithTermVector)
              {
                termVector = Field.TermVector.WITH_POSITIONS_OFFSETS;
              }
              else
              {
                termVector = Field.TermVector.WITH_OFFSETS;
              }
            }
            else if (fi.storePositionWithTermVector)
            {
              termVector = Field.TermVector.WITH_POSITIONS;
            }
            else
            {
              termVector = Field.TermVector.YES;
            }
          }
          else
          {
            termVector = Field.TermVector.NO;
          }
          
          if (compressed)
          {
            store = Field.Store.COMPRESS;
            byte[] b = new byte[fieldsStream.ReadVInt()];
            fieldsStream.ReadBytes(b, 0, b.Length);
            Field f = new Field(fi.name, System.Text.Encoding.GetEncoding("UTF-8").GetString(Uncompress(b)), store, index, termVector);
            f.SetOmitNorms(fi.omitNorms);
            doc.Add(f);
          }
          else
          {
            Field f = new Field(fi.name, fieldsStream.ReadString(), store, index, termVector);
            f.SetOmitNorms(fi.omitNorms);
            doc.Add(f);
          }
        }
      }
      
      return doc;
    }
    
    private byte[] Uncompress(byte[] input)
    {
            // {{Aroush-1.9}} for .NET 1.1, we can use reflection and ZLib?
            return input;

            /*
      //UPGRADE_ISSUE: Class 'java.util.zip.Inflater' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
      //UPGRADE_ISSUE: Constructor 'java.util.zip.Inflater.Inflater' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
      Inflater decompressor = new Inflater();
      //UPGRADE_ISSUE: Method 'java.util.zip.Inflater.setInput' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
      decompressor.setInput(input);
      
      // Create an expandable byte array to hold the decompressed data
      System.IO.MemoryStream bos = new System.IO.MemoryStream(input.Length);
      
      // Decompress the data
      byte[] buf = new byte[1024];
      //UPGRADE_ISSUE: Method 'java.util.zip.Inflater.finished' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
      while (!decompressor.finished())
      {
        try
        {
          //UPGRADE_ISSUE: Method 'java.util.zip.Inflater.inflate' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
          int count = decompressor.inflate(buf);
          bos.Write(SupportClass.ToByteArray(buf), 0, count);
        }
        catch (Exception e)
        {
          // this will happen if the field is not compressed
          throw new System.IO.IOException("field data are in wrong format: " + e.ToString());
        }
      }
      
      //UPGRADE_ISSUE: Method 'java.util.zip.Inflater.end' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipInflater'"
      decompressor.end();
      
      // Get the decompressed data
      return SupportClass.ToSByteArray(bos.ToArray());
            */
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.