Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Dec 28, 2017
1 parent 82fc18b commit 06c726b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 41 deletions.
21 changes: 13 additions & 8 deletions redisson/src/main/java/org/redisson/RedissonBitSet.java
Expand Up @@ -24,6 +24,7 @@
import org.redisson.api.RBitSet;
import org.redisson.api.RFuture;
import org.redisson.client.codec.ByteArrayCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandBatchService;
Expand Down Expand Up @@ -80,8 +81,12 @@ public void set(long bitIndex, boolean value) {
}

@Override
public RFuture<Void> setAsync(long bitIndex, boolean value) {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETBIT_VOID, getName(), bitIndex, value ? 1 : 0);
public RFuture<Boolean> setAsync(long bitIndex, boolean value) {
RedisCommand<Boolean> command = RedisCommands.SETBIT_TRUE;
if (!value) {
command = RedisCommands.SETBIT_FALSE;
}
return commandExecutor.writeAsync(getName(), codec, command, getName(), bitIndex, value ? 1 : 0);
}

@Override
Expand All @@ -100,7 +105,7 @@ public long cardinality() {
}

@Override
public int size() {
public long size() {
return get(sizeAsync());
}

Expand All @@ -110,8 +115,8 @@ public void clear(long fromIndex, long toIndex) {
}

@Override
public void clear(long bitIndex) {
get(clearAsync(bitIndex));
public boolean clear(long bitIndex) {
return get(clearAsync(bitIndex));
}

@Override
Expand Down Expand Up @@ -232,12 +237,12 @@ public RFuture<Void> setAsync(long fromIndex, long toIndex) {
}

@Override
public RFuture<Integer> sizeAsync() {
public RFuture<Long> sizeAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.BITS_SIZE, getName());
}

@Override
public RFuture<Void> setAsync(long bitIndex) {
public RFuture<Boolean> setAsync(long bitIndex) {
return setAsync(bitIndex, true);
}

Expand All @@ -247,7 +252,7 @@ public RFuture<Long> cardinalityAsync() {
}

@Override
public RFuture<Void> clearAsync(long bitIndex) {
public RFuture<Boolean> clearAsync(long bitIndex) {
return setAsync(bitIndex, false);
}

Expand Down
16 changes: 12 additions & 4 deletions redisson/src/main/java/org/redisson/RedissonBloomFilter.java
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;

import org.redisson.api.RBitSetAsync;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RFuture;
import org.redisson.client.RedisException;
Expand Down Expand Up @@ -104,8 +105,9 @@ public boolean add(T object) {

CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
addConfigCheck(hashIterations, size, executorService);
RBitSetAsync bs = createBitSet(executorService);
for (int i = 0; i < indexes.length; i++) {
executorService.writeAsync(getName(), codec, RedisCommands.SETBIT, getName(), indexes[i], 1);
bs.setAsync(indexes[i]);
}
try {
List<Boolean> result = (List<Boolean>) executorService.execute();
Expand Down Expand Up @@ -154,8 +156,9 @@ public boolean contains(T object) {

CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
addConfigCheck(hashIterations, size, executorService);
RBitSetAsync bs = createBitSet(executorService);
for (int i = 0; i < indexes.length; i++) {
executorService.readAsync(getName(), codec, RedisCommands.GETBIT, getName(), indexes[i]);
bs.getAsync(indexes[i]);
}
try {
List<Boolean> result = (List<Boolean>) executorService.execute();
Expand All @@ -175,6 +178,10 @@ public boolean contains(T object) {
}
}

protected RBitSetAsync createBitSet(CommandBatchService executorService) {
return new RedissonBitSet(executorService, getName());
}

private void addConfigCheck(int hashIterations, long size, CommandBatchService executorService) {
executorService.evalReadAsync(getConfigName(), codec, RedisCommands.EVAL_VOID,
"local size = redis.call('hget', KEYS[1], 'size');" +
Expand All @@ -188,7 +195,8 @@ public int count() {
CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
RFuture<Map<String, String>> configFuture = executorService.readAsync(getConfigName(), StringCodec.INSTANCE,
new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder()), getConfigName());
RFuture<Long> cardinalityFuture = executorService.readAsync(getName(), codec, RedisCommands.BITCOUNT, getName());
RBitSetAsync bs = createBitSet(executorService);
RFuture<Long> cardinalityFuture = bs.cardinalityAsync();
executorService.execute();

readConfig(configFuture.getNow());
Expand Down Expand Up @@ -260,7 +268,7 @@ public boolean tryInit(long expectedInsertions, double falseProbability) {
}

private String getConfigName() {
return "{" + getName() + "}" + "__config";
return suffixName(getName(), "config");
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions redisson/src/main/java/org/redisson/api/RBitSet.java
Expand Up @@ -37,7 +37,7 @@ public interface RBitSet extends RExpirable, RBitSetAsync {

void set(long fromIndex, long toIndex);

int size();
long size();

boolean get(long bitIndex);

Expand All @@ -49,7 +49,7 @@ public interface RBitSet extends RExpirable, RBitSetAsync {

long cardinality();

void clear(long bitIndex);
boolean clear(long bitIndex);

void clear();

Expand Down
8 changes: 4 additions & 4 deletions redisson/src/main/java/org/redisson/api/RBitSetAsync.java
Expand Up @@ -39,17 +39,17 @@ public interface RBitSetAsync extends RExpirableAsync {

RFuture<Void> setAsync(long fromIndex, long toIndex);

RFuture<Integer> sizeAsync();
RFuture<Long> sizeAsync();

RFuture<Boolean> getAsync(long bitIndex);

RFuture<Void> setAsync(long bitIndex);
RFuture<Boolean> setAsync(long bitIndex);

RFuture<Void> setAsync(long bitIndex, boolean value);
RFuture<Boolean> setAsync(long bitIndex, boolean value);

RFuture<Long> cardinalityAsync();

RFuture<Void> clearAsync(long bitIndex);
RFuture<Boolean> clearAsync(long bitIndex);

RFuture<Void> clearAsync();

Expand Down
8 changes: 4 additions & 4 deletions redisson/src/main/java/org/redisson/api/RBitSetReactive.java
Expand Up @@ -42,17 +42,17 @@ public interface RBitSetReactive extends RExpirableReactive {

Publisher<Void> set(long fromIndex, long toIndex);

Publisher<Integer> size();
Publisher<Long> size();

Publisher<Boolean> get(long bitIndex);

Publisher<Void> set(long bitIndex);
Publisher<Boolean> set(long bitIndex);

Publisher<Void> set(long bitIndex, boolean value);
Publisher<Boolean> set(long bitIndex, boolean value);

Publisher<Long> cardinality();

Publisher<Void> clear(long bitIndex);
Publisher<Boolean> clear(long bitIndex);

Publisher<Void> clear();

Expand Down
Expand Up @@ -86,12 +86,13 @@ public interface RedisCommands {
RedisStrictCommand<RType> TYPE = new RedisStrictCommand<RType>("TYPE", new TypeConvertor());

RedisStrictCommand<Boolean> GETBIT = new RedisStrictCommand<Boolean>("GETBIT", new BooleanReplayConvertor());
RedisStrictCommand<Integer> BITS_SIZE = new RedisStrictCommand<Integer>("STRLEN", new BitsSizeReplayConvertor());
RedisStrictCommand<Long> BITS_SIZE = new RedisStrictCommand<Long>("STRLEN", new BitsSizeReplayConvertor());
RedisStrictCommand<Long> STRLEN = new RedisStrictCommand<Long>("STRLEN");
RedisStrictCommand<Long> BITCOUNT = new RedisStrictCommand<Long>("BITCOUNT");
RedisStrictCommand<Integer> BITPOS = new RedisStrictCommand<Integer>("BITPOS", new IntegerReplayConvertor());
RedisStrictCommand<Void> SETBIT_VOID = new RedisStrictCommand<Void>("SETBIT", new VoidReplayConvertor());
RedisStrictCommand<Boolean> SETBIT = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor());
RedisStrictCommand<Boolean> SETBIT_TRUE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(0));
RedisStrictCommand<Boolean> SETBIT_FALSE = new RedisStrictCommand<Boolean>("SETBIT", new BitSetReplayConvertor(1));
RedisStrictCommand<Void> BITOP = new RedisStrictCommand<Void>("BITOP", new VoidReplayConvertor());

RedisStrictCommand<Integer> WAIT = new RedisStrictCommand<Integer>("WAIT", new IntegerReplayConvertor());
Expand Down
Expand Up @@ -15,11 +15,23 @@
*/
package org.redisson.client.protocol.convertor;

/**
*
* @author Nikita Koksharov
*
*/
public class BitSetReplayConvertor extends SingleConvertor<Boolean> {

private final int expectedValue;

public BitSetReplayConvertor(int expectedValue) {
super();
this.expectedValue = expectedValue;
}

@Override
public Boolean convert(Object obj) {
return Long.valueOf(0).equals(obj);
return expectedValue == (Long)obj;
}


Expand Down
Expand Up @@ -15,15 +15,14 @@
*/
package org.redisson.client.protocol.convertor;

public class BitsSizeReplayConvertor extends SingleConvertor<Integer> {
public class BitsSizeReplayConvertor extends SingleConvertor<Long> {

@Override
public Integer convert(Object obj) {
public Long convert(Object obj) {
if (obj == null) {
return null;
}
int val = ((Long) obj).intValue();
return val * 8;
return ((Long) obj) * 8;
}

}
Expand Up @@ -51,10 +51,10 @@ public RFuture<Boolean> get() {
});
}

public Publisher<Void> set(final long bitIndex, final boolean value) {
return reactive(new Supplier<RFuture<Void>>() {
public Publisher<Boolean> set(final long bitIndex, final boolean value) {
return reactive(new Supplier<RFuture<Boolean>>() {
@Override
public RFuture<Void> get() {
public RFuture<Boolean> get() {
return instance.setAsync(bitIndex, value);
}
});
Expand Down Expand Up @@ -134,20 +134,20 @@ public RFuture<Void> get() {
}

@Override
public Publisher<Integer> size() {
return reactive(new Supplier<RFuture<Integer>>() {
public Publisher<Long> size() {
return reactive(new Supplier<RFuture<Long>>() {
@Override
public RFuture<Integer> get() {
public RFuture<Long> get() {
return instance.sizeAsync();
}
});
}

@Override
public Publisher<Void> set(final long bitIndex) {
return reactive(new Supplier<RFuture<Void>>() {
public Publisher<Boolean> set(final long bitIndex) {
return reactive(new Supplier<RFuture<Boolean>>() {
@Override
public RFuture<Void> get() {
public RFuture<Boolean> get() {
return instance.setAsync(bitIndex);
}
});
Expand All @@ -164,10 +164,10 @@ public RFuture<Long> get() {
}

@Override
public Publisher<Void> clear(final long bitIndex) {
return reactive(new Supplier<RFuture<Void>>() {
public Publisher<Boolean> clear(final long bitIndex) {
return reactive(new Supplier<RFuture<Boolean>>() {
@Override
public RFuture<Void> get() {
public RFuture<Boolean> get() {
return instance.clearAsync(bitIndex);
}
});
Expand Down

0 comments on commit 06c726b

Please sign in to comment.