01: package org.apache.lucene.store.je;
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.je.DatabaseEntry;
23: import com.sleepycat.je.DatabaseException;
24:
25: /**
26: * Port of Andi Vajda's DbDirectory to Java Edition of Berkeley Database
27: *
28: * @author Aaron Donovan
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: data = new DatabaseEntry(new byte[JEIndexOutput.BLOCK_LEN]);
39:
40: System.arraycopy(fileKey, 0, key.getData(), 0, fileKey.length);
41: seek(0L);
42: }
43:
44: protected byte[] getKey() {
45: return key.getData();
46: }
47:
48: protected byte[] getData() {
49: return data.getData();
50: }
51:
52: protected void seek(long position) throws IOException {
53: byte[] data = key.getData();
54: int index = data.length - 8;
55:
56: position >>>= JEIndexOutput.BLOCK_SHIFT;
57:
58: data[index + 0] = (byte) (0xff & (position >>> 56));
59: data[index + 1] = (byte) (0xff & (position >>> 48));
60: data[index + 2] = (byte) (0xff & (position >>> 40));
61: data[index + 3] = (byte) (0xff & (position >>> 32));
62: data[index + 4] = (byte) (0xff & (position >>> 24));
63: data[index + 5] = (byte) (0xff & (position >>> 16));
64: data[index + 6] = (byte) (0xff & (position >>> 8));
65: data[index + 7] = (byte) (0xff & (position >>> 0));
66: }
67:
68: protected void get(JEDirectory directory) throws IOException {
69: try {
70: // TODO check LockMode
71: directory.blocks.get(directory.txn, key, data, null);
72: } catch (DatabaseException e) {
73: throw new IOException(e.getMessage());
74: }
75: }
76:
77: protected void put(JEDirectory directory) throws IOException {
78: try {
79: directory.blocks.put(directory.txn, key, data);
80: } catch (DatabaseException e) {
81: throw new IOException(e.getMessage());
82: }
83: }
84: }
|