Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Sep 1, 2017
1 parent da3c5df commit 98aa72d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 55 deletions.
25 changes: 15 additions & 10 deletions redisson/src/main/java/org/redisson/RedissonBloomFilter.java
Expand Up @@ -76,13 +76,21 @@ private long optimalNumOfBits(long n, double p) {
}
return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2)));
}

private long[] hash(Object object) {
ByteBuf state = encode(object);
try {
long hash1 = LongHashFunction.xx().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
long hash2 = LongHashFunction.farmUo().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
return new long[] {hash1, hash2};
} finally {
state.release();
}
}

@Override
public boolean add(T object) {
ByteBuf state = encode(object);
long hash1 = LongHashFunction.xx().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
long hash2 = LongHashFunction.farmUo().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
state.release();
long[] hashes = hash(object);

while (true) {
if (size == 0) {
Expand All @@ -92,7 +100,7 @@ public boolean add(T object) {
int hashIterations = this.hashIterations;
long size = this.size;

long[] indexes = hash(hash1, hash2, hashIterations, size);
long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);

CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
addConfigCheck(hashIterations, size, executorService);
Expand Down Expand Up @@ -132,10 +140,7 @@ private long[] hash(long hash1, long hash2, int iterations, long size) {

@Override
public boolean contains(T object) {
ByteBuf state = encode(object);
long hash1 = LongHashFunction.xx().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
long hash2 = LongHashFunction.farmUo().hashBytes(state.internalNioBuffer(state.readerIndex(), state.readableBytes()));
state.release();
long[] hashes = hash(object);

while (true) {
if (size == 0) {
Expand All @@ -145,7 +150,7 @@ public boolean contains(T object) {
int hashIterations = this.hashIterations;
long size = this.size;

long[] indexes = hash(hash1, hash2, hashIterations, size);
long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);

CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
addConfigCheck(hashIterations, size, executorService);
Expand Down
46 changes: 43 additions & 3 deletions redisson/src/main/java/org/redisson/RedissonTopic.java
Expand Up @@ -15,6 +15,7 @@
*/
package org.redisson;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

Expand All @@ -27,8 +28,15 @@
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.PubSubConnectionEntry;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonObjectFactory;
import org.redisson.misc.RedissonPromise;
import org.redisson.pubsub.AsyncSemaphore;

import io.netty.buffer.ByteBuf;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;

/**
* Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster.
*
Expand All @@ -42,11 +50,11 @@ public class RedissonTopic<M> implements RTopic<M> {
private final String name;
private final Codec codec;

protected RedissonTopic(CommandAsyncExecutor commandExecutor, String name) {
public RedissonTopic(CommandAsyncExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
}

protected RedissonTopic(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
public RedissonTopic(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor;
this.name = name;
this.codec = codec;
Expand All @@ -63,9 +71,24 @@ public long publish(M message) {

@Override
public RFuture<Long> publishAsync(M message) {
return commandExecutor.writeAsync(name, codec, RedisCommands.PUBLISH, name, message);
return commandExecutor.writeAsync(name, codec, RedisCommands.PUBLISH, name, encode(message));
}

protected ByteBuf encode(Object value) {
if (commandExecutor.isRedissonReferenceSupportEnabled()) {
RedissonReference reference = RedissonObjectFactory.toReference(commandExecutor.getConnectionManager().getCfg(), value);
if (reference != null) {
value = reference;
}
}

try {
return codec.getValueEncoder().encode(value);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}

@Override
public int addListener(StatusListener listener) {
return addListener(new PubSubStatusListener<Object>(listener, name));
Expand All @@ -82,6 +105,23 @@ private int addListener(RedisPubSubListener<?> pubSubListener) {
commandExecutor.syncSubscription(future);
return System.identityHashCode(pubSubListener);
}

public RFuture<Integer> addListenerAsync(final RedisPubSubListener<?> pubSubListener) {
RFuture<PubSubConnectionEntry> future = commandExecutor.getConnectionManager().subscribe(codec, name, pubSubListener);
RPromise<Integer> result = new RedissonPromise<Integer>();
future.addListener(new FutureListener<PubSubConnectionEntry>() {
@Override
public void operationComplete(Future<PubSubConnectionEntry> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}

result.trySuccess(System.identityHashCode(pubSubListener));
}
});
return result;
}

@Override
public void removeAllListeners() {
Expand Down
Expand Up @@ -302,7 +302,7 @@ public interface RedisCommands {
RedisStrictCommand<Boolean> MOVE = new RedisStrictCommand<Boolean>("MOVE", new BooleanReplayConvertor());
RedisStrictCommand<Void> MIGRATE = new RedisStrictCommand<Void>("MIGRATE", new VoidReplayConvertor());

RedisStrictCommand<Long> PUBLISH = new RedisStrictCommand<Long>("PUBLISH", 2);
RedisStrictCommand<Long> PUBLISH = new RedisStrictCommand<Long>("PUBLISH");

RedisCommand<Object> SUBSCRIBE = new RedisCommand<Object>("SUBSCRIBE", new PubSubStatusDecoder());
RedisCommand<Object> UNSUBSCRIBE = new RedisCommand<Object>("UNSUBSCRIBE", new PubSubStatusDecoder());
Expand Down
Expand Up @@ -17,7 +17,6 @@

import org.redisson.api.RFuture;
import org.redisson.client.RedisPubSubConnection;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.connection.ClientConnectionsEntry;
Expand Down
Expand Up @@ -21,20 +21,16 @@
import org.reactivestreams.Publisher;
import org.redisson.PubSubMessageListener;
import org.redisson.PubSubStatusListener;
import org.redisson.RedissonTopic;
import org.redisson.api.RFuture;
import org.redisson.api.RTopic;
import org.redisson.api.RTopicReactive;
import org.redisson.api.listener.MessageListener;
import org.redisson.api.listener.StatusListener;
import org.redisson.client.RedisPubSubListener;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandReactiveExecutor;
import org.redisson.connection.PubSubConnectionEntry;
import org.redisson.misc.RPromise;
import org.redisson.pubsub.AsyncSemaphore;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import reactor.fn.Supplier;

/**
Expand All @@ -46,18 +42,18 @@
*/
public class RedissonTopicReactive<M> implements RTopicReactive<M> {

private final RTopic<M> topic;
private final CommandReactiveExecutor commandExecutor;
private final String name;
private final Codec codec;

public RedissonTopicReactive(CommandReactiveExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
}

public RedissonTopicReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) {
this.topic = new RedissonTopic<M>(codec, commandExecutor, name);
this.commandExecutor = commandExecutor;
this.name = name;
this.codec = codec;
}

@Override
Expand All @@ -66,8 +62,13 @@ public List<String> getChannelNames() {
}

@Override
public Publisher<Long> publish(M message) {
return commandExecutor.writeReactive(name, codec, RedisCommands.PUBLISH, name, message);
public Publisher<Long> publish(final M message) {
return commandExecutor.reactive(new Supplier<RFuture<Long>>() {
@Override
public RFuture<Long> get() {
return topic.publishAsync(message);
}
});
}

@Override
Expand All @@ -82,45 +83,18 @@ public Publisher<Integer> addListener(MessageListener<M> listener) {
}

private Publisher<Integer> addListener(final RedisPubSubListener<?> pubSubListener) {
return new NettyFuturePublisher<Integer>(new Supplier<RFuture<Integer>>() {
return commandExecutor.reactive(new Supplier<RFuture<Integer>>() {
@Override
public RFuture<Integer> get() {
final RPromise<Integer> promise = commandExecutor.getConnectionManager().newPromise();
RFuture<PubSubConnectionEntry> future = commandExecutor.getConnectionManager().subscribe(codec, name, pubSubListener);
future.addListener(new FutureListener<PubSubConnectionEntry>() {
@Override
public void operationComplete(Future<PubSubConnectionEntry> future) throws Exception {
if (!future.isSuccess()) {
promise.tryFailure(future.cause());
return;
}

promise.trySuccess(System.identityHashCode(pubSubListener));
}
});
return promise;
return ((RedissonTopic<Integer>) topic).addListenerAsync(pubSubListener);
}
});
}


@Override
public void removeListener(int listenerId) {
AsyncSemaphore semaphore = commandExecutor.getConnectionManager().getSemaphore(name);
semaphore.acquireUninterruptibly();

PubSubConnectionEntry entry = commandExecutor.getConnectionManager().getPubSubEntry(name);
if (entry == null) {
semaphore.release();
return;
}

entry.removeListener(name, listenerId);
if (!entry.hasListeners(name)) {
commandExecutor.getConnectionManager().unsubscribe(name, semaphore);
} else {
semaphore.release();
}
topic.removeListener(listenerId);
}


Expand Down

0 comments on commit 98aa72d

Please sign in to comment.