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.IndexOutput; 22 23 24 25 26 public class DbIndexOutput extends IndexOutput { 27 28 /** 29 * The size of data blocks, currently 16k (2^14), is determined by this 30 * constant. 31 */ 32 static public final int BLOCK_SHIFT = 14; 33 static public final int BLOCK_LEN = 1 << BLOCK_SHIFT; 34 static public final int BLOCK_MASK = BLOCK_LEN - 1; 35 36 protected long position = 0L, length = 0L; 37 protected DbDirectory directory; 38 protected Block block; 39 protected File file; 40 41 protected DbIndexOutput(DbDirectory directory, String name, boolean create) 42 throws IOException 43 { 44 super(); 45 46 this.directory = directory; 47 48 file = new File(directory, name, create); 49 block = new Block(file); 50 length = file.getLength(); 51 52 seek(length); 53 block.get(directory); 54 55 directory.openFiles.add(this); 56 } 57 58 @Override 59 public void close() 60 throws IOException 61 { 62 flush(); 63 file.modify(directory, length, System.currentTimeMillis()); 64 65 directory.openFiles.remove(this); 66 } 67 68 @Override 69 public void flush() 70 throws IOException 71 { 72 if (length > 0) 73 block.put(directory); 74 } 75 76 @Override 77 public void writeByte(byte b) 78 throws IOException 79 { 80 int blockPos = (int) (position++ & BLOCK_MASK); 81 82 block.getData()[blockPos] = b; 83 84 if (blockPos + 1 == BLOCK_LEN) 85 { 86 block.put(directory); 87 block.seek(position); 88 block.get(directory); 89 } 90 91 if (position > length) 92 length = position; 93 } 94 95 @Override 96 public void writeBytes(byte[] b, int offset, int len) 97 throws IOException 98 { 99 int blockPos = (int) (position & BLOCK_MASK); 100 101 while (blockPos + len >= BLOCK_LEN) { 102 int blockLen = BLOCK_LEN - blockPos; 103 104 System.arraycopy(b, offset, block.getData(), blockPos, blockLen); 105 block.put(directory); 106 107 len -= blockLen; 108 offset += blockLen; 109 position += blockLen; 110 111 block.seek(position); 112 block.get(directory); 113 blockPos = 0; 114 } 115 116 if (len > 0) 117 { 118 System.arraycopy(b, offset, block.getData(), blockPos, len); 119 position += len; 120 } 121 122 if (position > length) 123 length = position; 124 } 125 126 @Override 127 public long length() 128 throws IOException 129 { 130 return length; 131 } 132 133 @Override 134 public void seek(long pos) 135 throws IOException 136 { 137 if (pos > length) 138 throw new IOException("seeking past end of file"); 139 140 if ((pos >>> BLOCK_SHIFT) == (position >>> BLOCK_SHIFT)) 141 position = pos; 142 else 143 { 144 block.put(directory); 145 block.seek(pos); 146 block.get(directory); 147 position = pos; 148 } 149 } 150 151 @Override 152 public long getFilePointer() 153 { 154 return position; 155 } 156 }