1 8 9 package com.sleepycat.je.tree; 10 11 import java.nio.ByteBuffer ; 12 import java.util.Comparator ; 13 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.dbi.CursorImpl; 16 import com.sleepycat.je.dbi.DatabaseId; 17 import com.sleepycat.je.dbi.DatabaseImpl; 18 import com.sleepycat.je.dbi.DbConfigManager; 19 import com.sleepycat.je.dbi.MemoryBudget; 20 import com.sleepycat.je.log.LogEntryType; 21 import com.sleepycat.je.log.LogException; 22 import com.sleepycat.je.log.LogUtils; 23 import com.sleepycat.je.log.LoggableObject; 24 25 28 public final class DBIN extends BIN implements LoggableObject { 29 private static final String BEGIN_TAG = "<dbin>"; 30 private static final String END_TAG = "</dbin>"; 31 32 35 private byte[] dupKey; 36 37 public DBIN() { 38 super(); 39 } 40 41 public DBIN(DatabaseImpl db, 42 byte[] identifierKey, 43 int maxEntriesPerNode, 44 byte[] dupKey, 45 int level) { 46 super(db, identifierKey, maxEntriesPerNode, level); 47 this.dupKey = dupKey; 48 } 49 50 54 protected IN createNewInstance(byte[] identifierKey, 55 int maxEntries, 56 int level) { 57 return new DBIN(getDatabase(), 58 identifierKey, 59 maxEntries, 60 dupKey, 61 level); 62 } 63 64 69 boolean isAlwaysLatchedExclusively() { 70 return true; 71 } 72 73 74 protected int generateLevel(DatabaseId dbId, int newLevel) { 75 return newLevel; 76 } 77 78 82 public final Comparator getKeyComparator() { 83 return getDatabase().getDuplicateComparator(); 84 } 85 86 89 public byte[] getDupKey() { 90 return dupKey; 91 } 92 93 97 public byte[] getChildKey(IN child) 98 throws DatabaseException { 99 100 return child.getIdentifierKey(); 101 } 102 103 106 public byte[] selectKey(byte[] mainTreeKey, byte[] dupTreeKey) { 107 return dupTreeKey; 108 } 109 110 113 public byte[] getDupTreeKey() { 114 return getIdentifierKey(); 115 } 116 117 120 public byte[] getMainTreeKey() { 121 return dupKey; 122 } 123 124 128 public boolean containsDuplicates() { 129 return true; 130 } 131 132 135 LogEntryType getBINDeltaType() { 136 return LogEntryType.LOG_DUP_BIN_DELTA; 137 } 138 139 public BINReference createReference() { 140 return new DBINReference(getNodeId(), getDatabase().getId(), 141 getIdentifierKey(), dupKey); 142 } 143 144 147 protected long computeMemorySize() { 148 long size = super.computeMemorySize(); 149 154 return size; 155 } 156 157 158 public static long computeOverhead(DbConfigManager configManager) 159 throws DatabaseException { 160 161 165 return MemoryBudget.DBIN_FIXED_OVERHEAD + 166 IN.computeArraysOverhead(configManager); 167 } 168 169 protected long getMemoryOverhead(MemoryBudget mb) { 170 return mb.getDBINOverhead(); 171 } 172 173 176 protected boolean canBeAncestor(boolean targetContainsDuplicates) { 177 return false; 178 } 179 180 183 boolean hasNonLNChildren() { 184 return false; 185 } 186 187 194 BIN getCursorBIN(CursorImpl cursor) { 195 return cursor.getDupBIN(); 196 } 197 198 BIN getCursorBINToBeRemoved(CursorImpl cursor) { 199 return cursor.getDupBINToBeRemoved(); 200 } 201 202 int getCursorIndex(CursorImpl cursor) { 203 return cursor.getDupIndex(); 204 } 205 206 void setCursorBIN(CursorImpl cursor, BIN bin) { 207 cursor.setDupBIN((DBIN) bin); 208 } 209 210 void setCursorIndex(CursorImpl cursor, int index) { 211 cursor.setDupIndex(index); 212 } 213 214 221 boolean matchLNByNodeId(TreeLocation location, long nodeId) 222 throws DatabaseException { 223 224 latch(); 225 try { 226 for (int i = 0; i < getNEntries(); i++) { 227 LN ln = (LN) fetchTarget(i); 228 if (ln != null) { 229 if (ln.getNodeId() == nodeId) { 230 location.bin = this; 231 location.index = i; 232 location.lnKey = getKey(i); 233 location.childLsn = getLsn(i); 234 return true; 235 } 236 } 237 } 238 239 return false; 240 } finally { 241 releaseLatch(); 242 } 243 } 244 245 248 void accumulateStats(TreeWalkerStatsAccumulator acc) { 249 acc.processDBIN(this, new Long (getNodeId()), getLevel()); 250 } 251 252 public String beginTag() { 253 return BEGIN_TAG; 254 } 255 256 public String endTag() { 257 return END_TAG; 258 } 259 260 264 public String dumpString(int nSpaces, boolean dumpTags) { 265 StringBuffer sb = new StringBuffer (); 266 sb.append(TreeUtils.indent(nSpaces)); 267 sb.append(beginTag()); 268 sb.append('\n'); 269 270 sb.append(TreeUtils.indent(nSpaces+2)); 271 sb.append("<dupkey>"); 272 sb.append(dupKey == null ? "" : Key.dumpString(dupKey, 0)); 273 sb.append("</dupkey>"); 274 sb.append('\n'); 275 276 sb.append(super.dumpString(nSpaces, false)); 277 278 sb.append(TreeUtils.indent(nSpaces)); 279 sb.append(endTag()); 280 return sb.toString(); 281 } 282 283 286 287 290 public LogEntryType getLogType() { 291 return LogEntryType.LOG_DBIN; 292 } 293 294 297 public int getLogSize() { 298 int size = super.getLogSize(); size += LogUtils.getByteArrayLogSize(dupKey); return size; 301 } 302 303 306 public void writeToLog(ByteBuffer logBuffer) { 307 308 super.writeToLog(logBuffer); 310 311 LogUtils.writeByteArray(logBuffer, dupKey); 313 } 314 315 318 public void readFromLog(ByteBuffer itemBuffer, byte entryTypeVersion) 319 throws LogException { 320 321 super.readFromLog(itemBuffer, entryTypeVersion); 323 324 dupKey = LogUtils.readByteArray(itemBuffer); 326 } 327 328 331 protected void dumpLogAdditional(StringBuffer sb) { 332 super.dumpLogAdditional(sb); 333 sb.append(Key.dumpString(dupKey, 0)); 334 } 335 336 public String shortClassName() { 337 return "DBIN"; 338 } 339 } 340 | Popular Tags |