use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class ContinuousQueryObjectStorageTest method testContinuousQueryWithProjections.
public void testContinuousQueryWithProjections() 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> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue()[0], 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);
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);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class SingleFileStore method purgeExpiredEntries.
private void purgeExpiredEntries(long now, UnicastProcessor<MarshallableEntry<K, V>> processor, List<KeyValuePair<Object, FileEntry>> entriesToPurge) {
entriesToPurge.sort(Comparator.comparingLong(kvp -> kvp.getValue().offset));
for (ListIterator<KeyValuePair<Object, FileEntry>> it = entriesToPurge.listIterator(); it.hasNext(); ) {
KeyValuePair<Object, FileEntry> next = it.next();
FileEntry fe = next.getValue();
if (fe.isExpired(now)) {
it.set(null);
// Safe to unlock because the entry was locked collectExpiredEntries
MarshallableEntry<K, V> entry = readFromDisk(fe, next.getKey(), true, true);
processor.onNext(entry);
try {
free(fe);
} catch (Exception e) {
throw new PersistenceException(e);
}
}
}
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class JpaStore method bulkUpdate.
@Override
public CompletionStage<Void> bulkUpdate(Publisher<MarshallableEntry<? extends K, ? extends V>> publisher) {
CompletableFuture<Void> future = new CompletableFuture<>();
Flowable.using(() -> {
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
return new KeyValuePair<>(em, txn);
}, kvp -> createBatchFlowable(kvp.getKey(), kvp.getValue(), publisher), kvp -> {
EntityTransaction txn = kvp.getValue();
if (txn != null && txn.isActive())
txn.rollback();
kvp.getKey().close();
}).doOnError(e -> {
if (e instanceof JpaStoreException)
throw (JpaStoreException) e;
throw new JpaStoreException("Exception caught in bulkUpdate()", e);
}).subscribe(RxJavaInterop.emptyConsumer(), future::completeExceptionally, () -> future.complete(null));
return future;
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class ProtobufMetadataManagerInterceptor method visitReplaceCommand.
@Override
public Object visitReplaceCommand(InvocationContext ctx, ReplaceCommand command) {
final Object key = command.getKey();
final Object value = command.getNewValue();
if (!ctx.isOriginLocal()) {
return invokeNext(ctx, command);
}
if (!(key instanceof String)) {
throw log.keyMustBeString(key.getClass());
}
if (!(value instanceof String)) {
throw log.valueMustBeString(value.getClass());
}
if (!shouldIntercept(key)) {
return invokeNext(ctx, command);
}
if (!((String) key).endsWith(PROTO_KEY_SUFFIX)) {
throw log.keyMustBeStringEndingWithProto(key);
}
// lock global errors key
LockControlCommand cmd = commandsFactory.buildLockControlCommand(ERRORS_KEY_SUFFIX, command.getFlagsBitSet(), null);
InvocationStage stage = invoker.running().invokeStage(ctx, cmd);
return makeStage(asyncInvokeNext(ctx, command, stage)).thenApply(ctx, command, (rCtx, rCommand, rv) -> {
if (rCommand.isSuccessful()) {
long flagsBitSet = copyFlags(rCommand);
if (rCtx.isOriginLocal()) {
ProgressCallback progressCallback = new ProgressCallback();
registerProtoFile((String) key, (String) value, progressCallback);
List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
return updateSchemaErrorsIterator(rCtx, flagsBitSet, errorUpdates.iterator());
}
registerProtoFile((String) key, (String) value, EMPTY_CALLBACK);
}
return InvocationStage.completedNullStage();
});
}
use of org.infinispan.util.KeyValuePair in project infinispan by infinispan.
the class ProtobufMetadataManagerInterceptor method handlePutKeyValueResult.
private InvocationStage handlePutKeyValueResult(InvocationContext ctx, PutKeyValueCommand putKeyValueCommand, Object rv) {
if (putKeyValueCommand.isSuccessful()) {
// StateConsumerImpl uses PutKeyValueCommands with InternalCacheEntry
// values in order to preserve timestamps, so read the value from the context
Object key = putKeyValueCommand.getKey();
Object value = ctx.lookupEntry(key).getValue();
if (!(value instanceof String)) {
throw log.valueMustBeString(value.getClass());
}
long flagsBitSet = copyFlags(putKeyValueCommand);
if (ctx.isOriginLocal() && !putKeyValueCommand.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER)) {
ProgressCallback progressCallback = new ProgressCallback();
registerProtoFile((String) key, (String) value, progressCallback);
List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
InvocationStage updateStage = updateSchemaErrorsIterator(ctx, flagsBitSet, errorUpdates.iterator());
return makeStage(updateStage.thenReturn(ctx, putKeyValueCommand, rv));
}
registerProtoFile((String) key, (String) value, EMPTY_CALLBACK);
}
return makeStage(rv);
}
Aggregations