refactoring

pull/968/head
Nikita 8 years ago
parent 369e30640c
commit b5f2827413

@ -0,0 +1,63 @@
/**
* Copyright 2016 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import java.util.concurrent.ExecutorService;
import org.redisson.api.RFuture;
import org.redisson.command.CommandAsyncExecutor;
import io.netty.util.concurrent.Future;
/**
*
* @author Nikita Koksharov
*
* @param <R> result type
*/
public abstract class MapWriterExecutorPromise<R> extends MapWriterPromise<R> {
public MapWriterExecutorPromise(RFuture<R> f, CommandAsyncExecutor commandExecutor) {
super(f, commandExecutor);
}
@Override
public void execute(final Future<R> future, ExecutorService executorService) {
if (condition(future)) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
executeWriter();
} catch (Exception e) {
tryFailure(e);
return;
}
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
}
protected boolean condition(Future<R> future) {
return true;
}
protected abstract void executeWriter();
}

@ -0,0 +1,52 @@
/**
* Copyright 2016 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import java.util.concurrent.ExecutorService;
import org.redisson.api.RFuture;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.RedissonPromise;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
/**
*
* @author Nikita Koksharov
*
* @param <R> result type
*/
public abstract class MapWriterPromise<R> extends RedissonPromise<R> {
public MapWriterPromise(RFuture<R> f, final CommandAsyncExecutor commandExecutor) {
super();
f.addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (!future.isSuccess()) {
tryFailure(future.cause());
return;
}
execute(future, commandExecutor.getConnectionManager().getExecutor());
}
});
}
public abstract void execute(Future<R> future, ExecutorService executorService);
}

@ -29,7 +29,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import org.redisson.api.RFuture;
@ -263,16 +262,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<Void> result = new MapWriterPromise<Void>(future, commandExecutor) {
RPromise<Void> result = new MapWriterExecutorPromise<Void>(future, commandExecutor) {
@Override
public void execute(Future<Void> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
@Override
public void run() {
public void executeWriter() {
mapWriter.writeAll((Map<K, V>) map);
trySuccess(null);
}
});
}
};
return result;
@ -373,23 +366,17 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
if (future.getNow() == null) {
commandExecutor.getConnectionManager().getExecutor().execute(new Runnable() {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<V> future) {
return future.getNow() == null;
}
};
return result;
}
@ -418,20 +405,15 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
if (future.getNow()) {
commandExecutor.getConnectionManager().getExecutor().execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<Boolean> future) {
return future.getNow();
}
};
@ -457,20 +439,15 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
if (future.getNow()) {
executorService.execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
protected void executeWriter() {
mapWriter.delete((K) key);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<Boolean> future) {
return future.getNow();
}
};
@ -514,20 +491,15 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
if (future.getNow()) {
executorService.execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, newValue);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<Boolean> future) {
return future.getNow();
}
};
@ -560,20 +532,15 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
if (future.getNow() != null) {
executorService.execute(new Runnable() {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<V> future) {
return future.getNow() != null;
}
};
@ -777,16 +744,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void run() {
public void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
}
};
@ -811,16 +772,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
@Override
public void run() {
public void executeWriter() {
mapWriter.delete(key);
trySuccess(future.getNow());
}
});
}
};
@ -845,16 +800,10 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
public void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
}
};
@ -961,21 +910,15 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
checkKey(key);
checkValue(value);
RFuture<V> future = addAndGetOperationAsync(key, value);
final RFuture<V> future = addAndGetOperationAsync(key, value);
if (mapWriter == null) {
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void run() {
public void executeWriter() {
mapWriter.write(key, future.getNow());
trySuccess(future.getNow());
}
});
}
};

@ -320,20 +320,15 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
if (future.getNow() == null) {
commandExecutor.getConnectionManager().getExecutor().execute(new Runnable() {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<V> future) {
return future.getNow() == null;
}
};
@ -593,16 +588,10 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
public void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
}
};
return result;
@ -708,16 +697,10 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
return future;
}
RPromise<V> result = new MapWriterPromise<V>(future, commandExecutor) {
RPromise<V> result = new MapWriterExecutorPromise<V>(future, commandExecutor) {
@Override
public void execute(final Future<V> future, ExecutorService executorService) {
executorService.execute(new Runnable() {
@Override
public void run() {
public void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
}
};
@ -1143,20 +1126,15 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
return future;
}
RPromise<Boolean> result = new MapWriterPromise<Boolean>(future, commandExecutor) {
@Override
public void execute(final Future<Boolean> future, ExecutorService executorService) {
if (future.getNow()) {
commandExecutor.getConnectionManager().getExecutor().execute(new Runnable() {
RPromise<Boolean> result = new MapWriterExecutorPromise<Boolean>(future, commandExecutor) {
@Override
public void run() {
protected void executeWriter() {
mapWriter.write(key, value);
trySuccess(future.getNow());
}
});
} else {
trySuccess(future.getNow());
}
@Override
protected boolean condition(Future<Boolean> future) {
return future.getNow();
}
};

Loading…
Cancel
Save