use of org.redisson.api.RSet in project redisson by redisson.
the class RedissonLiveObjectService method detach.
@SuppressWarnings("unchecked")
private <T> T detach(T attachedObject, Map<String, Object> alreadyDetached) {
validateAttached(attachedObject);
try {
T detached = instantiateDetachedObject((Class<T>) attachedObject.getClass().getSuperclass(), asLiveObject(attachedObject).getLiveObjectId());
BeanCopy.beans(attachedObject, detached).declared(true, true).copy();
alreadyDetached.put(getMap(attachedObject).getName(), detached);
for (Entry<String, Object> obj : getMap(attachedObject).entrySet()) {
if (!checkCascade(attachedObject, RCascadeType.DETACH, obj.getKey())) {
continue;
}
if (obj.getValue() instanceof RSortedSet) {
SortedSet<Object> redissonSet = (SortedSet<Object>) obj.getValue();
Set<Object> set = new TreeSet<Object>(redissonSet.comparator());
for (Object object : redissonSet) {
if (isLiveObject(object)) {
Object detachedObject = alreadyDetached.get(getMap(object).getName());
if (detachedObject == null) {
detachedObject = detach(object, alreadyDetached);
}
object = detachedObject;
}
set.add(object);
}
ClassUtils.setField(detached, obj.getKey(), set);
} else if (obj.getValue() instanceof RDeque) {
Collection<Object> redissonDeque = (Collection<Object>) obj.getValue();
Deque<Object> deque = new LinkedList<Object>();
for (Object object : redissonDeque) {
if (isLiveObject(object)) {
Object detachedObject = alreadyDetached.get(getMap(object).getName());
if (detachedObject == null) {
detachedObject = detach(object, alreadyDetached);
}
object = detachedObject;
}
deque.add(object);
}
ClassUtils.setField(detached, obj.getKey(), deque);
} else if (obj.getValue() instanceof RQueue) {
Collection<Object> redissonQueue = (Collection<Object>) obj.getValue();
Queue<Object> queue = new LinkedList<Object>();
for (Object object : redissonQueue) {
if (isLiveObject(object)) {
Object detachedObject = alreadyDetached.get(getMap(object).getName());
if (detachedObject == null) {
detachedObject = detach(object, alreadyDetached);
}
object = detachedObject;
}
queue.add(object);
}
ClassUtils.setField(detached, obj.getKey(), queue);
} else if (obj.getValue() instanceof RSet) {
Set<Object> set = new HashSet<Object>();
Collection<Object> redissonSet = (Collection<Object>) obj.getValue();
for (Object object : redissonSet) {
if (isLiveObject(object)) {
Object detachedObject = alreadyDetached.get(getMap(object).getName());
if (detachedObject == null) {
detachedObject = detach(object, alreadyDetached);
}
object = detachedObject;
}
set.add(object);
}
ClassUtils.setField(detached, obj.getKey(), set);
} else if (obj.getValue() instanceof RList) {
List<Object> list = new ArrayList<Object>();
Collection<Object> redissonList = (Collection<Object>) obj.getValue();
for (Object object : redissonList) {
if (isLiveObject(object)) {
Object detachedObject = alreadyDetached.get(getMap(object).getName());
if (detachedObject == null) {
detachedObject = detach(object, alreadyDetached);
}
object = detachedObject;
}
list.add(object);
}
ClassUtils.setField(detached, obj.getKey(), list);
} else if (isLiveObject(obj.getValue())) {
Object detachedObject = alreadyDetached.get(getMap(obj.getValue()).getName());
if (detachedObject == null) {
detachedObject = detach(obj.getValue(), alreadyDetached);
}
ClassUtils.setField(detached, obj.getKey(), detachedObject);
} else if (obj.getValue() instanceof RMap) {
Map<Object, Object> map = new LinkedHashMap<Object, Object>();
Map<Object, Object> redissonMap = (Map<Object, Object>) obj.getValue();
for (Entry<Object, Object> entry : redissonMap.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (isLiveObject(key)) {
Object detachedObject = alreadyDetached.get(getMap(key).getName());
if (detachedObject == null) {
detachedObject = detach(key, alreadyDetached);
}
key = detachedObject;
}
if (isLiveObject(value)) {
Object detachedObject = alreadyDetached.get(getMap(value).getName());
if (detachedObject == null) {
detachedObject = detach(value, alreadyDetached);
}
value = detachedObject;
}
map.put(key, value);
}
ClassUtils.setField(detached, obj.getKey(), map);
} else {
validateAnnotation(detached, obj.getKey());
}
}
return detached;
} catch (Exception ex) {
throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
}
}
use of org.redisson.api.RSet in project redisson by redisson.
the class RedissonSessionManager method startInternal.
@Override
protected void startInternal() throws LifecycleException {
super.startInternal();
redisson = buildClient();
final ClassLoader applicationClassLoader;
if (getContext().getLoader().getClassLoader() != null) {
applicationClassLoader = getContext().getLoader().getClassLoader();
} else if (Thread.currentThread().getContextClassLoader() != null) {
applicationClassLoader = Thread.currentThread().getContextClassLoader();
} else {
applicationClassLoader = getClass().getClassLoader();
}
Codec codec = redisson.getConfig().getCodec();
try {
codecToUse = codec.getClass().getConstructor(ClassLoader.class, codec.getClass()).newInstance(applicationClassLoader, codec);
} catch (Exception e) {
throw new LifecycleException(e);
}
Pipeline pipeline = getContext().getPipeline();
synchronized (pipeline) {
if (readMode == ReadMode.REDIS) {
Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UsageValve.class).findAny();
if (res.isPresent()) {
((UsageValve) res.get()).incUsage();
} else {
pipeline.addValve(new UsageValve());
}
}
if (updateMode == UpdateMode.AFTER_REQUEST) {
Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UpdateValve.class).findAny();
if (res.isPresent()) {
((UpdateValve) res.get()).incUsage();
} else {
pipeline.addValve(new UpdateValve());
}
}
}
if (readMode == ReadMode.MEMORY && this.broadcastSessionUpdates || broadcastSessionEvents) {
RTopic updatesTopic = getTopic();
messageListener = new MessageListener<AttributeMessage>() {
@Override
public void onMessage(CharSequence channel, AttributeMessage msg) {
try {
if (msg.getNodeId().equals(nodeId)) {
return;
}
RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId());
if (session != null) {
if (msg instanceof SessionDestroyedMessage) {
session.expire();
}
if (msg instanceof AttributeRemoveMessage) {
for (String name : ((AttributeRemoveMessage) msg).getNames()) {
session.superRemoveAttributeInternal(name, true);
}
}
if (msg instanceof AttributesClearMessage) {
RedissonSessionManager.super.remove(session, false);
}
if (msg instanceof AttributesPutAllMessage) {
AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
Map<String, Object> attrs = m.getAttrs(codecToUse.getMapValueDecoder());
session.load(attrs);
}
if (msg instanceof AttributeUpdateMessage) {
AttributeUpdateMessage m = (AttributeUpdateMessage) msg;
session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true);
}
} else {
if (msg instanceof SessionCreatedMessage) {
Session s = findSession(msg.getSessionId());
if (s == null) {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
}
}
if (msg instanceof SessionDestroyedMessage) {
Session s = findSession(msg.getSessionId(), false);
if (s == null) {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
}
s.expire();
RSet<String> set = getNotifiedNodes(msg.getSessionId());
set.add(nodeId);
}
}
} catch (Exception e) {
log.error("Unable to handle topic message", e);
}
}
};
updatesTopic.addListener(AttributeMessage.class, messageListener);
}
setState(LifecycleState.STARTING);
}
use of org.redisson.api.RSet in project redisson by redisson.
the class LiveObjectSearch method traverseAnd.
private Set<Object> traverseAnd(ANDCondition condition, NamingScheme namingScheme, Class<?> entityClass) {
Set<Object> allIds = new HashSet<Object>();
List<String> eqNames = new ArrayList<String>();
Map<RScoredSortedSet<Object>, Number> gtNumericNames = new HashMap<>();
Map<RScoredSortedSet<Object>, Number> geNumericNames = new HashMap<>();
Map<RScoredSortedSet<Object>, Number> ltNumericNames = new HashMap<>();
Map<RScoredSortedSet<Object>, Number> leNumericNames = new HashMap<>();
Map<RScoredSortedSet<Object>, Number> eqNumericNames = new HashMap<>();
for (Condition cond : condition.getConditions()) {
if (cond instanceof EQCondition) {
EQCondition eqc = (EQCondition) cond;
String indexName = namingScheme.getIndexName(entityClass, eqc.getName());
if (eqc.getValue() instanceof Number) {
RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
eqNumericNames.put(values, (Number) eqc.getValue());
} else {
RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
RSet<Object> values = map.get(eqc.getValue());
eqNames.add(((RedissonObject) values).getRawName());
}
}
if (cond instanceof LTCondition) {
LTCondition ltc = (LTCondition) cond;
String indexName = namingScheme.getIndexName(entityClass, ltc.getName());
RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
ltNumericNames.put(values, ltc.getValue());
}
if (cond instanceof LECondition) {
LECondition lec = (LECondition) cond;
String indexName = namingScheme.getIndexName(entityClass, lec.getName());
RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
leNumericNames.put(values, lec.getValue());
}
if (cond instanceof GECondition) {
GECondition gec = (GECondition) cond;
String indexName = namingScheme.getIndexName(entityClass, gec.getName());
RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
geNumericNames.put(values, gec.getValue());
}
if (cond instanceof GTCondition) {
GTCondition gtc = (GTCondition) cond;
String indexName = namingScheme.getIndexName(entityClass, gtc.getName());
RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
gtNumericNames.put(values, gtc.getValue());
}
if (cond instanceof ORCondition) {
Collection<Object> ids = traverseOr((ORCondition) cond, namingScheme, entityClass);
if (ids.isEmpty()) {
return Collections.emptySet();
}
if (!allIds.isEmpty()) {
allIds.retainAll(ids);
} else {
allIds.addAll(ids);
}
if (allIds.isEmpty()) {
return Collections.emptySet();
}
}
}
if (!eqNames.isEmpty()) {
RSet<Object> set = new RedissonSet<>(commandExecutor, eqNames.get(0), null);
Set<Object> intersect = set.readIntersection(eqNames.toArray(new String[eqNames.size()]));
if (!allIds.isEmpty()) {
allIds.retainAll(intersect);
if (allIds.isEmpty()) {
return Collections.emptySet();
}
} else {
allIds.addAll(intersect);
}
if (allIds.isEmpty()) {
return allIds;
}
}
if (!checkValueRange(allIds, eqNumericNames, (r, v) -> {
return r.valueRange(v.doubleValue(), true, v.doubleValue(), true);
})) {
return Collections.emptySet();
}
if (!checkValueRange(allIds, gtNumericNames, (r, v) -> {
return r.valueRange(v.doubleValue(), false, Double.POSITIVE_INFINITY, false);
})) {
return Collections.emptySet();
}
if (!checkValueRange(allIds, geNumericNames, (r, v) -> {
return r.valueRange(v.doubleValue(), true, Double.POSITIVE_INFINITY, false);
})) {
return Collections.emptySet();
}
if (!checkValueRange(allIds, ltNumericNames, (r, v) -> {
return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), false);
})) {
return Collections.emptySet();
}
if (!checkValueRange(allIds, leNumericNames, (r, v) -> {
return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), true);
})) {
return Collections.emptySet();
}
return allIds;
}
Aggregations