Merge pull request #549 from HeartSaVioR/make_multi_in_pipeline_and_sync_work_smoothly
Fix multi in Pipeline and sync() - JedisDataException (fixes #547)
This commit is contained in:
@@ -31,6 +31,12 @@ public class Pipeline extends MultiKeyPipelineBase {
|
|||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setResponseDependency(Response<?> dependency) {
|
||||||
|
for (Response<?> response : responses) {
|
||||||
|
response.setDependency(dependency);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void addResponse(Response<?> response) {
|
public void addResponse(Response<?> response) {
|
||||||
responses.add(response);
|
responses.add(response);
|
||||||
}
|
}
|
||||||
@@ -106,6 +112,7 @@ public class Pipeline extends MultiKeyPipelineBase {
|
|||||||
public Response<List<Object>> exec() {
|
public Response<List<Object>> exec() {
|
||||||
client.exec();
|
client.exec();
|
||||||
Response<List<Object>> response = super.getResponse(currentMulti);
|
Response<List<Object>> response = super.getResponse(currentMulti);
|
||||||
|
currentMulti.setResponseDependency(response);
|
||||||
currentMulti = null;
|
currentMulti = null;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ public class Response<T> {
|
|||||||
private boolean set = false;
|
private boolean set = false;
|
||||||
private Builder<T> builder;
|
private Builder<T> builder;
|
||||||
private Object data;
|
private Object data;
|
||||||
|
private Response<?> dependency = null;
|
||||||
|
private boolean requestDependencyBuild = false;
|
||||||
|
|
||||||
public Response(Builder<T> b) {
|
public Response(Builder<T> b) {
|
||||||
this.builder = b;
|
this.builder = b;
|
||||||
@@ -19,22 +21,38 @@ public class Response<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public T get() {
|
public T get() {
|
||||||
|
// if response has dependency response and dependency is not built,
|
||||||
|
// build it first and no more!!
|
||||||
|
if (!requestDependencyBuild && dependency != null && dependency.set
|
||||||
|
&& !dependency.built) {
|
||||||
|
requestDependencyBuild = true;
|
||||||
|
dependency.build();
|
||||||
|
}
|
||||||
if (!set) {
|
if (!set) {
|
||||||
throw new JedisDataException(
|
throw new JedisDataException(
|
||||||
"Please close pipeline or multi block before calling this method.");
|
"Please close pipeline or multi block before calling this method.");
|
||||||
}
|
}
|
||||||
if (!built) {
|
if (!built) {
|
||||||
|
build();
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDependency(Response<?> dependency) {
|
||||||
|
this.dependency = dependency;
|
||||||
|
this.requestDependencyBuild = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void build() {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
if (data instanceof JedisDataException) {
|
if (data instanceof JedisDataException) {
|
||||||
throw new JedisDataException((JedisDataException) data);
|
throw new JedisDataException((JedisDataException) data);
|
||||||
}
|
}
|
||||||
response = builder.build(data);
|
response = builder.build(data);
|
||||||
}
|
}
|
||||||
this.data = null;
|
data = null;
|
||||||
built = true;
|
built = true;
|
||||||
}
|
}
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Response " + builder.toString();
|
return "Response " + builder.toString();
|
||||||
|
|||||||
@@ -251,6 +251,27 @@ public class PipeliningTest extends Assert {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multiWithSync() {
|
||||||
|
jedis.set("foo", "314");
|
||||||
|
jedis.set("bar", "foo");
|
||||||
|
jedis.set("hello", "world");
|
||||||
|
Pipeline p = jedis.pipelined();
|
||||||
|
Response<String> r1 = p.get("bar");
|
||||||
|
p.multi();
|
||||||
|
Response<String> r2 = p.get("foo");
|
||||||
|
p.exec();
|
||||||
|
Response<String> r3 = p.get("hello");
|
||||||
|
p.sync();
|
||||||
|
|
||||||
|
// before multi
|
||||||
|
assertEquals("foo", r1.get());
|
||||||
|
// It should be readable whether exec's response was built or not
|
||||||
|
assertEquals("314", r2.get());
|
||||||
|
// after multi
|
||||||
|
assertEquals("world", r3.get());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDiscardInPipeline() {
|
public void testDiscardInPipeline() {
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
|
|||||||
Reference in New Issue
Block a user