Skip to content

Commit 8b2967b

Browse files
committed
Almost there
1 parent 440bc54 commit 8b2967b

2 files changed

Lines changed: 119 additions & 101 deletions

File tree

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

Lines changed: 117 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Arrays;
1111
import java.util.concurrent.ConcurrentHashMap;
1212
import java.util.concurrent.locks.ReentrantReadWriteLock;
13+
import java.util.Collection;
14+
import java.util.Iterator;
1315

1416
// JavaCommons imports
1517
import picoded.core.conv.ConvertJSON;
@@ -26,6 +28,11 @@
2628
import org.redisson.api.RedissonClient;
2729
import org.redisson.api.RMap;
2830
import org.redisson.api.RKeys;
31+
import org.redisson.api.RSet;
32+
import org.redisson.api.RSetMultimap;
33+
34+
35+
import org.redisson.client.codec.StringCodec;
2936

3037
// import org.redisson.api.RSet;
3138

@@ -50,6 +57,7 @@ public class Redis_DataObjectMap extends Core_DataObjectMap_struct {
5057
RedisStack redisStack = null;
5158
RedissonClient redisson = null;
5259
RMap<String, Object> redisMap = null;
60+
//RSet<Object> set = null;
5361

5462
/**
5563
* Constructor, with name constructor
@@ -61,7 +69,8 @@ public Redis_DataObjectMap(RedisStack inStack, String name) {
6169
super();
6270
redisStack = inStack;
6371
redisson = inStack.getConnection();
64-
redisMap = redisson.getMap(name);
72+
redisMap = redisson.getMap(name, StringCodec.INSTANCE);
73+
//set = redisson.getSet(name, StringCodec.INSTANCE);
6574
}
6675

6776
//--------------------------------------------------------------------------
@@ -107,7 +116,7 @@ protected RMap<String, Map<String, Object>> backendMap() {
107116
if (_backendRMap != null) {
108117
return _backendRMap;
109118
}
110-
_backendRMap = redisson.getMap(name());
119+
_backendRMap = redisson.getMap(name(), StringCodec.INSTANCE);
111120
return _backendRMap;
112121
}
113122

@@ -124,24 +133,24 @@ protected RMap<String, Map<String, Object>> backendMap() {
124133
public void systemSetup() {
125134
}
126135

127-
/**
128-
* Teardown and delete the backend storage table, etc. If needed
129-
**/
130-
public void systemDestroy() {
131-
redisMap.delete();
132-
}
133-
134136
/**
135137
* Removes all data, without tearing down setup
136138
**/
137139
@Override
138140
public void clear() {
139141
//Delete all the keys of the currently selected database
140-
//redisson.getKeys().flushdb();
142+
redisson.getKeys().flushdb();
141143

142144
//Delete all the keys of all the existing databases
143145
redisson.getKeys().flushall();
144146
}
147+
148+
/**
149+
* Teardown and delete the backend storage table, etc. If needed
150+
**/
151+
public void systemDestroy() {
152+
//redisMap.delete();
153+
}
145154

146155
/**
147156
* Updates the actual backend storage of DataObject
@@ -151,76 +160,89 @@ public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> full
151160
Set<String> updateKeys) {
152161

153162
Map<String, Object> clonedMap = new HashMap<String, Object>();
154-
163+
155164
// Lets iterate the keys, and decide accordingly
156165
for (String key : fullMap.keySet()) {
157166
// Get the full map value
158167
Object val = fullMap.get(key);
159-
160-
// Check for Map / List like objects
161-
if (val instanceof Map || val instanceof List) {
162-
// Clone it - by JSON serializing back and forth
163-
clonedMap.put(key, ConvertJSON.toObject(ConvertJSON.fromObject(val)));
164-
} else {
165-
// Store it directly, this should be a primative, or byte[]
168+
169+
// // Check for Map / List like objects
170+
// if (val instanceof Map || val instanceof List) {
171+
// // Clone it - by JSON serializing back and forth
172+
// clonedMap.put(key, ConvertJSON.toObject(ConvertJSON.fromObject(val)));
173+
// }
174+
175+
if (updateKeys.contains(key)) {
166176
clonedMap.put(key, val);
167177
}
168178
}
169-
170-
// call the default implementation
179+
180+
//RSET test
181+
//check with SMEMBERS instead of HGETALL
182+
//set.add(clonedMap);
183+
184+
// call the default implementation, basically equal to redisMap.put(_oid,clonedMap)
171185
super.DataObjectRemoteDataMap_update(_oid, clonedMap, updateKeys);
186+
187+
//Print map slice for the _oid
188+
// Set<String> keys = new HashSet<String>();
189+
// keys.add(_oid);
190+
// Map<String, Object> mapSlice = redisMap.getAll(keys);
191+
// System.out.println(mapSlice);
172192
}
173193

174194
/**
175195
* Gets the complete remote data map, for DataObject.
176196
* @return null if not exists, else a map with the data
177197
**/
178198
public Map<String, Object> DataObjectRemoteDataMap_get(String _oid) {
179-
180-
RMap<String, Object> res = redisMap;
181199

182-
// System.out.println("------------RMap content--------------:");
183-
// System.out.println(res.readAllMap()); // all map entries
184-
// System.out.println(res.readAllEntrySet()); // everything
185-
// System.out.println(res.readAllValues()); // all values
186-
// System.out.println(res.readAllKeySet()); // just keys
187-
// System.out.println("------------RMap END--------------:");
200+
Collection<Object> res = redisMap.values(_oid);
201+
202+
Object resObj = res.iterator().next();
203+
if (resObj == null) {
204+
return null;
205+
}
206+
207+
//NEED TO FIND HOW TO CONVERT resObj TO MAP
188208

189209
Map<String, Object> ret = new HashMap<>();
190210

191-
Set<String> fullKeys = res.keySet();
211+
// Lets iterate through the object
212+
Set<String> fullKeys = resObj.keySet();
192213
for (String key : fullKeys) {
193-
214+
194215
// Get the value
195-
Object val = res.get(key);
196-
216+
Object val = resObj.get(key);
217+
197218
// Populate the ret map
198219
ret.put(key, val);
199220
}
200-
201-
return ret;
202-
}
203221

204-
/**
205-
* @return set of keys
206-
**/
207-
public Set<String> keySet(String value) {
208-
// The return hashset
209-
HashSet<String> ret = new HashSet<String>();
210-
211-
//Fetch everything in current db
212-
RKeys keySet = redisson.getKeys();
213-
if (value != null) {
214-
// Return key where value is matched
215-
keySet.getKeysByPattern(value).forEach(k -> ret.add(k));
216-
}
217-
else {
218-
// Return the full keyset
219-
keySet.getKeys().forEach(k -> ret.add(k));
220-
}
221222
return ret;
222223
}
223224

225+
// /**
226+
// * @return set of keys
227+
// **/
228+
// @Override
229+
// public Set<String> keySet() {
230+
// System.out.println("KEYSET");
231+
// // The return hashset
232+
// HashSet<String> ret = new HashSet<String>();
233+
234+
// //Fetch everything in current db
235+
// RKeys keySet = redisson.getKeys();
236+
// if (value != null) {
237+
// // Return key where value is matched
238+
// keySet.getKeysByPattern(value).forEach(k -> ret.add(k));
239+
// } else {
240+
// // Return the full keyset
241+
// keySet.getKeys().forEach(k -> ret.add(k));
242+
// }
243+
// return ret;
244+
// }
245+
224246
/**
225247
* [Internal use, to be extended in future implementation]
226248
*
@@ -232,10 +254,10 @@ public Set<String> keySet(String value) {
232254
* @return nothing
233255
**/
234256
public void DataObjectRemoteDataMap_remove(String _oid) {
235-
//redisson.getKeys().delete(_oid);
257+
// //redisson.getKeys().delete(_oid);
236258
redisMap.fastRemove(_oid);
237259
}
238-
260+
239261
/**
240262
* Performs a search query, and returns the respective DataObject keys.
241263
*
@@ -248,46 +270,46 @@ public void DataObjectRemoteDataMap_remove(String _oid) {
248270
*
249271
* @return The String[] array
250272
**/
251-
public String[] query_id(Query queryClause, String orderByStr, int offset, int limit) {
252-
253-
// The return list of DataObjects
254-
List<String> retList = null;
255-
256-
RMap<String, Object> myRedisMap = redisMap;
257-
258-
// Setup the query, if needed
259-
if (queryClause == null) {
260-
// Null gets all
261-
retList = new ArrayList<String>(myRedisMap.readAllKeySet());
262-
} else {
263-
264-
// Get the list of _oid that passes the query
265-
//Set<String> idSet = backendIMap().keySet(queryPredicate);
266-
//String[] idArr = idSet.toArray(new String[0]);
267-
268-
// DataObject[] from idArr
269-
//DataObject[] doArr = getArrayFromID(idArr, true);
270-
271-
// Converts to a list
272-
//retList = new ArrayList(Arrays.asList(doArr));
273-
retList = new ArrayList<String>(myRedisMap.readAllKeySet());
274-
}
275-
276-
// Sort, offset, convert to array, and return
277-
// ???
278-
279-
// Prepare the actual return string array
280-
int retLength = retList.size();
281-
String[] ret = new String[retLength];
282-
for (int a = 0; a < retLength; ++a) {
283-
//._oid(); -> where is it coming from
284-
//ret[a] = retList.get(a)._oid();
285-
ret[a] = String.valueOf(retList.get(a));
286-
}
287-
288-
System.out.println(ret);
289-
// Returns sorted array of strings
290-
return ret;
291-
}
273+
// public String[] query_id(Query queryClause, String orderByStr, int offset, int limit) {
274+
275+
// // The return list of DataObjects
276+
// List<String> retList = null;
277+
278+
// RMap<String, Object> myRedisMap = redisMap;
279+
280+
// // Setup the query, if needed
281+
// if (queryClause == null) {
282+
// // Null gets all
283+
// retList = new ArrayList<String>(myRedisMap.readAllKeySet());
284+
// } else {
285+
286+
// // Get the list of _oid that passes the query
287+
// //Set<String> idSet = backendIMap().keySet(queryPredicate);
288+
// //String[] idArr = idSet.toArray(new String[0]);
289+
290+
// // DataObject[] from idArr
291+
// //DataObject[] doArr = getArrayFromID(idArr, true);
292+
293+
// // Converts to a list
294+
// //retList = new ArrayList(Arrays.asList(doArr));
295+
// retList = new ArrayList<String>(myRedisMap.readAllKeySet());
296+
// }
297+
298+
// // Sort, offset, convert to array, and return
299+
// // ???
300+
301+
// // Prepare the actual return string array
302+
// int retLength = retList.size();
303+
// String[] ret = new String[retLength];
304+
// for (int a = 0; a < retLength; ++a) {
305+
// //._oid(); -> where is it coming from
306+
// //ret[a] = retList.get(a)._oid();
307+
// ret[a] = String.valueOf(retList.get(a));
308+
// }
309+
310+
// System.out.println(Arrays.toString(ret));
311+
// // Returns sorted array of strings
312+
// return ret;
313+
// }
292314

293315
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.redisson.api.RBucket;
2020
import org.redisson.api.RMap;
2121

22+
import org.redisson.client.codec.StringCodec;
23+
2224
import org.redisson.api.RAtomicLong;
2325

2426
import org.apache.commons.lang3.RandomUtils;
@@ -53,12 +55,6 @@ public void stackTest() {
5355
redisson = instance.getConnection();
5456
assertNotNull(redisson);
5557

56-
//Test that redisson client works properly because i'm paranoid
57-
RBucket<String> bucket = redisson.getBucket("stringObject");
58-
bucket.set("hello world");
59-
String objValue = bucket.get();
60-
assertEquals(objValue, "hello world");
61-
6258
assertNotNull(instance.dataObjectMap(DStackTestConfig.randomTablePrefix()));
6359

6460
//Clear current db of from all keys

0 commit comments

Comments
 (0)