| 1 | package org.jtoolkit.essence.data; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.concurrency.Concurrency; |
| 6 | import org.jtoolkit.essence.concurrency.ThreadSafe; |
| 7 | import org.jtoolkit.essence.utils.Closeable; |
| 8 | import org.jtoolkit.essence.utils.Named; |
| 9 | |
| 10 | import javax.cache.Cache; |
| 11 | import java.util.Map; |
| 12 | import java.util.Queue; |
| 13 | import java.util.concurrent.ConcurrentMap; |
| 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 | /** |
| 32 | * @author Peter Lawrey |
| 33 | */ |
| 34 | @ThreadSafe(Concurrency.CONCURRENT_READ_WRITE) |
| 35 | public interface Store<K, V> extends Event.Listenable, Closeable, Named, Visitor.Visitable<Store<K, V>> { |
| 36 | /** |
| 37 | * @return The cluster this store is a member of. |
| 38 | */ |
| 39 | @Nullable public Cluster getCluster(); |
| 40 | /** |
| 41 | * Set to add/remove event listeners and event handlers to/from |
| 42 | * |
| 43 | * @return This object has a set of listeners. |
| 44 | */ |
| 45 | @NotNull public Event.ListenerSet getListenerSet(); |
| 46 | |
| 47 | /** |
| 48 | * @return The contents of this Store as a Map. |
| 49 | */ |
| 50 | @NotNull public Cache<K, V> getCacheView(); |
| 51 | |
| 52 | /** |
| 53 | * @return The contents of this Store as a Map. |
| 54 | */ |
| 55 | @NotNull public ConcurrentMap<K, V> getMapView(); |
| 56 | |
| 57 | /** |
| 58 | * @return The contents of this Store as a Queue. |
| 59 | */ |
| 60 | @NotNull public Queue<V> getQueueView(); |
| 61 | |
| 62 | /** |
| 63 | * @return A snap shot copy of the Store. |
| 64 | */ |
| 65 | @NotNull public Map<K, V> asMap(); |
| 66 | |
| 67 | /** |
| 68 | * @return The matching results. |
| 69 | */ |
| 70 | public Map<K, V> selectMatching(Predicate<Map.Entry<K, V>> predicate); |
| 71 | |
| 72 | public int removeMatching(Predicate<Map.Entry<K, V>> predicate); |
| 73 | |
| 74 | /** |
| 75 | * An implmentation can be notified of changes to the Store. |
| 76 | */ |
| 77 | public interface StoreListener<K, V> extends Event.Listener { |
| 78 | public void onUpdate(@NotNull K key, @NotNull V value); |
| 79 | |
| 80 | public void onRemoved(@NotNull K key, @NotNull V value); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * What types of Collections are allowed. |
| 85 | */ |
| 86 | enum CollectionType { |
| 87 | /** |
| 88 | * Any type is allowed including Map, Queue, Store and Event.Listenable |
| 89 | */ |
| 90 | ANY, |
| 91 | /** |
| 92 | * Only Map is allowed. |
| 93 | */ |
| 94 | MAP, |
| 95 | /** |
| 96 | * Only Collection or Queue interfaces allowed |
| 97 | */ |
| 98 | QUEUE |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * How is the data read. |
| 103 | */ |
| 104 | enum ReadMode { |
| 105 | /** |
| 106 | * A default mode for the store is chosen. All Stores support this mode. |
| 107 | */ |
| 108 | ANY, |
| 109 | /** |
| 110 | * Data is not read from the persisted store. This can mean the store is truncated or only new data is visable. |
| 111 | */ |
| 112 | NONE, |
| 113 | /** |
| 114 | * All data is read into memory. |
| 115 | */ |
| 116 | ALL, |
| 117 | /** |
| 118 | * Only the minimum of data is read to perform each operation. |
| 119 | */ |
| 120 | MINIMAL |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * How is the data persisted. |
| 125 | */ |
| 126 | enum PersistMode { |
| 127 | /** |
| 128 | * A default mode for the store is chosen. All Stores support this mode. |
| 129 | */ |
| 130 | ANY, |
| 131 | /** |
| 132 | * Data can be modified in memory but is not persisted. |
| 133 | */ |
| 134 | NONE, |
| 135 | /** |
| 136 | * Data is persisted in large batches. This is typically done witha 500-5000 ms delay. |
| 137 | */ |
| 138 | ASYNC, |
| 139 | /** |
| 140 | * Data is persisted by a background thread as quickly as is efficient. Typically 25 - 250 ms. |
| 141 | */ |
| 142 | SEMI_SYNC, |
| 143 | /** |
| 144 | * Data is always persisted before the operation returns. |
| 145 | */ |
| 146 | SYNC, |
| 147 | /** |
| 148 | * Data cannot be updates and throws an IllegalStateException if an attempt is made. |
| 149 | */ |
| 150 | READ_ONLY |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * How is the Store represented/persisted. |
| 155 | */ |
| 156 | enum StoreType { |
| 157 | /** |
| 158 | * Data is volatile and in memory only. Any kind of object is supported. The key type must implement equals() and hashCode() Data can be initialised from the configuration file. |
| 159 | */ |
| 160 | MEMORY, |
| 161 | /** |
| 162 | * The store is a proxy for another store in another Container/JVM/machine. DataValues, Datable and Serializable objects are supported. |
| 163 | */ |
| 164 | NET, |
| 165 | /** |
| 166 | * The store is backed by a JDBC database. DataValues are supported. This is the most scalable and reliable solution. |
| 167 | * Not current supported. |
| 168 | */ |
| 169 | JDBC, |
| 170 | /** |
| 171 | * The store is backed by a tab seperated file. DataValues are supported. |
| 172 | */ |
| 173 | TABS, |
| 174 | /** |
| 175 | * The store is backed by a binary format file. DataValues, Datable and Serializable objects are supported. |
| 176 | */ |
| 177 | SINGLE_FILE, |
| 178 | /** |
| 179 | * The store is backed by a directory of small binary format files. DataValues, Datable and Serializable objects are supported. |
| 180 | * This provides beter update performance and scalability than a single file or tabs could provide. |
| 181 | */ |
| 182 | FILE_SET |
| 183 | } |
| 184 | } |