Permalink
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...
gsharma authored and Jonathan Leibiusky committed Dec 15, 2010
1 parent 978696e commit 051ec190ca532761433a6abfc9b91d946ff9135a
@@ -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);
@@ -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()));
@@ -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));
+ }
}

0 comments on commit 051ec19

Please sign in to comment.