use of org.redisson.client.protocol.decoder.ListScanResult in project redisson by redisson.
the class SetReactiveIterator method subscribe.
@Override
public void subscribe(final Subscriber<? super V> t) {
t.onSubscribe(new ReactiveSubscription<V>(this, t) {
private List<ByteBuf> firstValues;
private List<ByteBuf> lastValues;
private long nextIterPos;
private InetSocketAddress client;
private boolean finished;
@Override
protected void onRequest(long n) {
nextValues();
}
private void handle(List<ScanObjectEntry> vals) {
for (ScanObjectEntry val : vals) {
onNext((V) val.getObj());
}
}
protected void nextValues() {
final ReactiveSubscription<V> m = this;
scanIteratorReactive(client, nextIterPos).subscribe(new Subscriber<ListScanResult<ScanObjectEntry>>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ListScanResult<ScanObjectEntry> res) {
if (finished) {
free(firstValues);
free(lastValues);
client = null;
firstValues = null;
lastValues = null;
nextIterPos = 0;
return;
}
long prevIterPos = nextIterPos;
if (lastValues != null) {
free(lastValues);
}
lastValues = convert(res.getValues());
client = res.getRedisClient();
if (nextIterPos == 0 && firstValues == null) {
firstValues = lastValues;
lastValues = null;
if (firstValues.isEmpty()) {
client = null;
firstValues = null;
nextIterPos = 0;
prevIterPos = -1;
}
} else {
if (firstValues.isEmpty()) {
firstValues = lastValues;
lastValues = null;
if (firstValues.isEmpty()) {
if (res.getPos() == 0) {
finished = true;
m.onComplete();
return;
}
}
} else if (lastValues.removeAll(firstValues)) {
free(firstValues);
free(lastValues);
client = null;
firstValues = null;
lastValues = null;
nextIterPos = 0;
prevIterPos = -1;
finished = true;
m.onComplete();
return;
}
}
handle(res.getValues());
nextIterPos = res.getPos();
if (prevIterPos == nextIterPos) {
finished = true;
m.onComplete();
}
}
@Override
public void onError(Throwable error) {
m.onError(error);
}
@Override
public void onComplete() {
if (finished) {
return;
}
nextValues();
}
});
}
});
}
use of org.redisson.client.protocol.decoder.ListScanResult in project redisson by redisson.
the class RedissonKeysReactive method createKeysIterator.
private Publisher<String> createKeysIterator(final MasterSlaveEntry entry, final String pattern) {
return new Stream<String>() {
@Override
public void subscribe(final Subscriber<? super String> t) {
t.onSubscribe(new ReactiveSubscription<String>(this, t) {
private List<String> firstValues;
private long nextIterPos;
private InetSocketAddress client;
private long currentIndex;
@Override
protected void onRequest(final long n) {
currentIndex = n;
nextValues();
}
protected void nextValues() {
final ReactiveSubscription<String> m = this;
scanIterator(entry, nextIterPos, pattern).subscribe(new Subscriber<ListScanResult<String>>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ListScanResult<String> res) {
client = res.getRedisClient();
long prevIterPos = nextIterPos;
if (nextIterPos == 0 && firstValues == null) {
firstValues = res.getValues();
} else if (res.getValues().equals(firstValues)) {
m.onComplete();
currentIndex = 0;
return;
}
nextIterPos = res.getPos();
if (prevIterPos == nextIterPos) {
nextIterPos = -1;
}
for (String val : res.getValues()) {
m.onNext(val);
currentIndex--;
if (currentIndex == 0) {
m.onComplete();
return;
}
}
if (nextIterPos == -1) {
m.onComplete();
currentIndex = 0;
}
}
@Override
public void onError(Throwable error) {
m.onError(error);
}
@Override
public void onComplete() {
if (currentIndex == 0) {
return;
}
nextValues();
}
});
}
});
}
};
}
use of org.redisson.client.protocol.decoder.ListScanResult in project redisson by redisson.
the class CommandAsyncService method handleReference.
private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
if (res instanceof List) {
List<Object> r = (List<Object>) res;
for (int i = 0; i < r.size(); i++) {
if (r.get(i) instanceof RedissonReference) {
try {
r.set(i, redisson != null ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) r.get(i)) : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) r.get(i)));
} catch (Exception exception) {
//skip and carry on to next one.
}
} else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) {
try {
ScoredEntry<?> se = ((ScoredEntry<?>) r.get(i));
se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
r.set(i, se);
} catch (Exception exception) {
//skip and carry on to next one.
}
}
}
mainPromise.trySuccess(res);
} else if (res instanceof ListScanResult) {
List<ScanObjectEntry> r = ((ListScanResult) res).getValues();
for (int i = 0; i < r.size(); i++) {
Object obj = r.get(i);
if (!(obj instanceof ScanObjectEntry)) {
break;
}
ScanObjectEntry e = r.get(i);
if (e.getObj() instanceof RedissonReference) {
try {
r.set(i, new ScanObjectEntry(e.getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getObj())));
} catch (Exception exception) {
//skip and carry on to next one.
}
} else if (e.getObj() instanceof ScoredEntry && ((ScoredEntry<?>) e.getObj()).getValue() instanceof RedissonReference) {
try {
ScoredEntry<?> se = ((ScoredEntry<?>) e.getObj());
se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
r.set(i, new ScanObjectEntry(e.getBuf(), se));
} catch (Exception exception) {
//skip and carry on to next one.
}
}
}
mainPromise.trySuccess(res);
} else if (res instanceof MapScanResult) {
Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult) res).getMap();
HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
if (e.getValue().getObj() instanceof RedissonReference) {
try {
e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getValue().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getValue().getObj())));
} catch (Exception exception) {
//skip and carry on to next one.
}
}
if (e.getKey().getObj() instanceof RedissonReference) {
if (toAdd == null) {
toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
}
toAdd.put(e.getKey(), e.getValue());
}
}
if (toAdd != null) {
for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : toAdd.entrySet()) {
try {
map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getKey().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey()));
} catch (Exception exception) {
//skip and carry on to next one.
}
}
}
mainPromise.trySuccess(res);
} else if (res instanceof RedissonReference) {
try {
mainPromise.trySuccess(redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
} catch (Exception exception) {
//fallback
mainPromise.trySuccess(res);
}
} else {
mainPromise.trySuccess(res);
}
}
use of org.redisson.client.protocol.decoder.ListScanResult in project redisson by redisson.
the class RedissonLiveObjectService method findIds.
@Override
public <K> Iterable<K> findIds(Class<?> entityClass, int count) {
NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
String pattern = namingScheme.getNamePattern(entityClass);
RedissonKeys keys = new RedissonKeys(commandExecutor);
RedisCommand<ListScanResult<String>> command = new RedisCommand<>("SCAN", new ListMultiDecoder2(new ListScanResultReplayDecoder(), new ObjectListReplayDecoder<Object>()), new Convertor<Object>() {
@Override
public Object convert(Object obj) {
if (!(obj instanceof String)) {
return obj;
}
return namingScheme.resolveId(obj.toString());
}
});
return keys.getKeysByPattern(command, pattern, 0, count);
}
use of org.redisson.client.protocol.decoder.ListScanResult in project redisson by redisson.
the class RedissonObjectBuilder method tryHandleReference.
public Object tryHandleReference(Object o, ReferenceType type) throws ReflectiveOperationException {
boolean hasConversion = false;
if (o instanceof List) {
List<Object> r = (List<Object>) o;
for (int i = 0; i < r.size(); i++) {
Object ref = tryHandleReference0(r.get(i), type);
if (ref != r.get(i)) {
r.set(i, ref);
}
}
return o;
} else if (o instanceof Set) {
Set<Object> set;
Set<Object> r = (Set<Object>) o;
boolean useNewSet = o instanceof LinkedHashSet;
try {
set = (Set<Object>) o.getClass().getConstructor().newInstance();
} catch (Exception exception) {
set = new LinkedHashSet<>();
}
for (Object i : r) {
Object ref = tryHandleReference0(i, type);
// like only one element fails.
if (useNewSet) {
set.add(ref);
} else {
try {
r.add(ref);
set.add(i);
} catch (Exception e) {
// r is not supporting add operation, like
// LinkedHashMap$LinkedEntrySet and others.
// fall back to use a new set.
useNewSet = true;
set.add(ref);
}
}
hasConversion |= ref != i;
}
if (!hasConversion) {
return o;
} else if (useNewSet) {
return set;
} else if (!set.isEmpty()) {
r.removeAll(set);
}
return o;
} else if (o instanceof Map) {
Map<Object, Object> r = (Map<Object, Object>) o;
for (Map.Entry<Object, Object> e : r.entrySet()) {
if (e.getKey() instanceof RedissonReference || e.getValue() instanceof RedissonReference) {
Object key = e.getKey();
Object value = e.getValue();
if (e.getKey() instanceof RedissonReference) {
key = fromReference((RedissonReference) e.getKey(), type);
r.remove(e.getKey());
}
if (e.getValue() instanceof RedissonReference) {
value = fromReference((RedissonReference) e.getValue(), type);
}
r.put(key, value);
}
}
return o;
} else if (o instanceof ListScanResult) {
tryHandleReference(((ListScanResult) o).getValues(), type);
return o;
} else if (o instanceof MapScanResult) {
MapScanResult scanResult = (MapScanResult) o;
Map oldMap = ((MapScanResult) o).getMap();
Map map = (Map) tryHandleReference(oldMap, type);
if (map != oldMap) {
MapScanResult<Object, Object> newScanResult = new MapScanResult<Object, Object>(scanResult.getPos(), map);
newScanResult.setRedisClient(scanResult.getRedisClient());
return newScanResult;
} else {
return o;
}
} else {
return tryHandleReference0(o, type);
}
}
Aggregations