use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class ContinuousQueryObjectStorageTest method testContinuousQuery.
public void testContinuousQuery() {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<User> query = qf.<User>create("FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, User>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, User> listener = new ContinuousQueryListener<String, User>() {
@Override
public void resultJoining(String key, User value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue().getAge(), 32, 22);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue().getAge(), 30);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
user1.setAge(40);
user2.setAge(40);
user3.setAge(40);
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 3);
expectNoMoreElementsInQueues(joined, left);
remoteCache.clear();
user1.setAge(21);
user2.setAge(22);
remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
expectElementsInQueue(joined, 2);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
expectNoMoreElementsInQueues(joined, left);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class ContinuousQueryObjectStorageTest method testContinuousQueryChangingParameter.
public void testContinuousQueryChangingParameter() throws InterruptedException {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> cq = Search.getContinuousQuery(remoteCache);
cq.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
joined.clear();
left.clear();
cq.removeContinuousQueryListener(listener);
query.setParameter("ageParam", 40);
listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
cq.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 3);
expectElementsInQueue(left, 0);
cq.removeContinuousQueryListener(listener);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class EagerKeyValueConverterTest method testWriteMap.
public void testWriteMap() throws InterruptedException {
BlockingQueue<KeyValuePair<String, Person>> eventsQueue = new LinkedBlockingQueue<>();
RemoteCache<String, Person> cache = remoteCacheManager.getCache();
cache.addClientListener(new EventListener(eventsQueue, cache.getDataFormat()));
Map<String, Person> data = new HashMap<>();
data.put("1", new Person("John"));
data.put("2", new Person("Mary"));
data.put("3", new Person("George"));
cache.putAll(data);
KeyValuePair<String, Person> event = eventsQueue.poll(5, TimeUnit.SECONDS);
assertNotNull(event);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class JsonKeyValueRawEventsTest method testReceiveKeyValuesAsJson.
public void testReceiveKeyValuesAsJson() throws InterruptedException {
BlockingQueue<KeyValuePair<String, String>> eventsQueue = new LinkedBlockingQueue<>();
DataFormat jsonValues = DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build();
RemoteCache<String, Person> cache = remoteCacheManager.getCache();
RemoteCache<String, String> jsonCache = cache.withDataFormat(jsonValues);
jsonCache.addClientListener(new EventListener(eventsQueue, jsonCache.getDataFormat()));
cache.put("1", new Person("John"));
KeyValuePair<String, String> event = eventsQueue.poll(5, TimeUnit.SECONDS);
assertNotNull(event);
assertEquals(event.getKey(), "1");
assertEquals(event.getValue(), "\n{\n \"_type\": \"org.infinispan.test.core.Person\",\n \"name\": \"John\",\n \"birthDate\": 0,\n \"accepted_tos\": false\n}\n");
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class TopologyAwareConsistentHashFactory method getLocationId.
private Object getLocationId(Address address, TopologyLevel level) {
TopologyAwareAddress taa = (TopologyAwareAddress) address;
Object locationId;
switch(level) {
case SITE:
locationId = taa.getSiteId();
break;
case RACK:
locationId = new KeyValuePair<>(taa.getSiteId(), taa.getRackId());
break;
case MACHINE:
locationId = new KeyValuePair<>(taa.getSiteId(), new KeyValuePair<>(taa.getRackId(), taa.getMachineId()));
break;
case NODE:
locationId = address;
break;
default:
throw new IllegalStateException("Unknown level: " + level);
}
return locationId;
}
Aggregations