1 8 9 package com.sleepycat.je.tree; 10 11 import java.nio.ByteBuffer ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.dbi.DatabaseId; 17 import com.sleepycat.je.dbi.DatabaseImpl; 18 import com.sleepycat.je.dbi.EnvironmentImpl; 19 import com.sleepycat.je.log.LogEntryType; 20 import com.sleepycat.je.log.LogException; 21 import com.sleepycat.je.log.LogReadable; 22 import com.sleepycat.je.log.LogUtils; 23 import com.sleepycat.je.log.LoggableObject; 24 import com.sleepycat.je.utilint.DbLsn; 25 26 31 public class BINDelta implements LoggableObject, LogReadable { 32 33 private DatabaseId dbId; private long lastFullLsn; private List deltas; private LogEntryType logEntryType; 39 42 public BINDelta(BIN bin) { 43 lastFullLsn = bin.getLastFullVersion(); 44 dbId = bin.getDatabaseId(); 45 deltas = new ArrayList (); 46 logEntryType = bin.getBINDeltaType(); 47 48 55 for (int i = 0; i < bin.getNEntries(); i++) { 56 if (bin.isDirty(i)) { 57 deltas.add(new DeltaInfo(bin.getKey(i), 58 bin.getLsn(i), 59 bin.getState(i))); 60 } 61 } 62 } 63 64 67 public BINDelta() { 68 dbId = new DatabaseId(); 69 lastFullLsn = DbLsn.NULL_LSN; 70 deltas = new ArrayList (); 71 } 72 73 76 int getNumDeltas() { 77 return deltas.size(); 78 } 79 80 83 public DatabaseId getDbId() { 84 return dbId; 85 } 86 87 90 public long getLastFullLsn() { 91 return lastFullLsn; 92 } 93 94 97 public BIN reconstituteBIN(EnvironmentImpl env) 98 throws DatabaseException { 99 100 101 BIN fullBIN = (BIN) env.getLogManager().get(lastFullLsn); 102 DatabaseImpl db = env.getDbMapTree().getDb(dbId); 103 104 110 fullBIN.setDatabase(db); 111 fullBIN.setLastFullLsn(lastFullLsn); 112 113 114 fullBIN.latch(); 115 for (int i = 0; i < deltas.size(); i++) { 116 DeltaInfo info = (DeltaInfo) deltas.get(i); 117 118 129 int foundIndex = fullBIN.findEntry(info.getKey(), true, false); 130 if (foundIndex >= 0 && 131 (foundIndex & IN.EXACT_MATCH) != 0) { 132 foundIndex &= ~IN.EXACT_MATCH; 133 134 138 if (info.isKnownDeleted()) { 139 fullBIN.setKnownDeleted(foundIndex); 140 } else { 141 fullBIN.updateEntry 142 (foundIndex, info.getLsn(), info.getState()); 143 } 144 } else { 145 146 if (!info.isKnownDeleted()) { 147 ChildReference entry = 148 new ChildReference(null, 149 info.getKey(), 150 info.getLsn(), 151 info.getState()); 152 boolean insertOk = fullBIN.insertEntry(entry); 153 assert insertOk; 154 } 155 } 156 } 157 158 162 fullBIN.setGeneration(0); 163 fullBIN.releaseLatch(); 164 return fullBIN; 165 } 166 167 170 171 174 public LogEntryType getLogType() { 175 return logEntryType; 176 } 177 178 182 public boolean marshallOutsideWriteLatch() { 183 return true; 184 } 185 186 189 public boolean countAsObsoleteWhenLogged() { 190 return false; 191 } 192 193 198 public void postLogWork(long justLoggedLsn) { 199 } 200 201 205 public void readFromLog(ByteBuffer itemBuffer,byte entryTypeVersion) 206 throws LogException { 207 208 dbId.readFromLog(itemBuffer, entryTypeVersion); lastFullLsn = LogUtils.readLong(itemBuffer); int numDeltas = LogUtils.readInt(itemBuffer); 211 212 for (int i=0; i < numDeltas; i++) { DeltaInfo info = new DeltaInfo(); 214 info.readFromLog(itemBuffer, entryTypeVersion); 215 deltas.add(info); 216 } 217 } 218 219 222 public int getLogSize() { 223 int size = 224 dbId.getLogSize() + LogUtils.LONG_BYTES + LogUtils.INT_BYTES; 228 for (int i = 0; i < deltas.size(); i++) { DeltaInfo info = (DeltaInfo) deltas.get(i); 230 size += info.getLogSize(); 231 } 232 233 return size; 234 } 235 236 240 public void writeToLog(ByteBuffer logBuffer) { 241 dbId.writeToLog(logBuffer); LogUtils.writeLong(logBuffer, lastFullLsn); LogUtils.writeInt(logBuffer, deltas.size()); 245 for (int i = 0; i < deltas.size(); i++) { DeltaInfo info = (DeltaInfo) deltas.get(i); 247 info.writeToLog(logBuffer); 248 } 249 } 250 251 254 public void dumpLog(StringBuffer sb, boolean verbose) { 255 dbId.dumpLog(sb, verbose); 256 sb.append("<lastFullLsn>"); 257 sb.append(DbLsn.toString(lastFullLsn)); 258 sb.append("</lastFullLsn>"); 259 sb.append("<deltas size=\"").append(deltas.size()).append("\"/>"); 260 for (int i = 0; i < deltas.size(); i++) { DeltaInfo info = (DeltaInfo) deltas.get(i); 262 info.dumpLog(sb, verbose); 263 } 264 } 265 266 269 public boolean logEntryIsTransactional() { 270 return false; 271 } 272 273 276 public long getTransactionId() { 277 return 0; 278 } 279 } 280 | Popular Tags |