@Override public long append(LogEntry logEntry) { try{ PreparedStatement ps = this.connection.prepareStatement(INSERT_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.execute(); this.connection.commit(); this.lastEntry = logEntry; return this.nextIndex.getAndIncrement(); }catch(Throwable error){ this.logger.error("failed to insert a new entry", error); throw new RuntimeException("log store error", error); } }
@Override public long append(LogEntry logEntry) { try{ PreparedStatement ps = this.connection.prepareStatement(INSERT_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.execute(); this.connection.commit(); this.lastEntry = logEntry; return this.nextIndex.getAndIncrement(); }catch(Throwable error){ this.logger.error("failed to insert a new entry", error); throw new RuntimeException("log store error", error); } }
@Override public void writeAt(long index, LogEntry logEntry) { if(index >= this.nextIndex.get() || index < this.startIndex.get()){ throw new IllegalArgumentException("index out of range"); } try{ PreparedStatement ps = this.connection.prepareStatement(UPDATE_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.setLong(4, index); ps.execute(); ps = this.connection.prepareStatement(TRIM_TABLE_SQL); ps.setLong(1, index); ps.execute(); ps = this.connection.prepareStatement(UPDATE_SEQUENCE_SQL); ps.setLong(1, index + 1); ps.execute(); this.connection.commit(); this.nextIndex.set(index + 1); this.lastEntry = logEntry; }catch(Throwable error){ this.logger.error("failed to write an entry at a specific index", error); throw new RuntimeException("log store error", error); } }
@Override public void writeAt(long index, LogEntry logEntry) { if(index >= this.nextIndex.get() || index < this.startIndex.get()){ throw new IllegalArgumentException("index out of range"); } try{ PreparedStatement ps = this.connection.prepareStatement(UPDATE_ENTRY_SQL); ps.setLong(1, logEntry.getTerm()); ps.setByte(2, logEntry.getValueType().toByte()); ps.setBytes(3, logEntry.getValue()); ps.setLong(4, index); ps.execute(); ps = this.connection.prepareStatement(TRIM_TABLE_SQL); ps.setLong(1, index); ps.execute(); ps = this.connection.prepareStatement(UPDATE_SEQUENCE_SQL); ps.setLong(1, index + 1); ps.execute(); this.connection.commit(); this.nextIndex.set(index + 1); this.lastEntry = logEntry; }catch(Throwable error){ this.logger.error("failed to write an entry at a specific index", error); throw new RuntimeException("log store error", error); } }
public static byte[] logEntryToBytes(LogEntry logEntry){ ByteArrayOutputStream output = new ByteArrayOutputStream(); try{ output.write(longToBytes(logEntry.getTerm())); output.write(logEntry.getValueType().toByte()); output.write(intToBytes(logEntry.getValue().length)); output.write(logEntry.getValue()); output.flush(); return output.toByteArray(); }catch(IOException exception){ LogManager.getLogger("BinaryUtil").error("failed to serialize LogEntry to memory", exception); throw new RuntimeException("Running into bad situation, where memory may not be sufficient", exception); } }
@Override public long append(LogEntry logEntry) { try{ this.storeWriteLock.lock(); this.indexFile.seek(this.indexFile.length()); long dataFileLength = this.dataFile.length(); this.indexFile.writeLong(dataFileLength); this.dataFile.seek(dataFileLength); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue()); this.dataFile.write(buffer.array()); this.entriesInStore += 1; this.buffer.append(logEntry); return this.entriesInStore + this.startIndex - 1; }catch(IOException exception){ this.logger.error("failed to append a log entry to store", exception); throw new RuntimeException(exception.getMessage(), exception); }finally{ this.storeWriteLock.unlock(); } }
public static byte[] logEntryToBytes(LogEntry logEntry){ ByteArrayOutputStream output = new ByteArrayOutputStream(); try{ output.write(longToBytes(logEntry.getTerm())); output.write(logEntry.getValueType().toByte()); output.write(intToBytes(logEntry.getValue().length)); output.write(logEntry.getValue()); output.flush(); return output.toByteArray(); }catch(IOException exception){ LogManager.getLogger("BinaryUtil").error("failed to serialize LogEntry to memory", exception); throw new RuntimeException("Running into bad situation, where memory may not be sufficient", exception); } }
@Override public long append(LogEntry logEntry) { try{ this.storeWriteLock.lock(); this.indexFile.seek(this.indexFile.length()); long dataFileLength = this.dataFile.length(); this.indexFile.writeLong(dataFileLength); this.dataFile.seek(dataFileLength); ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue()); this.dataFile.write(buffer.array()); this.entriesInStore += 1; this.buffer.append(logEntry); return this.entriesInStore + this.startIndex - 1; }catch(IOException exception){ this.logger.error("failed to append a log entry to store", exception); throw new RuntimeException(exception.getMessage(), exception); }finally{ this.storeWriteLock.unlock(); } }
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue());
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1 + logEntry.getValue().length); buffer.putLong(logEntry.getTerm()); buffer.put(logEntry.getValueType().toByte()); buffer.put(logEntry.getValue());