1 8 9 package com.sleepycat.je.recovery; 10 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 import java.util.SortedMap ; 15 import java.util.TreeMap ; 16 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.dbi.DatabaseId; 19 import com.sleepycat.je.dbi.DatabaseImpl; 20 import com.sleepycat.je.dbi.EnvironmentImpl; 21 import com.sleepycat.je.dbi.INList; 22 import com.sleepycat.je.dbi.MemoryBudget; 23 import com.sleepycat.je.recovery.Checkpointer.CheckpointReference; 24 import com.sleepycat.je.tree.IN; 25 26 30 class DirtyINMap { 31 32 private EnvironmentImpl envImpl; 33 private SortedMap dirtyMap; 34 private int numEntries; 35 private int highestLevelSeen; 36 37 DirtyINMap(EnvironmentImpl envImpl) { 38 this.envImpl = envImpl; 39 } 40 41 48 void selectDirtyINsForCheckpoint(Set mustSyncSet) 49 throws DatabaseException { 50 51 dirtyMap = new TreeMap (); 52 numEntries = 0; 53 highestLevelSeen = IN.MIN_LEVEL; 54 55 INList inMemINs = envImpl.getInMemoryINs(); 56 inMemINs.latchMajor(); 57 58 68 long totalSize = 0; 69 MemoryBudget mb = envImpl.getMemoryBudget(); 70 71 try { 72 Iterator iter = inMemINs.iterator(); 73 while (iter.hasNext()) { 74 IN in = (IN) iter.next(); 75 in.latch(false); 76 77 try { 78 totalSize = mb.accumulateNewUsage(in, totalSize); 79 80 84 if (in.getDatabase().isDeferredWrite() && 85 !mustSyncSet.contains(in.getDatabase().getId())){ 86 continue; 87 } 88 89 addDirtyIN(in, false); 90 } finally { 91 in.releaseLatch(); 92 } 93 } 94 95 96 mb.refreshTreeMemoryUsage(totalSize); 97 98 } finally { 99 inMemINs.releaseMajorLatchIfHeld(); 100 } 101 } 102 103 107 void selectDirtyINsForDb(DatabaseImpl dbImpl) 108 throws DatabaseException { 109 110 dirtyMap = new TreeMap (); 111 DatabaseId dbId = dbImpl.getId(); 112 INList inMemINs = envImpl.getInMemoryINs(); 113 inMemINs.latchMajor(); 114 115 try { 116 Iterator iter = inMemINs.iterator(); 117 while (iter.hasNext()) { 118 IN in = (IN) iter.next(); 119 if (in.getDatabaseId().equals(dbId)) { 120 in.latch(false); 121 try { 122 addDirtyIN(in, false); 123 } finally { 124 in.releaseLatch(); 125 } 126 } 127 } 128 } finally { 129 inMemINs.releaseMajorLatchIfHeld(); 130 } 131 } 132 133 int getNumLevels() { 134 return dirtyMap.size(); 135 } 136 137 int getHighestLevel() { 138 return highestLevelSeen; 139 } 140 141 void addCostToMemoryBudget() { 142 MemoryBudget mb = envImpl.getMemoryBudget(); 143 int cost = numEntries * MemoryBudget.CHECKPOINT_REFERENCE_SIZE; 144 mb.updateMiscMemoryUsage(cost); 145 } 146 147 void removeCostFromMemoryBudget() { 148 MemoryBudget mb = envImpl.getMemoryBudget(); 149 int cost = numEntries * MemoryBudget.CHECKPOINT_REFERENCE_SIZE; 150 mb.updateMiscMemoryUsage(0 - cost); 151 } 152 153 157 void addDirtyIN(IN in, boolean updateMemoryBudget) { 158 if (in.getDirty()) { 159 int levelVal = in.getLevel(); 160 if (levelVal > highestLevelSeen) { 161 highestLevelSeen = levelVal; 162 } 163 164 Integer level = new Integer (levelVal); 165 Set dirtySet; 166 if (dirtyMap.containsKey(level)) { 167 dirtySet = (Set ) dirtyMap.get(level); 168 } else { 169 dirtySet = new HashSet (); 170 dirtyMap.put(level, dirtySet); 171 } 172 173 dirtySet.add(new CheckpointReference(in.getDatabase(), 174 in.getNodeId(), 175 in.containsDuplicates(), 176 in.isDbRoot(), 177 in.getMainTreeKey(), 178 in.getDupTreeKey())); 179 numEntries++; 180 181 if (updateMemoryBudget) { 182 MemoryBudget mb = envImpl.getMemoryBudget(); 183 mb.updateMiscMemoryUsage 184 (MemoryBudget.CHECKPOINT_REFERENCE_SIZE); 185 } 186 } 187 } 188 189 192 Integer getLowestLevelSet() { 193 return (Integer ) dirtyMap.firstKey(); 194 } 195 196 199 Set getSet(Integer level) { 200 return (Set ) dirtyMap.get(level); 201 } 202 203 206 void removeSet(Integer level) { 207 dirtyMap.remove(level); 208 } 209 } 210 | Popular Tags |