/** * 追加一条事务日志 */ public StoreTxLogPosition append(Operation op, K key) throws DBException { return this.append(op, key, null); }
public long getNextRecordId() { return fileHeader.getFirstRecordId() + fileLength; }
@Override public void close() throws IOException { checkPoint(); }
public Cursor<StoreTxLogCursorEntry<K, V>> cursor(StoreTxLogPosition position) { long recordId = position.getRecordId(); if (storeTxLogs.size() == 0) { return new EmptyCursor<StoreTxLogCursorEntry<K, V>>(); } StoreTxLog targetTxLog = null; for (StoreTxLog txLog : storeTxLogs) { if (recordId >= txLog.getFirstRecordId() && recordId < txLog.getNextRecordId()) { targetTxLog = txLog; break; } } if (targetTxLog == null) { return new EmptyCursor<StoreTxLogCursorEntry<K, V>>(); } return new StoreTxLogCursor(targetTxLog, recordId - targetTxLog.getFirstRecordId()); }
private StoreTxLog nextNewStoreTxLog() throws IOException { long firstRecordId = storeTxLog.getNextRecordId(); String name = System.currentTimeMillis() + LOG_FILE_SUFFIX; StoreTxLog newStoreTxLog = new StoreTxLog(storeConfig, new File(logPath, name), false, true, firstRecordId); storeTxLogs.add(newStoreTxLog); storeTxLog.setNext(newStoreTxLog); storeTxLog = newStoreTxLog; return storeTxLog; }
private StoreTxLogPosition append(StoreTxLog storeTxLog, Operation op, K key, V value) throws IOException { StoreTxLogEntry<K, V> entry = null; long timestamp = System.currentTimeMillis(); switch (op) { case PUT: entry = new StoreTxLogEntry<K, V>(op, key, value, timestamp); break; case REMOVE: entry = new StoreTxLogEntry<K, V>(op, key, timestamp); break; } UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); try { serializer.serialize(entry, out); byte[] entryBytes = out.toByteArray(); return storeTxLog.append(entryBytes); } finally { out.close(); } }
/** * 追加一条事务日志 */ public StoreTxLogPosition append(Operation op, K key, V value) throws DBException { try { try { return this.append(storeTxLog, op, key, value); } catch (CapacityNotEnoughException notEnough) { // 要新建一个文件 return this.append(nextNewStoreTxLog(), op, key, value); } } catch (Exception e) { throw new DBException("append dbLog error:" + e.getMessage(), e); } }
public int getHeaderLength() { return fileHeader.getLength(); }
public StoreTxLogPosition getLastTxLogPosition() { return new StoreTxLogPosition(fileHeader.getStoreTxLogRecordId()); }
@Override public void write(FileChannel fileChannel) throws IOException { byteBuffer().position(0); byteBuffer().putShort(magic); byteBuffer().putLong(this.firstRecordId); byteBuffer().flip(); fileChannel.position(0); fileChannel.write(byteBuffer()); fileChannel.force(true); } }
public Cursor<StoreTxLogCursorEntry<K, V>> cursor(StoreTxLogPosition position) { long recordId = position.getRecordId(); if (storeTxLogs.size() == 0) { return new EmptyCursor<StoreTxLogCursorEntry<K, V>>(); } StoreTxLog targetTxLog = null; for (StoreTxLog txLog : storeTxLogs) { if (recordId >= txLog.getFirstRecordId() && recordId < txLog.getNextRecordId()) { targetTxLog = txLog; break; } } if (targetTxLog == null) { return new EmptyCursor<StoreTxLogCursorEntry<K, V>>(); } return new StoreTxLogCursor(targetTxLog, recordId - targetTxLog.getFirstRecordId()); }
private StoreTxLogPosition append(StoreTxLog storeTxLog, Operation op, K key, V value) throws IOException { StoreTxLogEntry<K, V> entry = null; long timestamp = System.currentTimeMillis(); switch (op) { case PUT: entry = new StoreTxLogEntry<K, V>(op, key, value, timestamp); break; case REMOVE: entry = new StoreTxLogEntry<K, V>(op, key, timestamp); break; } UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); try { serializer.serialize(entry, out); byte[] entryBytes = out.toByteArray(); return storeTxLog.append(entryBytes); } finally { out.close(); } }
public long getNextRecordId() { return fileHeader.getFirstRecordId() + fileLength; }
/** * 追加一条事务日志 */ public StoreTxLogPosition append(Operation op, K key) throws DBException { return this.append(op, key, null); }
@Override public void close() throws IOException { checkPoint(); }
public int getHeaderLength() { return fileHeader.getLength(); }
public StoreTxLogPosition getLastTxLogPosition() { return new StoreTxLogPosition(fileHeader.getStoreTxLogRecordId()); }
@Override public void write(FileChannel fileChannel) throws IOException { byteBuffer().position(0); byteBuffer().putShort(magic); byteBuffer().putLong(this.firstRecordId); byteBuffer().flip(); fileChannel.position(0); fileChannel.write(byteBuffer()); fileChannel.force(true); } }
public long getFirstRecordId() { return fileHeader.getFirstRecordId(); }
public long getFirstRecordId() { return fileHeader.getFirstRecordId(); }