2424import org .redisson .codec .JsonJacksonCodec ;
2525import org .redisson .api .RedissonClient ;
2626import org .redisson .api .RMap ;
27+ import org .redisson .api .RMapCache ;
2728
2829/**
29- * Redis implementation of KeyValueMap data structure.
30+ * Hazelcast implementation of KeyValueMap data structure.
3031 *
3132 * Built ontop of the Core_KeyValueMap implementation.
3233 **/
3334public class Redis_KeyValueMap extends Core_KeyValueMap {
34-
35- //--------------------------------------------------------------------------
35+
36+ //--------------------------------------------------------------------------
3637 //
3738 // Constructor
3839 //
3940 //--------------------------------------------------------------------------
4041
42+ /** Redis instance representing the backend connection */
4143 RedisStack redisStack = null ;
4244 RedissonClient redisson = null ;
43- RMap <String , Object > redisMap = null ;
45+ // RMap<String, Object> redisMap = null;
46+ RMap <String , String > redisMap = null ;
4447
4548 /**
4649 * Constructor, with name constructor
@@ -54,7 +57,8 @@ public Redis_KeyValueMap(RedisStack inStack, String name) {
5457 redisson = inStack .getConnection ();
5558 redisMap = redisson .getMap (name , JsonJacksonCodec .INSTANCE );
5659 }
57- //--------------------------------------------------------------------------
60+
61+ //--------------------------------------------------------------------------
5862 //
5963 // Local cache
6064 //
@@ -83,70 +87,72 @@ private String name() {
8387 // Return config cachename
8488 return _name ;
8589 }
86-
87- /**
90+
91+ /**
8892 * @return backendmap memoizer
8993 */
90- private RMap <String , String > _backendRMap = null ;
94+ // private RMap<String, Map<String, Object>> _backendRMap = null;
95+ private RMapCache <String , String > _backendRMap = null ;
9196
9297 /**
9398 * @return Storage map used for the backend operations of one "DataObjectMap"
9499 * identical to valueMap, made to be compliant with Core_DataObjectMap_struct
95100 */
96- protected RMap <String , String > backendMap () {
101+ // protected RMap<String, Map<String, Object>> backendMap() {
102+ protected RMapCache <String , String > backendMap () {
97103 if (_backendRMap != null ) {
98104 return _backendRMap ;
99105 }
100- _backendRMap = redisson .getMap (name (), JsonJacksonCodec .INSTANCE );
106+ _backendRMap = redisson .getMapCache (name (), JsonJacksonCodec .INSTANCE );
101107 return _backendRMap ;
102108 }
103-
104- //--------------------------------------------------------------------------
109+
110+ //--------------------------------------------------------------------------
105111 //
106- // Backend system setup / teardown (DStackCommon)
112+ // Backend system setup / teardown / maintenance (DStackCommon)
107113 //
108114 //--------------------------------------------------------------------------
109115
110116 /**
111- * Removes all data, without tearing down setup
112- **/
113- @ Override
114- public void clear () {
115- //Delete all the keys of the currently selected database
116- redisson .getKeys ().flushdb ();
117-
118- //Delete all the keys of all the existing databases
119- redisson .getKeys ().flushall ();
120- }
121-
122- /**
123117 * Setsup the backend storage table, etc. If needed
124118 **/
125119 @ Override
126120 public void systemSetup () {
127- // does nothing
128121 }
129-
122+
130123 /**
131124 * Teardown and delete the backend storage table, etc. If needed
132125 **/
133126 public void systemDestroy () {
134127 redisMap .delete ();
135128 }
136-
129+
130+ /**
131+ * Removes all data, without tearing down setup
132+ **/
133+ @ Override
134+ public void clear () {
135+ //Delete all the keys of the currently selected database
136+ //redisson.getKeys().flushdb();
137+
138+ //Delete all the keys of all the existing databases
139+ redisson .getKeys ().flushall ();
140+ }
141+
137142 /**
138143 * Perform maintenance, mainly removing of expired data if applicable
139144 **/
140145 @ Override
141146 public void maintenance () {
142147 // does nothing
143148 }
144-
145- //--------------------------------------------------------------------------
149+
150+ //--------------------------------------------------------------------------
146151 //
147152 // KeySet support implementation
148153 //
149154 //--------------------------------------------------------------------------
155+
150156 /**
151157 * Search using the value, all the relevent key mappings
152158 *
@@ -157,19 +163,21 @@ public void maintenance() {
157163 * @return array of keys
158164 **/
159165 @ Override
160- public Set <String > keySet (String value ) {
166+ public Set <String > keySet (String value ) { //TODO
161167 // The return hashset
162168 HashSet <String > ret = new HashSet <String >();
163169 //Fetch everything in current db
164- List <String > retList = null ;
165- if (value != null ) {
166- retList = new ArrayList <String >(redisMap .readAllKeySet ());
170+ List <String > retList = null ;
171+ // if (value != null) {
172+ //backendMap().get(value);
173+ //backendMap().keyset();
174+ retList = new ArrayList <String >(backendmap ().readAllKeySet ());
167175 // Return the full keyset
168176 retList .forEach (k -> ret .add (k ));
169- }
177+ // }
170178 return ret ;
171179 }
172-
180+
173181 //--------------------------------------------------------------------------
174182 //
175183 // Fundemental set/get value (core)
@@ -188,12 +196,15 @@ public Set<String> keySet(String value) {
188196 * @return
189197 **/
190198 public void setExpiryRaw (String key , long time ) {
191- // if (time > 0) {
192- // backendMap().setTtl(key, Math.max(time - System.currentTimeMillis(), 1),
193- // TimeUnit.MILLISECONDS);
194- // } else {
195- // backendMap().setTtl(key, 0, TimeUnit.MILLISECONDS);
196- // }
199+ if (time > 0 ) {
200+ backendMap ()
201+ .updateEntryExpiration (key , Math .max (time - System .currentTimeMillis (), 1 ),
202+ TimeUnit .MILLISECONDS ,0 ,TimeUnit .MILLISECONDS );
203+ } else {
204+ backendMap ()
205+ .updateEntryExpiration (key , 0 ,
206+ TimeUnit .MILLISECONDS ,0 ,TimeUnit .MILLISECONDS );
207+ }
197208 }
198209
199210 /**
@@ -209,22 +220,23 @@ public void setExpiryRaw(String key, long time) {
209220 * @return null
210221 **/
211222 public String setValueRaw (String key , String value , long expire ) {
212- // // removal
213- // if (value == null) {
214- // backendMap().remove(key);
215- // return null;
216- // }
223+ // removal
224+ if (value == null ) {
225+ backendMap ().remove (key );
226+ return null ;
227+ }
217228
218- // // Setup key, value - with expirary?
219- // if (expire > 0) {
220- // backendMap().set(key, value, Math.max(expire - System.currentTimeMillis(), 1),
221- // TimeUnit.MILLISECONDS);
222- // } else {
223- // backendMap().set(key, value);
224- // }
229+ // Setup key, value - with expirary?
230+ //TODO Use fastput ?
231+ if (expire > 0 ) {
232+ backendMap ().put (key , value , Math .max (expire - System .currentTimeMillis (), 1 ),
233+ TimeUnit .MILLISECONDS );
234+ } else {
235+ backendMap ().put (key , value );
236+ }
225237 return null ;
226238 }
227-
239+
228240 /**
229241 * [Internal use, to be extended in future implementation]
230242 *
@@ -238,28 +250,28 @@ public String setValueRaw(String key, String value, long expire) {
238250 * @return String value, and expiry pair
239251 **/
240252 public MutablePair <String , Long > getValueExpiryRaw (String key , long now ) {
241- // // Get the entry view
242- // EntryView<String, String> entry = backendMap().getEntryView(key);
243- // if (entry == null) {
244- // return null;
245- // }
253+
254+ // Get the entry view
255+ Map .Entry <String , String > entry = backendMap ().get (key );
256+ if (entry == null ) {
257+ return null ;
258+ }
246259
247- // // Get the value and expire object : milliseconds?
248- // String value = entry.getValue();
249- // Long expireObj = entry.getExpirationTime();
250- // if (expireObj == null) {
251- // expireObj = 0L;
252- // }
260+ // Get the value and expire object : milliseconds?
261+ String value = entry .getValue ();
262+ Long expireObj = entry .getExpirationTime ();
263+ if (expireObj == null ) {
264+ expireObj = 0L ;
265+ }
253266
254- // // Note: 0 = no timestamp, hence valid value
255- // long expiry = expireObj.longValue();
256- // if (expiry != 0 && expiry < now) {
257- // return null;
258- // }
267+ // Note: 0 = no timestamp, hence valid value
268+ long expiry = expireObj .longValue ();
269+ if (expiry != 0 && expiry < now ) {
270+ return null ;
271+ }
259272
260- // // Return the expirary pair
261- // return new MutablePair<String, Long>(value, expiry);
262- return null ; //TO REMOVE
273+ // Return the expirary pair
274+ return new MutablePair <String , Long >(value , expiry );
263275 }
264-
276+
265277}
0 commit comments