1 8 9 package com.sleepycat.je.log; 10 11 import java.nio.ByteBuffer ; 12 13 import com.sleepycat.je.DatabaseException; 14 import com.sleepycat.je.dbi.EnvironmentImpl; 15 import com.sleepycat.je.latch.Latch; 16 import com.sleepycat.je.latch.LatchSupport; 17 import com.sleepycat.je.utilint.DbLsn; 18 19 22 class LogBuffer implements LogSource { 23 24 private static final String DEBUG_NAME = LogBuffer.class.getName(); 25 26 27 private ByteBuffer buffer; 28 29 30 private long firstLsn; 31 private long lastLsn; 32 33 34 private Latch readLatch; 35 36 39 private boolean rewriteAllowed; 40 41 LogBuffer(int capacity, EnvironmentImpl env) 42 throws DatabaseException { 43 44 if (env.useDirectNIO()) { 45 buffer = ByteBuffer.allocateDirect(capacity); 46 } else { 47 buffer = ByteBuffer.allocate(capacity); 48 } 49 readLatch = LatchSupport.makeLatch(DEBUG_NAME, env); 50 reinit(); 51 } 52 53 59 LogBuffer(ByteBuffer buffer, long firstLsn) 60 throws DatabaseException { 61 62 this.buffer = buffer; 63 this.firstLsn = firstLsn; 64 this.lastLsn = firstLsn; 65 rewriteAllowed = false; 66 } 67 68 void reinit() 69 throws DatabaseException { 70 71 readLatch.acquire(); 72 buffer.clear(); 73 firstLsn = DbLsn.NULL_LSN; 74 lastLsn = DbLsn.NULL_LSN; 75 rewriteAllowed = false; 76 readLatch.release(); 77 } 78 79 82 83 87 long getFirstLsn() { 88 return firstLsn; 89 } 90 91 94 void registerLsn(long lsn) 95 throws DatabaseException { 96 97 readLatch.acquire(); 98 try { 99 if (lastLsn != DbLsn.NULL_LSN) { 100 assert (DbLsn.compareTo(lsn, lastLsn) > 0); 101 } 102 lastLsn = lsn; 103 if (firstLsn == DbLsn.NULL_LSN) { 104 firstLsn = lsn; 105 } 106 } finally { 107 readLatch.release(); 108 } 109 } 110 111 115 boolean hasRoom(int numBytes) { 116 return (numBytes <= (buffer.capacity() - buffer.position())); 117 } 118 119 122 ByteBuffer getDataBuffer() { 123 return buffer; 124 } 125 126 129 int getCapacity() { 130 return buffer.capacity(); 131 } 132 133 136 137 143 boolean containsLsn(long lsn) 144 throws DatabaseException { 145 146 147 readLatch.acquire(); 148 boolean found = false; 149 if ((firstLsn != DbLsn.NULL_LSN) && 150 ((DbLsn.compareTo(firstLsn, lsn) <= 0) && 151 (DbLsn.compareTo(lastLsn, lsn) >= 0))) { 152 found = true; 153 } 154 155 if (found) { 156 return true; 157 } else { 158 readLatch.release(); 159 return false; 160 } 161 } 162 163 168 public void latchForWrite() 169 throws DatabaseException { 170 171 readLatch.acquire(); 172 } 173 174 177 178 181 public void release() 182 throws DatabaseException { 183 184 readLatch.releaseIfOwner(); 185 } 186 187 boolean getRewriteAllowed() { 188 return rewriteAllowed; 189 } 190 191 void setRewriteAllowed() { 192 rewriteAllowed = true; 193 } 194 195 198 public ByteBuffer getBytes(long fileOffset) { 199 200 213 ByteBuffer copy = null; 214 while (true) { 215 try { 216 copy = buffer.duplicate(); 217 copy.position((int) 218 (fileOffset - DbLsn.getFileOffset(firstLsn))); 219 break; 220 } catch (IllegalArgumentException IAE) { 221 continue; 222 } 223 } 224 return copy; 225 } 226 227 230 public ByteBuffer getBytes(long fileOffset, int numBytes) { 231 ByteBuffer copy = getBytes(fileOffset); 232 233 assert (copy.remaining() >= numBytes) : 234 "copy.remaining=" + copy.remaining() + 235 " numBytes=" + numBytes; 236 return copy; 237 } 238 } 239 | Popular Tags |