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 22 import com.sleepycat.db.DatabaseEntry; 23 import com.sleepycat.db.internal.Db; 24 import com.sleepycat.db.internal.DbTxn; 25 import com.sleepycat.db.DatabaseException; 26 27 28 29 public class Block extends Object { 30 protected DatabaseEntry key, data; 31 32 protected Block(File file) 33 throws IOException 34 { 35 byte[] fileKey = file.getKey(); 36 37 key = new DatabaseEntry(new byte[fileKey.length + 8]); 38 key.setUserBuffer(fileKey.length + 8, true); 39 40 data = new DatabaseEntry(new byte[DbIndexOutput.BLOCK_LEN]); 41 data.setUserBuffer(data.getSize(), true); 42 43 System.arraycopy(fileKey, 0, key.getData(), 0, fileKey.length); 44 seek(0L); 45 } 46 47 protected byte[] getKey() 48 { 49 return key.getData(); 50 } 51 52 protected byte[] getData() 53 { 54 return data.getData(); 55 } 56 57 protected void seek(long position) 58 throws IOException 59 { 60 byte[] data = key.getData(); 61 int index = data.length - 8; 62 63 position >>>= DbIndexOutput.BLOCK_SHIFT; 64 65 data[index + 0] = (byte) (0xff & (position >>> 56)); 66 data[index + 1] = (byte) (0xff & (position >>> 48)); 67 data[index + 2] = (byte) (0xff & (position >>> 40)); 68 data[index + 3] = (byte) (0xff & (position >>> 32)); 69 data[index + 4] = (byte) (0xff & (position >>> 24)); 70 data[index + 5] = (byte) (0xff & (position >>> 16)); 71 data[index + 6] = (byte) (0xff & (position >>> 8)); 72 data[index + 7] = (byte) (0xff & (position >>> 0)); 73 } 74 75 protected void get(DbDirectory directory) 76 throws IOException 77 { 78 try { 79 directory.blocks.get(directory.txn, key, data, directory.flags); 80 } catch (DatabaseException e) { 81 throw new IOException(e.getMessage()); 82 } 83 } 84 85 protected void put(DbDirectory directory) 86 throws IOException 87 { 88 try { 89 directory.blocks.put(directory.txn, key, data, 0); 90 } catch (DatabaseException e) { 91 throw new IOException(e.getMessage()); 92 } 93 } 94 }