Skip to content
This repository
Browse code

JOhm.delete() should provide an option to delete the associated index…

…es as well.

This delivery takes care of the aforementioned requirement.
  • Loading branch information...
commit 051ec190ca532761433a6abfc9b91d946ff9135a 1 parent 978696e
Gaurav Sharma gsharma authored Jonathan Leibiusky committed
37 src/main/java/redis/clients/johm/JOhm.java
@@ -254,17 +254,47 @@ public void execute() throws JedisException {
254 254 * @return
255 255 */
256 256 public static boolean delete(Class<?> clazz, int id) {
257   - return delete(clazz, id, false);
  257 + return delete(clazz, id, true, false);
258 258 }
259 259
260 260 @SuppressWarnings("unchecked")
261   - public static boolean delete(Class<?> clazz, int id, boolean deleteChildren) {
  261 + public static boolean delete(Class<?> clazz, int id, boolean deleteIndexes,
  262 + boolean deleteChildren) {
262 263 JOhmUtils.Validator.checkValidModelClazz(clazz);
263 264 boolean deleted = false;
264 265 Object persistedModel = get(clazz, id);
265 266 if (persistedModel != null) {
266 267 Nest nest = new Nest(persistedModel);
267 268 nest.setJedisPool(jedisPool);
  269 + if (deleteIndexes) {
  270 + // think about promoting deleteChildren as default behavior so
  271 + // that this field lookup gets folded into that
  272 + // if-deleteChildren block
  273 + List<Field> fields = new ArrayList<Field>();
  274 + fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
  275 + fields.addAll(Arrays.asList(clazz.getSuperclass()
  276 + .getDeclaredFields()));
  277 + for (Field field : fields) {
  278 + if (field.isAnnotationPresent(Indexed.class)) {
  279 + field.setAccessible(true);
  280 + Object fieldValue = null;
  281 + try {
  282 + fieldValue = field.get(persistedModel);
  283 + } catch (IllegalArgumentException e) {
  284 + throw new JOhmException(e);
  285 + } catch (IllegalAccessException e) {
  286 + throw new JOhmException(e);
  287 + }
  288 + if (fieldValue != null
  289 + && field.isAnnotationPresent(Reference.class)) {
  290 + fieldValue = JOhmUtils.getId(fieldValue);
  291 + }
  292 + if (!JOhmUtils.isNullOrEmpty(fieldValue)) {
  293 + nest.cat(field.getName()).cat(fieldValue).del();
  294 + }
  295 + }
  296 + }
  297 + }
268 298 if (deleteChildren) {
269 299 List<Field> fields = new ArrayList<Field>();
270 300 fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
@@ -277,7 +307,8 @@ public static boolean delete(Class<?> clazz, int id, boolean deleteChildren) {
277 307 Object child = field.get(persistedModel);
278 308 if (child != null) {
279 309 delete(child.getClass(),
280   - JOhmUtils.getId(child), deleteChildren); // children
  310 + JOhmUtils.getId(child), deleteIndexes,
  311 + deleteChildren); // children
281 312 }
282 313 } catch (IllegalArgumentException e) {
283 314 throw new JOhmException(e);
2  src/test/java/redis/clients/johm/BasicPersistanceTest.java
@@ -60,7 +60,7 @@ public void saveWithArray() {
60 60 assertEquals(item2.getId(), saved[2].getId());
61 61 assertEquals(item2.getName(), saved[2].getName());
62 62
63   - assertTrue(JOhm.delete(User.class, savedUser.getId(), true));
  63 + assertTrue(JOhm.delete(User.class, savedUser.getId(), true, true));
64 64 assertTrue(JOhm.delete(Item.class, item0.getId()));
65 65 assertTrue(JOhm.delete(Item.class, item1.getId()));
66 66 assertTrue(JOhm.delete(Item.class, item2.getId()));
35 src/test/java/redis/clients/johm/SearchTest.java
@@ -254,4 +254,39 @@ public void canSearchOnReferences() {
254 254 assertEquals(user1.getId(), users.get(0).getId());
255 255 assertEquals(user2.getId(), users.get(1).getId());
256 256 }
  257 +
  258 + @Test
  259 + public void cannotSearchAfterDeletingIndexes() {
  260 + User user = new User();
  261 + user.setAge(88);
  262 + JOhm.save(user);
  263 +
  264 + user.setAge(77); // younger
  265 + JOhm.save(user);
  266 +
  267 + user.setAge(66); // younger still
  268 + JOhm.save(user);
  269 +
  270 + int id = user.getId();
  271 +
  272 + assertNotNull(JOhm.get(User.class, id));
  273 +
  274 + List<User> users = JOhm.find(User.class, "age", 88);
  275 + assertEquals(1, users.size());
  276 + users = JOhm.find(User.class, "age", 77);
  277 + assertEquals(1, users.size());
  278 + users = JOhm.find(User.class, "age", 66);
  279 + assertEquals(1, users.size());
  280 +
  281 + JOhm.delete(User.class, id);
  282 +
  283 + users = JOhm.find(User.class, "age", 88);
  284 + assertEquals(0, users.size());
  285 + users = JOhm.find(User.class, "age", 77);
  286 + assertEquals(0, users.size());
  287 + users = JOhm.find(User.class, "age", 66);
  288 + assertEquals(0, users.size());
  289 +
  290 + assertNull(JOhm.get(User.class, id));
  291 + }
257 292 }

0 comments on commit 051ec19

Please sign in to comment.
Something went wrong with that request. Please try again.