forked from xetorthio/johm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan Leibiusky
committed
Sep 29, 2010
0 parents
commit c19dc40
Showing
12 changed files
with
449 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>redis</groupId> | ||
<artifactId>johm</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.8.1</version> | ||
<type>jar</type> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package redis.clients.johm; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Attribute { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package redis.clients.johm; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import redis.clients.jedis.JedisPool; | ||
|
||
public class JOhm { | ||
private static JedisPool jedisPool; | ||
private static Nest nest; | ||
|
||
public JOhm() { | ||
} | ||
|
||
public static void setPool(JedisPool jedisPool) { | ||
JOhm.jedisPool = jedisPool; | ||
} | ||
|
||
public static JedisPool getPool() { | ||
return jedisPool; | ||
} | ||
|
||
protected static Nest getNest() { | ||
if (nest == null) { | ||
nest = new Nest(jedisPool); | ||
} | ||
return nest; | ||
} | ||
|
||
public static <T extends Model> T get(Class<? extends Model> clazz, int id) { | ||
Map<String, String> hashedObject = getNest().cat(clazz.getSimpleName()) | ||
.cat(id).hgetAll(); | ||
Model newInstance; | ||
Map<String, Field> fields = new HashMap<String, Field>(); | ||
for (Field field : clazz.getDeclaredFields()) { | ||
fields.put(field.getName(), field); | ||
} | ||
for (Field field : clazz.getSuperclass().getDeclaredFields()) { | ||
fields.put(field.getName(), field); | ||
} | ||
|
||
try { | ||
newInstance = clazz.newInstance(); | ||
Iterator<String> iterator = hashedObject.keySet().iterator(); | ||
while (iterator.hasNext()) { | ||
String fieldName = iterator.next(); | ||
Field field = fields.get(fieldName); | ||
field.setAccessible(true); | ||
field.set(newInstance, convert(field, hashedObject | ||
.get(fieldName))); | ||
} | ||
return (T) newInstance; | ||
} catch (InstantiationException e) { | ||
throw new JOhmException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new JOhmException(e); | ||
} catch (SecurityException e) { | ||
throw new JOhmException(e); | ||
} | ||
} | ||
|
||
private static Object convert(Field field, String value) { | ||
if (field.getType().equals(Integer.class)) { | ||
return new Integer(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package redis.clients.johm; | ||
|
||
|
||
public class JOhmException extends RuntimeException { | ||
|
||
public JOhmException(Exception e) { | ||
super(e); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 5824673432789607128L; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package redis.clients.johm; | ||
|
||
public class MissingIdException extends RuntimeException { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 431576167757996845L; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package redis.clients.johm; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import redis.clients.jedis.JedisException; | ||
import redis.clients.jedis.TransactionBlock; | ||
|
||
public class Model extends JOhm { | ||
@Attribute | ||
private Integer id = null; | ||
|
||
private Integer initializeId() { | ||
return nest().cat("id").incr(); | ||
} | ||
|
||
public <T extends Model> T save() { | ||
try { | ||
final Map<String, String> hashedObject = new HashMap<String, String>(); | ||
|
||
if (this.id == null) { | ||
this.id = initializeId(); | ||
} | ||
List<Field> fields = new ArrayList<Field>(); | ||
fields.addAll(Arrays.asList(this.getClass().getDeclaredFields())); | ||
fields.addAll(Arrays.asList(this.getClass().getSuperclass() | ||
.getDeclaredFields())); | ||
|
||
for (Field field : fields) { | ||
if (field.getAnnotation(Attribute.class) != null) { | ||
field.setAccessible(true); | ||
hashedObject.put(field.getName(), field.get(this) | ||
.toString()); | ||
} | ||
} | ||
nest().multi(new TransactionBlock() { | ||
public void execute() throws JedisException { | ||
nest().cat(getId()).del(); | ||
nest().cat(getId()).hmset(hashedObject); | ||
} | ||
}); | ||
return (T) this; | ||
} catch (IllegalArgumentException e) { | ||
throw new JOhmException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new JOhmException(e); | ||
} | ||
} | ||
|
||
public int getId() { | ||
if (id == null) { | ||
throw new MissingIdException(); | ||
} | ||
return id; | ||
} | ||
|
||
private Nest nest() { | ||
return getNest().cat(this.getClass().getSimpleName()); | ||
} | ||
|
||
public String key() { | ||
return nest().cat(getId()).key(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package redis.clients.johm; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.TransactionBlock; | ||
|
||
public class Nest { | ||
private static final String COLON = ":"; | ||
private StringBuilder sb; | ||
private String key; | ||
private JedisPool jedisPool; | ||
|
||
public Nest(JedisPool jedis) { | ||
this.key = ""; | ||
this.jedisPool = jedis; | ||
} | ||
|
||
public Nest(String key, JedisPool jedis) { | ||
this.key = key; | ||
this.jedisPool = jedis; | ||
} | ||
|
||
public String key() { | ||
if (sb == null) { | ||
sb = new StringBuilder(); | ||
sb.append(key); | ||
} | ||
String generatedKey = sb.toString(); | ||
sb = null; | ||
return generatedKey; | ||
} | ||
|
||
public Nest cat(int id) { | ||
if (sb == null) { | ||
sb = new StringBuilder(); | ||
sb.append(key); | ||
} | ||
sb.append(COLON); | ||
sb.append(id); | ||
return this; | ||
} | ||
|
||
public Nest cat(String field) { | ||
if (sb == null) { | ||
sb = new StringBuilder(); | ||
sb.append(key); | ||
} | ||
sb.append(COLON); | ||
sb.append(field); | ||
return this; | ||
} | ||
|
||
public String set(String value) { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
String set = jedis.set(key(), value); | ||
jedisPool.returnResource(jedis); | ||
return set; | ||
} | ||
|
||
public String get() { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
String string = jedis.get(key()); | ||
jedisPool.returnResource(jedis); | ||
return string; | ||
} | ||
|
||
public Integer incr() { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
Integer incr = jedis.incr(key()); | ||
jedisPool.returnResource(jedis); | ||
|
||
return incr; | ||
} | ||
|
||
public String hmset(Map<String, String> hash) { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
String hmset = jedis.hmset(key(), hash); | ||
jedisPool.returnResource(jedis); | ||
return hmset; | ||
} | ||
|
||
public List<Object> multi(TransactionBlock transaction) { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
List<Object> multi = jedis.multi(transaction); | ||
jedisPool.returnResource(jedis); | ||
return multi; | ||
} | ||
|
||
public Integer del() { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
Integer del = jedis.del(key()); | ||
jedisPool.returnResource(jedis); | ||
return del; | ||
} | ||
|
||
public Map<String, String> hgetAll() { | ||
Jedis jedis; | ||
try { | ||
jedis = jedisPool.getResource(); | ||
} catch (TimeoutException e) { | ||
throw new JOhmException(e); | ||
} | ||
Map<String, String> hgetAll = jedis.hgetAll(key()); | ||
jedisPool.returnResource(jedis); | ||
return hgetAll; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package redis.clients.johm; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
public class JOhmTestBase extends Assert { | ||
protected static JedisPool jedisPool; | ||
|
||
@BeforeClass | ||
public static void before() { | ||
jedisPool = new JedisPool("localhost"); | ||
jedisPool.init(); | ||
JOhm.setPool(jedisPool); | ||
} | ||
|
||
@Before | ||
public void startUp() throws TimeoutException { | ||
Jedis jedis = jedisPool.getResource(); | ||
jedis.flushAll(); | ||
jedisPool.returnResource(jedis); | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
jedisPool.destroy(); | ||
} | ||
} |
Oops, something went wrong.