Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@gsharma gsharma authored Jonathan Leibiusky committed
View
37 src/main/java/redis/clients/johm/JOhm.java
@@ -254,17 +254,47 @@ public void execute() throws JedisException {
* @return
*/
public static boolean delete(Class<?> clazz, int id) {
- return delete(clazz, id, false);
+ return delete(clazz, id, true, false);
}
@SuppressWarnings("unchecked")
- public static boolean delete(Class<?> clazz, int id, boolean deleteChildren) {
+ public static boolean delete(Class<?> clazz, int id, boolean deleteIndexes,
+ boolean deleteChildren) {
JOhmUtils.Validator.checkValidModelClazz(clazz);
boolean deleted = false;
Object persistedModel = get(clazz, id);
if (persistedModel != null) {
Nest nest = new Nest(persistedModel);
nest.setJedisPool(jedisPool);
+ if (deleteIndexes) {
+ // think about promoting deleteChildren as default behavior so
+ // that this field lookup gets folded into that
+ // if-deleteChildren block
+ List<Field> fields = new ArrayList<Field>();
+ fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
+ fields.addAll(Arrays.asList(clazz.getSuperclass()
+ .getDeclaredFields()));
+ for (Field field : fields) {
+ if (field.isAnnotationPresent(Indexed.class)) {
+ field.setAccessible(true);
+ Object fieldValue = null;
+ try {
+ fieldValue = field.get(persistedModel);
+ } catch (IllegalArgumentException e) {
+ throw new JOhmException(e);
+ } catch (IllegalAccessException e) {
+ throw new JOhmException(e);
+ }
+ if (fieldValue != null
+ && field.isAnnotationPresent(Reference.class)) {
+ fieldValue = JOhmUtils.getId(fieldValue);
+ }
+ if (!JOhmUtils.isNullOrEmpty(fieldValue)) {
+ nest.cat(field.getName()).cat(fieldValue).del();
+ }
+ }
+ }
+ }
if (deleteChildren) {
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
@@ -277,7 +307,8 @@ public static boolean delete(Class<?> clazz, int id, boolean deleteChildren) {
Object child = field.get(persistedModel);
if (child != null) {
delete(child.getClass(),
- JOhmUtils.getId(child), deleteChildren); // children
+ JOhmUtils.getId(child), deleteIndexes,
+ deleteChildren); // children
}
} catch (IllegalArgumentException e) {
throw new JOhmException(e);
View
2  src/test/java/redis/clients/johm/BasicPersistanceTest.java
@@ -60,7 +60,7 @@ public void saveWithArray() {
assertEquals(item2.getId(), saved[2].getId());
assertEquals(item2.getName(), saved[2].getName());
- assertTrue(JOhm.delete(User.class, savedUser.getId(), true));
+ assertTrue(JOhm.delete(User.class, savedUser.getId(), true, true));
assertTrue(JOhm.delete(Item.class, item0.getId()));
assertTrue(JOhm.delete(Item.class, item1.getId()));
assertTrue(JOhm.delete(Item.class, item2.getId()));
View
35 src/test/java/redis/clients/johm/SearchTest.java
@@ -254,4 +254,39 @@ public void canSearchOnReferences() {
assertEquals(user1.getId(), users.get(0).getId());
assertEquals(user2.getId(), users.get(1).getId());
}
+
+ @Test
+ public void cannotSearchAfterDeletingIndexes() {
+ User user = new User();
+ user.setAge(88);
+ JOhm.save(user);
+
+ user.setAge(77); // younger
+ JOhm.save(user);
+
+ user.setAge(66); // younger still
+ JOhm.save(user);
+
+ int id = user.getId();
+
+ assertNotNull(JOhm.get(User.class, id));
+
+ List<User> users = JOhm.find(User.class, "age", 88);
+ assertEquals(1, users.size());
+ users = JOhm.find(User.class, "age", 77);
+ assertEquals(1, users.size());
+ users = JOhm.find(User.class, "age", 66);
+ assertEquals(1, users.size());
+
+ JOhm.delete(User.class, id);
+
+ users = JOhm.find(User.class, "age", 88);
+ assertEquals(0, users.size());
+ users = JOhm.find(User.class, "age", 77);
+ assertEquals(0, users.size());
+ users = JOhm.find(User.class, "age", 66);
+ assertEquals(0, users.size());
+
+ assertNull(JOhm.get(User.class, id));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.