Skip to content

Commit ffc6f9f

Browse files
committed
Added KeyValueMap func
1 parent b75d893 commit ffc6f9f

6 files changed

Lines changed: 105 additions & 84 deletions

File tree

src/main/java/picoded/dstack/redis/RedisStack.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ protected Core_DataStructure initDataStructure(String name, String type) {
135135
if (type.equalsIgnoreCase("DataObjectMap")) {
136136
ret = new Redis_DataObjectMap(this, name);
137137
}
138+
if (type.equalsIgnoreCase("KeyValueMap")) {
139+
ret = new Redis_KeyValueMap(this, name);
140+
}
141+
if (type.equalsIgnoreCase("KeyLongMap")) {
142+
ret = new Redis_KeyLongMap(this, name);
143+
}
138144
// If datastrucutre initialized, setup name
139145
if (ret != null) {
140146
ret.configMap().put("name", name);

src/main/java/picoded/dstack/redis/Redis_DataObjectMap.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class Redis_DataObjectMap extends Core_DataObjectMap_struct {
5252
RedisStack redisStack = null;
5353
RedissonClient redisson = null;
5454
RMap<String, Object> redisMap = null;
55-
//RSet<Object> set = null;
5655

5756
/**
5857
* Constructor, with name constructor
@@ -65,7 +64,6 @@ public Redis_DataObjectMap(RedisStack inStack, String name) {
6564
redisStack = inStack;
6665
redisson = inStack.getConnection();
6766
redisMap = redisson.getMap(name, JsonJacksonCodec.INSTANCE);
68-
//set = redisson.getSet(name, StringCodec.INSTANCE);
6967
}
7068

7169
//--------------------------------------------------------------------------
@@ -134,7 +132,7 @@ public void systemSetup() {
134132
@Override
135133
public void clear() {
136134
//Delete all the keys of the currently selected database
137-
redisson.getKeys().flushdb();
135+
//redisson.getKeys().flushdb();
138136

139137
//Delete all the keys of all the existing databases
140138
redisson.getKeys().flushall();

src/main/java/picoded/dstack/redis/Redis_KeyLongMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ public Set<String> keySet(Long value) {
158158
HashSet<String> ret = new HashSet<String>();
159159
//Fetch everything in current db
160160
List<String> retList = null;
161-
if (value != null) {
161+
//if (value != null) {
162162
retList = new ArrayList<String>(redisMap.readAllKeySet());
163163
// Return the full keyset
164164
retList.forEach(k -> ret.add(k));
165-
}
165+
//}
166166
return ret;
167167
}
168168

src/main/java/picoded/dstack/redis/Redis_KeyValueMap.java

Lines changed: 87 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@
2424
import org.redisson.codec.JsonJacksonCodec;
2525
import org.redisson.api.RedissonClient;
2626
import 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
**/
3334
public 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
}

src/test/java/picoded/dstack/redis/Redis_KeyLongMap_test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public KeyLongMap implementationConstructor() {
2727
redisConfig.put("name", DStackTestConfig.randomTablePrefix());
2828

2929
GenericConvertMap<String, Object> stackConfig = new GenericConvertHashMap<>();
30-
stackConfig.put("name", "RedisKeyLongMap_test");
30+
stackConfig.put("name", "Redis_KeyLongMap_test");
3131
stackConfig.put("redis", redisConfig);
3232

3333
instance = new RedisStack(stackConfig);

src/test/java/picoded/dstack/redis/Redis_KeyValueMap_test.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
import java.util.concurrent.ThreadLocalRandom;
88

9-
9+
/**
10+
* ## Purpose
11+
* This class is meant to test the Redis_KeyValueMap implementation,
12+
* and ensure that it passes all the test layed out in StructSimple_KeyValueMap_test
13+
*
14+
*/
1015
public class Redis_KeyValueMap_test extends StructSimple_KeyValueMap_test {
1116
// Redis stack instance
1217
protected static volatile RedisStack instance = null;
@@ -28,7 +33,7 @@ public KeyValueMap implementationConstructor() {
2833
redisConfig.put("name", DStackTestConfig.randomTablePrefix());
2934

3035
GenericConvertMap<String, Object> stackConfig = new GenericConvertHashMap<>();
31-
stackConfig.put("name", "RedisKeyValueMap_test");
36+
stackConfig.put("name", "Redis_KeyValueMap_test");
3237
stackConfig.put("redis", redisConfig);
3338

3439
instance = new RedisStack(stackConfig);
@@ -37,4 +42,4 @@ public KeyValueMap implementationConstructor() {
3742
// Load the KeyValueMap
3843
return instance.keyValueMap(DStackTestConfig.randomTablePrefix());
3944
}
40-
}
45+
}

0 commit comments

Comments
 (0)