/** * 新添加一条记录 * * @return 返回记录ID */ public StoreTxLogPosition append(byte[] entry) throws IOException { int length = entry.length; if (length > storeConfig.getMaxxLogEntryLength()) { throw new DBException("Value size can not great than " + storeConfig.getMaxxLogEntryLength()); } // 检查当前文件容量是否足够 if (fileLength + length + ENTRY_HEAD_LENGTH > storeConfig.getTxLogFileSize()) { throw new CapacityNotEnoughException(); } StoreTxLogPosition result = new StoreTxLogPosition(); result.setRecordId(getNextRecordId()); boolean ok = false; try { entryBuffer.clear(); entryBuffer.put(magic); // 1 byte entryBuffer.putInt(length); // 4 byte entryBuffer.put(entry); // entry.length entryBuffer.flip(); fileChannel.position(fileLength); fileChannel.write(entryBuffer); int entryLength = (ENTRY_HEAD_LENGTH + length); fileLength += entryLength; ok = true; } finally { if (ok) { if (syncTimerTask == null || syncTimerTask.isDone()) { syncTimerTask = new FutureTimerTask("ltsdb-dblog-sync-timertask", syncCallable); syncTimer.schedule(syncTimerTask, storeConfig.getDbLogFlushInterval()); } } } return result; }