Home » lucene-3.0.1-src » org.apache » lucene » store » db » [javadoc | source]

    1   package org.apache.lucene.store.db;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.IOException;
   21   import org.apache.lucene.store.IndexInput;
   22   
   23   
   24   
   25   
   26   public class DbIndexInput extends IndexInput {
   27   
   28       protected long position = 0L, length = 0L;
   29       protected DbDirectory directory;
   30       protected Block block;
   31       protected File file;
   32   
   33       protected DbIndexInput(DbDirectory directory, String name)
   34           throws IOException
   35       {
   36           super();
   37   
   38           this.directory = directory;
   39   
   40           this.file = new File(name);
   41           if (!file.exists(directory))
   42               throw new IOException("File does not exist: " + name);
   43   
   44           length = file.getLength();
   45   
   46           block = new Block(file);
   47           block.get(directory);
   48       }
   49   
   50       @Override
   51       public Object clone()
   52       {
   53           try {
   54               DbIndexInput clone = (DbIndexInput) super.clone();
   55   
   56               clone.block = new Block(file);
   57               clone.block.seek(position);
   58               clone.block.get(directory);
   59   
   60               return clone;
   61           } catch (IOException e) {
   62               throw new RuntimeException(e.getMessage());
   63           }
   64       }
   65   
   66       @Override
   67       public void close()
   68           throws IOException
   69       {
   70       }
   71   
   72       @Override
   73       public long length()
   74       {
   75           return length;
   76       }
   77   
   78       @Override
   79       public byte readByte()
   80           throws IOException
   81       {
   82           if (position + 1 > length)
   83               throw new IOException("Reading past end of file");
   84   
   85           int blockPos = (int) (position++ & DbIndexOutput.BLOCK_MASK);
   86           byte b = block.getData()[blockPos];
   87   
   88           if (blockPos + 1 == DbIndexOutput.BLOCK_LEN)
   89           {
   90               block.seek(position);
   91               block.get(directory);
   92           }
   93   
   94           return b;
   95       }
   96   
   97       @Override
   98       public void readBytes(byte[] b, int offset, int len)
   99           throws IOException
  100       {
  101           if (position + len > length)
  102               throw new IOException("Reading past end of file");
  103           else
  104           {
  105               int blockPos = (int) (position & DbIndexOutput.BLOCK_MASK);
  106   
  107               while (blockPos + len >= DbIndexOutput.BLOCK_LEN) {
  108                   int blockLen = DbIndexOutput.BLOCK_LEN - blockPos;
  109   
  110                   System.arraycopy(block.getData(), blockPos,
  111                                    b, offset, blockLen);
  112   
  113                   len -= blockLen;
  114                   offset += blockLen;
  115                   position += blockLen;
  116   
  117                   block.seek(position);
  118                   block.get(directory);
  119                   blockPos = 0;
  120               }
  121   
  122               if (len > 0)
  123               {
  124                   System.arraycopy(block.getData(), blockPos, b, offset, len);
  125                   position += len;
  126               }
  127           }
  128       }
  129   
  130       @Override
  131       public void seek(long pos)
  132           throws IOException
  133       {
  134           if (pos > length)
  135               throw new IOException("seeking past end of file");
  136   
  137           if ((pos >>> DbIndexOutput.BLOCK_SHIFT) !=
  138               (position >>> DbIndexOutput.BLOCK_SHIFT))
  139           {
  140               block.seek(pos);
  141               block.get(directory);
  142           }
  143   
  144           position = pos;
  145       }
  146   
  147       @Override
  148       public long getFilePointer()
  149       {
  150           return position;
  151       }
  152   }

Home » lucene-3.0.1-src » org.apache » lucene » store » db » [javadoc | source]