|
| 1 | +package picoded.dstack.redis; |
| 2 | + |
| 3 | +// Java imports |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 13 | + |
| 14 | +// Picoded imports |
| 15 | +import picoded.core.conv.ConvertJSON; |
| 16 | +import picoded.core.common.ObjectToken; |
| 17 | +import picoded.core.struct.MutablePair; |
| 18 | +import picoded.dstack.*; |
| 19 | +import picoded.dstack.core.*; |
| 20 | + |
| 21 | +// Redis imports |
| 22 | +import org.redisson.Redisson; |
| 23 | +import org.redisson.client.codec.StringCodec; |
| 24 | +import org.redisson.codec.JsonJacksonCodec; |
| 25 | +import org.redisson.api.RedissonClient; |
| 26 | +import org.redisson.api.RMap; |
| 27 | + |
| 28 | +public class Redis_KeyLongMap extends Core_KeyLongMap { |
| 29 | + |
| 30 | + //-------------------------------------------------------------------------- |
| 31 | + // |
| 32 | + // Constructor |
| 33 | + // |
| 34 | + //-------------------------------------------------------------------------- |
| 35 | + |
| 36 | + RedisStack redisStack = null; |
| 37 | + RedissonClient redisson = null; |
| 38 | + RMap<String, Object> redisMap = null; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor, with name constructor |
| 42 | + * |
| 43 | + * @param inStack hazelcast stack to use |
| 44 | + * @param name of data object map to use |
| 45 | + */ |
| 46 | + public Redis_KeyLongMap(RedisStack inStack, String name) { |
| 47 | + super(); |
| 48 | + redisStack = inStack; |
| 49 | + redisson = inStack.getConnection(); |
| 50 | + redisMap = redisson.getMap(name, JsonJacksonCodec.INSTANCE); |
| 51 | + } |
| 52 | + |
| 53 | + //-------------------------------------------------------------------------- |
| 54 | + // |
| 55 | + // Local cache |
| 56 | + // |
| 57 | + //-------------------------------------------------------------------------- |
| 58 | + |
| 59 | + /** |
| 60 | + * @return name memoizer |
| 61 | + */ |
| 62 | + private String _name = null; |
| 63 | + |
| 64 | + /** |
| 65 | + * @return Get the internal map name, required to be in configMap |
| 66 | + */ |
| 67 | + private String name() { |
| 68 | + // Return memorized name |
| 69 | + if (_name != null) { |
| 70 | + return _name; |
| 71 | + } |
| 72 | + |
| 73 | + // Attempt to load cachename from config |
| 74 | + _name = configMap().getString("name"); |
| 75 | + if (_name == null || _name.equals("")) { |
| 76 | + throw new IllegalArgumentException("Missing name configuration"); |
| 77 | + } |
| 78 | + |
| 79 | + // Return config cachename |
| 80 | + return _name; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return backendmap memoizer |
| 85 | + */ |
| 86 | + private RMap<String, String> _backendRMap = null; |
| 87 | + |
| 88 | + /** |
| 89 | + * @return Storage map used for the backend operations of one "DataObjectMap" |
| 90 | + * identical to valueMap, made to be compliant with Core_DataObjectMap_struct |
| 91 | + */ |
| 92 | + protected RMap<String, String> backendMap() { |
| 93 | + if (_backendRMap != null) { |
| 94 | + return _backendRMap; |
| 95 | + } |
| 96 | + _backendRMap = redisson.getMap(name(), JsonJacksonCodec.INSTANCE); |
| 97 | + return _backendRMap; |
| 98 | + } |
| 99 | + |
| 100 | + //-------------------------------------------------------------------------- |
| 101 | + // |
| 102 | + // Backend system setup / teardown (DStackCommon) |
| 103 | + // |
| 104 | + //-------------------------------------------------------------------------- |
| 105 | + |
| 106 | + /** |
| 107 | + * Removes all data, without tearing down setup |
| 108 | + **/ |
| 109 | + @Override |
| 110 | + public void clear() { |
| 111 | + //Delete all the keys of the currently selected database |
| 112 | + redisson.getKeys().flushdb(); |
| 113 | + |
| 114 | + //Delete all the keys of all the existing databases |
| 115 | + redisson.getKeys().flushall(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Setsup the backend storage table, etc. If needed |
| 120 | + **/ |
| 121 | + @Override |
| 122 | + public void systemSetup() { |
| 123 | + // does nothing |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Teardown and delete the backend storage table, etc. If needed |
| 128 | + **/ |
| 129 | + public void systemDestroy() { |
| 130 | + redisMap.delete(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Perform maintenance, mainly removing of expired data if applicable |
| 135 | + **/ |
| 136 | + @Override |
| 137 | + public void maintenance() { |
| 138 | + // does nothing |
| 139 | + } |
| 140 | + |
| 141 | + //-------------------------------------------------------------------------- |
| 142 | + // |
| 143 | + // KeySet support implementation |
| 144 | + // |
| 145 | + //-------------------------------------------------------------------------- |
| 146 | + /** |
| 147 | + * Search using the value, all the relevent key mappings |
| 148 | + * |
| 149 | + * Handles re-entrant lock where applicable |
| 150 | + * |
| 151 | + * @param key, note that null matches ALL |
| 152 | + * |
| 153 | + * @return array of keys |
| 154 | + **/ |
| 155 | + @Override |
| 156 | + public Set<String> keySet(Long value) { |
| 157 | + // The return hashset |
| 158 | + HashSet<String> ret = new HashSet<String>(); |
| 159 | + //Fetch everything in current db |
| 160 | + List<String> retList = null; |
| 161 | + if (value != null) { |
| 162 | + retList = new ArrayList<String>(redisMap.readAllKeySet()); |
| 163 | + // Return the full keyset |
| 164 | + retList.forEach(k -> ret.add(k)); |
| 165 | + } |
| 166 | + return ret; |
| 167 | + } |
| 168 | + |
| 169 | + //-------------------------------------------------------------------------- |
| 170 | + // |
| 171 | + // Fundemental set/get value (core) |
| 172 | + // |
| 173 | + //-------------------------------------------------------------------------- |
| 174 | + |
| 175 | + /** |
| 176 | + * Stores (and overwrites if needed) key, value pair |
| 177 | + * |
| 178 | + * Important note: It does not return the previously stored value |
| 179 | + * |
| 180 | + * @param key as String |
| 181 | + * @param expect as Long |
| 182 | + * @param update as Long |
| 183 | + * |
| 184 | + * @return true if successful |
| 185 | + **/ |
| 186 | + @Override |
| 187 | + public boolean weakCompareAndSet(String key, Long expect, Long update) { |
| 188 | + // // Possibly a blank setup |
| 189 | + // if (expect == null) { |
| 190 | + // expect = new Long(0); |
| 191 | + // } |
| 192 | + // if (expect.longValue() == 0) { |
| 193 | + // backendMap().putIfAbsent(key, expect); |
| 194 | + // } |
| 195 | + |
| 196 | + // // Value update - doa atomically directly |
| 197 | + // return backendMap().replace(key, expect, update); |
| 198 | + return false; //TO REMOVE |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * [Internal use, to be extended in future implementation] |
| 203 | + * Sets the expire time stamp value, raw without validation |
| 204 | + * |
| 205 | + * Handles re-entrant lock where applicable |
| 206 | + * |
| 207 | + * @param key as String |
| 208 | + * @param expire TIMESTAMP in milliseconds, 0 means NO expire |
| 209 | + * |
| 210 | + * @return |
| 211 | + **/ |
| 212 | + public void setExpiryRaw(String key, long time) { |
| 213 | + // if (time > 0) { |
| 214 | + // backendMap().setTtl(key, Math.max(time - System.currentTimeMillis(), 1), |
| 215 | + // TimeUnit.MILLISECONDS); |
| 216 | + // } else { |
| 217 | + // backendMap().setTtl(key, 0, TimeUnit.MILLISECONDS); |
| 218 | + // } |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * [Internal use, to be extended in future implementation] |
| 223 | + * Sets the value, with validation |
| 224 | + * |
| 225 | + * Handles re-entrant lock where applicable |
| 226 | + * |
| 227 | + * @param key |
| 228 | + * @param value, null means removal |
| 229 | + * @param expire TIMESTAMP, 0 means not timestamp |
| 230 | + * |
| 231 | + * @return null |
| 232 | + **/ |
| 233 | + public Long setValueRaw(String key, Long value, long expire) { |
| 234 | + // // removal |
| 235 | + // if (value == null) { |
| 236 | + // backendMap().remove(key); |
| 237 | + // return null; |
| 238 | + // } |
| 239 | + |
| 240 | + // // Setup key, value - with expirary? |
| 241 | + // if (expire > 0) { |
| 242 | + // backendMap().set(key, value, Math.max(expire - System.currentTimeMillis(), 1), |
| 243 | + // TimeUnit.MILLISECONDS); |
| 244 | + // } else { |
| 245 | + // backendMap().set(key, value); |
| 246 | + // } |
| 247 | + return null; |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * [Internal use, to be extended in future implementation] |
| 252 | + * |
| 253 | + * Returns the value and expiry, with validation against the current timestamp |
| 254 | + * |
| 255 | + * Handles re-entrant lock where applicable |
| 256 | + * |
| 257 | + * @param key as String |
| 258 | + * @param now timestamp, 0 = no timestamp so skip timestamp checks |
| 259 | + * |
| 260 | + * @return Long value, and expiry pair |
| 261 | + **/ |
| 262 | + public MutablePair<Long, Long> getValueExpiryRaw(String key, long now) { |
| 263 | + // // Get the entry view |
| 264 | + // EntryView<String, Long> entry = backendMap().getEntryView(key); |
| 265 | + // if (entry == null) { |
| 266 | + // return null; |
| 267 | + // } |
| 268 | + |
| 269 | + // // Get the value and expire object : milliseconds? |
| 270 | + // Long value = entry.getValue(); |
| 271 | + // Long expireObj = entry.getExpirationTime(); |
| 272 | + // if (expireObj == null) { |
| 273 | + // expireObj = 0L; |
| 274 | + // } |
| 275 | + |
| 276 | + // // Note: 0 = no timestamp, hence valid value |
| 277 | + // long expiry = expireObj.longValue(); |
| 278 | + // if (expiry != 0 && expiry < now) { |
| 279 | + // return null; |
| 280 | + // } |
| 281 | + |
| 282 | + // // Return the expirary pair |
| 283 | + // return new MutablePair<Long, Long>(value, expiry); |
| 284 | + return null; // TO REMOVE |
| 285 | + } |
| 286 | + |
| 287 | +} |
0 commit comments