@Override public boolean release() { return parent.release(); }
/** * Handles a count commit. */ public int size(Commit<QueueCommands.Size> commit) { try { return queue.size(); } finally { commit.release(); } }
/** * Handles an is empty commit. */ public boolean isEmpty(Commit<QueueCommands.IsEmpty> commit) { try { return queue.isEmpty(); } finally { commit.release(); } }
@Override public void delete() { Iterator<Commit<? extends QueueCommands.ValueCommand>> iterator = queue.iterator(); while (iterator.hasNext()) { Commit<? extends QueueCommands.ValueCommand> value = iterator.next(); value.release(); iterator.remove(); } }
@Override public boolean release() { if (commit.release()) { pool.release(this); return true; } return false; }
/** * Handles a clear commit. */ public void clear(Commit<QueueCommands.Clear> commit) { try { delete(); } finally { commit.release(); } }
@Override public void close(ServerSession session) { // Remove the session from event listeners. Iterator<Map.Entry<Object, Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>>>> keyIterator = listeners.entrySet().iterator(); while (keyIterator.hasNext()) { Map.Entry<Object, Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>>> keyEntry = keyIterator.next(); Iterator<Map.Entry<Integer, Map<Long, Commit<MapCommands.KeyListen>>>> eventIterator = keyEntry.getValue().entrySet().iterator(); while (eventIterator.hasNext()) { Map.Entry<Integer, Map<Long, Commit<MapCommands.KeyListen>>> eventEntry = eventIterator.next(); Map<Long, Commit<MapCommands.KeyListen>> sessions = eventEntry.getValue(); Commit<MapCommands.KeyListen> commit = sessions.remove(session.id()); if (commit != null) { commit.release(); if (sessions.isEmpty()) { eventIterator.remove(); } } } if (keyEntry.getValue().isEmpty()) { keyIterator.remove(); } } }
/** * Handles a peek commit. */ public Object peek(Commit<QueueCommands.Peek> commit) { try { Commit<? extends QueueCommands.ValueCommand> value = queue.peek(); if (value != null) { return value.operation().value(); } return null; } finally { commit.release(); } }
/** * Handles an element commit. */ public Object element(Commit<QueueCommands.Element> commit) { try { Commit<? extends QueueCommands.ValueCommand> value = queue.element(); if (value != null) { try { return value.operation().value(); } finally { value.release(); } } return null; } finally { commit.release(); } }
/** * Handles an offer commit. */ public boolean offer(Commit<QueueCommands.Offer> commit) { try { if (queue.offer(commit)) { notify(new ValueEvent<>(Events.ADD, commit.command().value())); return true; } else { commit.release(); return false; } } catch (Exception e) { commit.release(); throw e; } }
/** * Registers a key change listener. */ public void listen(Commit<MapCommands.KeyListen> commit) { Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>> listeners = this.listeners.computeIfAbsent(commit.command().key(), k -> new HashMap<>()); Map<Long, Commit<MapCommands.KeyListen>> sessions = listeners.computeIfAbsent(commit.command().event(), e -> new HashMap<>()); if (!sessions.containsKey(commit.session().id())) { sessions.put(commit.session().id(), commit); } else { commit.release(); } }
/** * Handles an add commit. */ public boolean add(Commit<QueueCommands.Add> commit) { try { queue.add(commit); notify(new ValueEvent<>(Events.ADD, commit.command().value())); } catch (Exception e) { commit.release(); throw e; } return true; }
/** * Handles a poll commit. */ public Object poll(Commit<QueueCommands.Poll> commit) { try { Commit<? extends QueueCommands.ValueCommand> value = queue.poll(); if (value != null) { try { notify(new ValueEvent<>(Events.REMOVE, value.command().value())); return value.operation().value(); } finally { value.release(); } } return null; } finally { commit.release(); } }
/** * Handles a contains commit. */ public boolean contains(Commit<QueueCommands.Contains> commit) { try { for (Commit<? extends QueueCommands.ValueCommand> value : queue) { if (value.operation().value().equals(commit.operation().value())) return true; } return false; } finally { commit.release(); } }
/** * Unregisters a key change listener. */ public void unlisten(Commit<MapCommands.KeyUnlisten> commit) { try { Map<Integer, Map<Long, Commit<MapCommands.KeyListen>>> listeners = this.listeners.get(commit.command().key()); if (listeners != null) { Map<Long, Commit<MapCommands.KeyListen>> sessions = listeners.get(commit.command().event()); if (sessions != null) { Commit<MapCommands.KeyListen> listen = sessions.remove(commit.session().id()); if (listen != null) { listen.release(); if (sessions.isEmpty()) { listeners.remove(commit.command().event()); if (listeners.isEmpty()) { this.listeners.remove(commit.command().key()); } } } } } } finally { commit.release(); } }