01: package org.apache.lucene.store.db;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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: * @author Andi Vajda
29: */
30:
31: public class Block extends Object {
32: protected DatabaseEntry key, data;
33:
34: protected Block(File file) throws IOException {
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: return key.getData();
49: }
50:
51: protected byte[] getData() {
52: return data.getData();
53: }
54:
55: protected void seek(long position) throws IOException {
56: byte[] data = key.getData();
57: int index = data.length - 8;
58:
59: position >>>= DbIndexOutput.BLOCK_SHIFT;
60:
61: data[index + 0] = (byte) (0xff & (position >>> 56));
62: data[index + 1] = (byte) (0xff & (position >>> 48));
63: data[index + 2] = (byte) (0xff & (position >>> 40));
64: data[index + 3] = (byte) (0xff & (position >>> 32));
65: data[index + 4] = (byte) (0xff & (position >>> 24));
66: data[index + 5] = (byte) (0xff & (position >>> 16));
67: data[index + 6] = (byte) (0xff & (position >>> 8));
68: data[index + 7] = (byte) (0xff & (position >>> 0));
69: }
70:
71: protected void get(DbDirectory directory) throws IOException {
72: try {
73: directory.blocks.get(directory.txn, key, data,
74: directory.flags);
75: } catch (DatabaseException e) {
76: throw new IOException(e.getMessage());
77: }
78: }
79:
80: protected void put(DbDirectory directory) throws IOException {
81: try {
82: directory.blocks.put(directory.txn, key, data, 0);
83: } catch (DatabaseException e) {
84: throw new IOException(e.getMessage());
85: }
86: }
87: }
|