Skip to content

Commit

Permalink
Add getAll
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Leibiusky committed Nov 22, 2010
1 parent 7ac333c commit 166bd55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/redis/clients/johm/JOhm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -147,7 +148,7 @@ public static <T> List<T> find(Class<?> clazz, String attributeName,
* @return
*/
public static <T> T save(final Object model) {
return JOhm.<T>save(model, false);
return JOhm.<T> save(model, false);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -218,6 +219,8 @@ public static <T> T save(final Object model, boolean saveChildren) {
String.valueOf(JOhmUtils.getId(model)));
}
}
// always add to the all set, to support getAll
nest.cat("all").sadd(String.valueOf(JOhmUtils.getId(model)));
}
} catch (IllegalArgumentException e) {
throw new JOhmException(e);
Expand Down Expand Up @@ -355,4 +358,23 @@ private static Nest initIfNeeded(final Object model) {
return nest;
}

@SuppressWarnings("unchecked")
public static <T> Set<T> getAll(Class<?> clazz) {
JOhmUtils.Validator.checkValidModelClazz(clazz);
Set<Object> results = null;
Nest nest = new Nest(clazz);
nest.setJedisPool(jedisPool);
Set<String> modelIdStrings = nest.cat("all").smembers();
if (modelIdStrings != null) {
results = new HashSet<Object>();
Object indexed = null;
for (String modelIdString : modelIdStrings) {
indexed = get(clazz, Integer.parseInt(modelIdString));
if (indexed != null) {
results.add(indexed);
}
}
}
return (Set<T>) results;
}
}
15 changes: 15 additions & 0 deletions src/test/java/redis/clients/johm/BasicPersistanceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package redis.clients.johm;

import java.util.Set;

import org.junit.Test;

import redis.clients.johm.models.Country;
Expand Down Expand Up @@ -196,4 +198,17 @@ public void shouldHandleReferences() {
assertEquals(somewhere.getId(), savedUser.getCountry().getId());
assertEquals(somewhere.getName(), savedUser.getCountry().getName());
}

@Test
public void getAll() {
User user = new User();
user.setName("foo");
JOhm.save(user);
user = new User();
user.setName("foo1");
JOhm.save(user);

Set<User> users = JOhm.getAll(User.class);
assertEquals(2, users.size());
}
}

0 comments on commit 166bd55

Please sign in to comment.