/** * Applies a journal entry commit to the state machine. * * This method is automatically discovered by the Copycat framework. * * @param commit the commit */ public synchronized void applyJournalEntryCommand(Commit<JournalEntryCommand> commit) { JournalEntry entry; try { entry = JournalEntry.parseFrom(commit.command().getSerializedJournalEntry()); } catch (Exception e) { ProcessUtils.fatalError(LOG, e, "Encountered invalid journal entry in commit: {}.", commit); System.exit(-1); throw new IllegalStateException(e); // We should never reach here. } try { applyEntry(entry); } finally { Preconditions.checkState(commit.index() > mLastAppliedCommitIndex); mLastAppliedCommitIndex = commit.index(); commit.close(); } }
/** * Handles a get commit. */ public T get(Commit<ValueCommands.Get<T>> commit) { try { return current != null ? value : null; } finally { commit.close(); } }
/** * Handles a count commit. */ public int size(Commit<SetCommands.Size> commit) { try { return map != null ? map.size() : 0; } finally { commit.close(); } }
/** * Handles a decrement and get commit. */ public long decrementAndGet(Commit<LongCommands.DecrementAndGet> commit) { try { return value.decrementAndGet(); } finally { commit.close(); } }
/** * Handles an add commit. */ public boolean add(Commit<QueueCommands.Add> commit) { try { queue.add(commit); } catch (Exception e) { commit.close(); throw e; } return false; }
public long index(Commit<Index> commit) { try { return commit.index(); } finally { commit.close(); } } }
public long index(Commit<Index> commit) { try { return commit.index(); } finally { commit.close(); } } }
/** * Handles a delete command. */ private void delete(Commit<ResourceCommand.Delete> commit) { try { delete(); } finally { commit.close(); } }
@Override public void delete() { if (lock != null) { lock.close(); } queue.forEach(Commit::close); queue.clear(); timers.values().forEach(Scheduled::cancel); timers.clear(); }
@Override public void delete() { Iterator<Map.Entry<Object, Value>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<Object, Value> entry = iterator.next(); Value value = entry.getValue(); if (value.timer != null) value.timer.cancel(); value.commit.close(); iterator.remove(); } }
@Override public void delete() { Iterator<Map.Entry<Object, Value>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<Object, Value> entry = iterator.next(); Value value = entry.getValue(); if (value.timer != null) value.timer.cancel(); value.commit.close(); iterator.remove(); } }
/** * Handles a clear commit. */ public void clear(Commit<MultiMapCommands.Clear> commit) { try { delete(); } finally { commit.close(); } }
public String get(Commit<Get> commit) { try { Commit<Put> value = map.get(commit.operation().key); return value != null ? value.operation().value : null; } finally { commit.close(); } }
public String get(Commit<Get> commit) { try { Commit<Put> value = map.get(commit.operation().key); return value != null ? value.operation().value : null; } finally { commit.close(); } }
/** * Handles a set commit. */ @Override public void set(Commit<ValueCommands.Set<Long>> commit) { try { value.set(commit.operation().value()); } finally { commit.close(); } }
/** * Handles a contains key commit. */ public boolean containsKey(Commit<MapCommands.ContainsKey> commit) { try { return map.containsKey(commit.operation().key()); } finally { commit.close(); } }
/** * Handles a get or default commit. */ public Object getOrDefault(Commit<MapCommands.GetOrDefault> commit) { try { Value value = map.get(commit.operation().key()); return value != null ? value.commit.operation().value() : commit.operation().defaultValue(); } finally { commit.close(); } }