1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.mavibot.btree; 21 22 23 import java.io.IOException; 24 25 import org.apache.directory.mavibot.btree.serializer.ElementSerializer; 26 27 28 /** 29 * A class storing either a key, or an offset to the key on the page's byte[] 30 * 31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 32 * 33 * <K> The key type 34 */ 35 /* No qualifier */class PersistedKeyHolder<K> extends KeyHolder<K> 36 { 37 /** The ByteBuffer storing the key */ 38 private byte[] raw; 39 40 /** The Key serializer */ 41 private ElementSerializer<K> keySerializer; 42 43 44 /** 45 * Create a new KeyHolder instance 46 * @param keySerializer The KeySerializer instance 47 * @param key The key to store 48 */ 49 /* no qualifier */PersistedKeyHolder( ElementSerializer<K> keySerializer, K key ) 50 { 51 super( key ); 52 this.keySerializer = keySerializer; 53 raw = keySerializer.serialize( key ); 54 } 55 56 57 /** 58 * Create a new KeyHolder instance 59 * @param keySerializer The KeySerializer instance 60 * @param raw the bytes representing the serialized key 61 */ 62 /* no qualifier */PersistedKeyHolder( ElementSerializer<K> keySerializer, byte[] raw ) 63 { 64 super( null ); 65 this.keySerializer = keySerializer; 66 this.raw = raw; 67 } 68 69 70 /** 71 * @return the key 72 */ 73 /* no qualifier */K getKey() 74 { 75 if ( key == null ) 76 { 77 try 78 { 79 key = keySerializer.fromBytes( raw ); 80 } 81 catch ( IOException ioe ) 82 { 83 // Nothing we can do here... 84 } 85 } 86 87 return key; 88 } 89 90 91 /** 92 * @param key the Key to store in into the KeyHolder 93 */ 94 /* no qualifier */void setKey( K key ) 95 { 96 this.key = key; 97 raw = keySerializer.serialize( key ); 98 } 99 100 101 /** 102 * @return The internal serialized byte[] 103 */ 104 /* No qualifier */byte[] getRaw() 105 { 106 return raw; 107 } 108 109 110 /** 111 * @see Object#toString() 112 */ 113 public String toString() 114 { 115 StringBuilder sb = new StringBuilder(); 116 117 sb.append( "PersistedKeyHolder[" ); 118 119 if ( key != null ) 120 { 121 sb.append( key ); 122 sb.append( ", " ); 123 } 124 else 125 { 126 sb.append( "null," ); 127 } 128 129 if ( raw != null ) 130 { 131 sb.append( raw.length ); 132 } 133 else 134 { 135 sb.append( "null" ); 136 } 137 138 sb.append( "]" ); 139 140 return sb.toString(); 141 } 142 }