| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jtoolkit.essence.concurrency.Concurrency; |
| 5 | import org.jtoolkit.essence.concurrency.ThreadSafe; |
| 6 | import org.jtoolkit.essence.concurrency.Timeout; |
| 7 | import org.jtoolkit.essence.data.*; |
| 8 | |
| 9 | import javax.cache.Cache; |
| 10 | import java.util.Map; |
| 11 | import java.util.Queue; |
| 12 | import java.util.concurrent.ConcurrentMap; |
| 13 | import java.util.concurrent.locks.Lock; |
| 14 | |
| 15 | /* |
| 16 | Copyright 2006 Peter Lawrey |
| 17 | |
| 18 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | you may not use this file except in compliance with the License. |
| 20 | You may obtain a copy of the License at |
| 21 | |
| 22 | http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | |
| 24 | Unless required by applicable law or agreed to in writing, software |
| 25 | distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | See the License for the specific language governing permissions and |
| 28 | limitations under the License. |
| 29 | */ |
| 30 | /** |
| 31 | * The purpose of ClusterStore to act as a Store for a Cluster |
| 32 | * |
| 33 | * @author Peter Lawrey |
| 34 | * @since 25-Nov-2006 |
| 35 | */ |
| 36 | @ThreadSafe(Concurrency.CONCURRENT_READ_WRITE) |
| 37 | public class ClusterStore<K, V> implements Store<K, V> { |
| 38 | private final Cluster cluster; |
| 39 | private final String name; |
| 40 | private boolean closed = false; |
| 41 | |
| 42 | public ClusterStore(String name, Cluster cluster) { |
| 43 | this.name = name; |
| 44 | this.cluster = cluster; |
| 45 | } |
| 46 | |
| 47 | public Cluster getCluster() { |
| 48 | return cluster; |
| 49 | } |
| 50 | |
| 51 | @NotNull public Event.ListenerSet getListenerSet() { |
| 52 | return cluster.getListenerSet(name); |
| 53 | } |
| 54 | |
| 55 | @NotNull public Cache<K, V> getCacheView() { |
| 56 | return cluster.getMapView(name); |
| 57 | } |
| 58 | |
| 59 | @NotNull public ConcurrentMap<K, V> getMapView() { |
| 60 | return cluster.getMapView(name); |
| 61 | } |
| 62 | |
| 63 | @NotNull public Queue<V> getQueueView() { |
| 64 | return cluster.getQueueView(name); |
| 65 | } |
| 66 | |
| 67 | @NotNull public Map<K, V> asMap() { |
| 68 | return cluster.getMapView(name); |
| 69 | } |
| 70 | |
| 71 | public Map<K, V> selectMatching(Predicate<Map.Entry<K, V>> predicate) { |
| 72 | return cluster.selectMatching(name, predicate); |
| 73 | } |
| 74 | |
| 75 | public int removeMatching(Predicate<Map.Entry<K, V>> predicate) { |
| 76 | return cluster.removeMatching(name, predicate); |
| 77 | } |
| 78 | |
| 79 | public void close() { |
| 80 | closed = true; |
| 81 | } |
| 82 | |
| 83 | public boolean isClosed() { |
| 84 | return closed; |
| 85 | } |
| 86 | |
| 87 | @NotNull public String getName() { |
| 88 | return name; |
| 89 | } |
| 90 | |
| 91 | public <R> R visit(@NotNull Visitor<Store<K, V>, R> visitor) throws VisitorException { |
| 92 | Lock writeLock = ((ClusterImpl) cluster).getBackingStore(name).getLock().writeLock(); |
| 93 | Transaction t = Transaction.start("visit ClusterStore", Timeout.NEVER); |
| 94 | try { |
| 95 | writeLock.lockInterruptibly(); |
| 96 | R ret = visitor.visit(this); |
| 97 | t.commit(); |
| 98 | return ret; |
| 99 | } catch (InterruptedException ie) { |
| 100 | throw new VisitorException(ie); |
| 101 | } finally { |
| 102 | writeLock.unlock(); |
| 103 | t.complete(); |
| 104 | } |
| 105 | } |
| 106 | } |