Add intersection for Set operations

This commit is contained in:
Mark Pollack
2010-10-25 16:09:23 -04:00
parent 477093824f
commit bb6f5e269f
11 changed files with 212 additions and 17 deletions

17
.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-datastore-keyvalue-dist</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,9 @@
#Mon Oct 11 17:02:20 EDT 2010
activeProfiles=
eclipse.preferences.version=1
fullBuildGoals=process-test-resources
includeModules=false
resolveWorkspaceProjects=true
resourceFilterGoals=process-resources resources\:testResources
skipCompilerPlugin=true
version=1

View File

@@ -1,10 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-datastore-keyvalue-parent</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,9 @@
#Mon Oct 11 17:02:20 EDT 2010
activeProfiles=
eclipse.preferences.version=1
fullBuildGoals=process-test-resources
includeModules=false
resolveWorkspaceProjects=true
resourceFilterGoals=process-resources resources\:testResources
skipCompilerPlugin=true
version=1

View File

@@ -0,0 +1,93 @@
package org.springframework.datastore.redis.core;
import java.util.Set;
public class DefaultSetOperations implements SetOperations {
private RedisTemplate redisTemplate;
public DefaultSetOperations(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public boolean add(final String key, final String member) {
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisClient redisClient) throws Exception {
return (redisClient.sadd(key, member) == 0) ? true : false;
}
});
}
public Set<String> getAll(final String key) {
return redisTemplate.execute(new RedisCallback<Set<String>>() {
public Set<String> doInRedis(RedisClient redisClient) throws Exception {
return redisClient.smembers(key);
}
});
}
public boolean remove(String key, String member) {
// TODO Auto-generated method stub
return false;
}
public boolean removeRandom(String key) {
// TODO Auto-generated method stub
return false;
}
public boolean moveBetweenSets(String srckey, String dstkey, String member) {
// TODO Auto-generated method stub
return false;
}
public int size(String key) {
// TODO Auto-generated method stub
return 0;
}
public boolean contains(String key, String member) {
// TODO Auto-generated method stub
return false;
}
public Set<String> getIntersectionOfSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeIntersectionOfSets(final String dstkey, final String... keys) {
redisTemplate.execute(new RedisCallback<Void>() {
public Void doInRedis(RedisClient redisClient) throws Exception {
redisClient.sinterstore(dstkey, keys);
return null;
}
});
}
public Set<String> getUnionOfSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeUnionOfSets(String dstkey, String... keys) {
// TODO Auto-generated method stub
}
public Set<String> getDifferenceBetweenSets(String... keys) {
// TODO Auto-generated method stub
return null;
}
public void storeDifferenceBetweenSets(String dstkey, String... keys) {
// TODO Auto-generated method stub
}
public String getRandom(String key) {
// TODO Auto-generated method stub
return null;
}
}

View File

@@ -281,8 +281,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperations {
}
public SetOperations getSetOperations() {
// TODO Auto-generated method stub
return null;
return new DefaultSetOperations(this);
}

View File

@@ -15,7 +15,7 @@ public class RedisSet extends AbstractRedisCollection implements Set {
public RedisSet(RedisTemplate redisTemplate, String redisKey) {
super(redisTemplate, redisKey);
}
public int size() {
return redisTemplate.getSetOperations().size(redisKey);
}
@@ -58,11 +58,29 @@ public class RedisSet extends AbstractRedisCollection implements Set {
return redisTemplate.getSetOperations().removeRandom(redisKey);
}
void intersection(RedisSet... redisSets) {
/*
public intersection(RedisSet... redisSets) {
//storeIntersectionOfSets..
return null;
}
*/
public RedisSet intersection(String newKey, RedisSet... redisSets) {
String[] keys = new String[redisSets.length];
int i = 0;
for (RedisSet redisSet : redisSets) {
keys[i] = redisSet.getRedisKey();
i++;
}
redisTemplate.getSetOperations().storeIntersectionOfSets(newKey, keys);
RedisSet resultSet = new RedisSet(redisTemplate, newKey);
Set<String> results = redisTemplate.getSetOperations().getAll(newKey);
resultSet.addAll(results);
return resultSet;
}
void union(RedisSet... redisSets) {
//storeUnionOfSets
}

View File

@@ -1,5 +1,7 @@
package org.springframework.datastore.redis.util;
import java.util.Set;
import org.springframework.datastore.redis.core.RedisTemplate;
public class Sets {

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
@@ -102,4 +103,6 @@ public abstract class AbstractClientIntegrationTests {
Person p = new Person("Joe", "Trader", 33);
}
}

View File

@@ -16,21 +16,52 @@
package org.springframework.datastore.redis.core.jedis;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.springframework.datastore.redis.core.AbstractClientIntegrationTests;
import org.springframework.datastore.redis.core.RedisClientFactory;
import org.springframework.datastore.redis.core.RedisTemplate;
import org.springframework.datastore.redis.util.RedisSet;
public class JedisRedisClientIntegrationTests extends
AbstractClientIntegrationTests {
RedisClientFactory clientFactory;
@Before
public void setUp() {
RedisClientFactory clientFactory = new JedisClientFactory();
clientFactory = new JedisClientFactory();
clientFactory.setPassword("foobared");
client = clientFactory.createClient();
client.flushAll();
}
@Test
public void setAdd() {
client.sadd("s1", "1");
client.sadd("s1", "2");
client.sadd("s1", "3");
client.sadd("s2", "2");
client.sadd("s2", "3");
Set<String> intersection = client.sinter("s1", "s2");
System.out.println(intersection);
}
@Test
public void setIntersectionTests() {
RedisTemplate template = new RedisTemplate(clientFactory);
RedisSet s1 = new RedisSet(template, "s1");
s1.add("1"); s1.add("2"); s1.add("3");
RedisSet s2 = new RedisSet(template, "s2");
s2.add("2"); s2.add("3");
Set s3 = s1.intersection("s3", s1, s2);
for (Object object : s3) {
System.out.println(object);
}
}
}