use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.
the class TransactionIT method removeSingleDevice.
private void removeSingleDevice(int numUsers) throws InterruptedException {
do {
TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
try (Transaction t = writer.begin()) {
String userId = "user" + random.nextInt(numUsers);
List<StreamElement> devices = new ArrayList<>();
view.scanWildcard(userId, device, devices::add);
List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
if (devices.isEmpty()) {
continue;
}
long stamp = System.currentTimeMillis();
t.update(keyAttributes);
String name = device.extractSuffix(devices.get(random.nextInt(devices.size())).getAttribute());
StreamElement deviceUpdate = device.delete(userId, name, stamp);
StreamElement numDevicesUpdate = numDevices.upsert(userId, "all", stamp, devices.size() - 1);
CountDownLatch latch = new CountDownLatch(1);
t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
latch.await();
break;
} catch (TransactionRejectedException e) {
// repeat
}
} while (true);
}
use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.
the class TransactionIT_Large method writeSingleDevice.
private void writeSingleDevice(int numUsers, boolean intoAll) throws InterruptedException {
String name = UUID.randomUUID().toString();
String userId = "user" + random.nextInt(numUsers);
do {
TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
long stamp = System.currentTimeMillis();
try (Transaction t = writer.begin()) {
List<StreamElement> devices = new ArrayList<>();
view.scanWildcard(userId, device, devices::add);
List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
t.update(keyAttributes);
StreamElement deviceUpdate = device.upsert(userId, name, stamp, new byte[] {});
final StreamElement numDevicesUpdate;
int count = devices.size() + 1;
if (intoAll) {
numDevicesUpdate = numDevices.upsert(userId, "all", stamp, count);
} else {
numDevicesUpdate = numDevices.upsert(userId, String.valueOf(count), stamp, count);
}
CountDownLatch latch = new CountDownLatch(1);
t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
latch.await();
break;
} catch (TransactionRejectedException e) {
// repeat
}
} while (true);
}
use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.
the class TransactionIT_Large method transferAmountRandomly.
private void transferAmountRandomly(int numUsers) throws InterruptedException {
int first = random.nextInt(numUsers);
int second = (first + 1 + random.nextInt(numUsers - 1)) % numUsers;
String userFirst = "user" + first;
String userSecond = "user" + second;
double swap = random.nextDouble() * 1000;
TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(amount)).transactional();
do {
Optional<KeyValue<Double>> firstAmount = view.get(userFirst, amount);
Optional<KeyValue<Double>> secondAmount = view.get(userSecond, amount);
try (Transaction t = writer.begin()) {
t.update(Arrays.asList(KeyAttributes.ofAttributeDescriptor(user, userFirst, amount, firstAmount.map(KeyValue::getSequentialId).orElse(1L)), KeyAttributes.ofAttributeDescriptor(user, userSecond, amount, secondAmount.map(KeyValue::getSequentialId).orElse(1L))));
double firstWillHave = firstAmount.map(KeyValue::getParsedRequired).orElse(0.0) - swap;
double secondWillHave = secondAmount.map(KeyValue::getParsedRequired).orElse(0.0) + swap;
List<StreamElement> outputs = Arrays.asList(amount.upsert(userFirst, System.currentTimeMillis(), firstWillHave), amount.upsert(userSecond, System.currentTimeMillis(), secondWillHave));
CountDownLatch latch = new CountDownLatch(1);
t.commitWrite(outputs, (succ, exc) -> latch.countDown());
latch.await();
break;
} catch (TransactionRejectedException e) {
// repeat
}
} while (true);
}
use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.
the class TransactionIT method transferAmountRandomly.
private void transferAmountRandomly(int numUsers) throws InterruptedException {
int first = random.nextInt(numUsers);
int second = (first + 1 + random.nextInt(numUsers - 1)) % numUsers;
String userFirst = "user" + first;
String userSecond = "user" + second;
double swap = random.nextDouble() * 1000;
TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(amount)).transactional();
do {
Optional<KeyValue<Double>> firstAmount = view.get(userFirst, amount);
Optional<KeyValue<Double>> secondAmount = view.get(userSecond, amount);
try (Transaction t = writer.begin()) {
t.update(Arrays.asList(KeyAttributes.ofAttributeDescriptor(user, userFirst, amount, firstAmount.map(KeyValue::getSequentialId).orElse(1L)), KeyAttributes.ofAttributeDescriptor(user, userSecond, amount, secondAmount.map(KeyValue::getSequentialId).orElse(1L))));
double firstWillHave = firstAmount.map(KeyValue::getParsedRequired).orElse(0.0) - swap;
double secondWillHave = secondAmount.map(KeyValue::getParsedRequired).orElse(0.0) + swap;
List<StreamElement> outputs = Arrays.asList(amount.upsert(userFirst, System.currentTimeMillis(), firstWillHave), amount.upsert(userSecond, System.currentTimeMillis(), secondWillHave));
CountDownLatch latch = new CountDownLatch(1);
t.commitWrite(outputs, (succ, exc) -> latch.countDown());
latch.await();
break;
} catch (TransactionRejectedException e) {
// repeat
}
} while (true);
}
use of cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.
the class TransactionIT_Large method removeSingleDevice.
private void removeSingleDevice(int numUsers) throws InterruptedException {
do {
TransactionalOnlineAttributeWriter writer = Optionals.get(direct.getWriter(device)).transactional();
try (Transaction t = writer.begin()) {
String userId = "user" + random.nextInt(numUsers);
List<StreamElement> devices = new ArrayList<>();
view.scanWildcard(userId, device, devices::add);
List<KeyAttribute> keyAttributes = KeyAttributes.ofWildcardQueryElements(user, userId, device, devices);
if (devices.isEmpty()) {
continue;
}
long stamp = System.currentTimeMillis();
t.update(keyAttributes);
String name = device.extractSuffix(devices.get(random.nextInt(devices.size())).getAttribute());
StreamElement deviceUpdate = device.delete(userId, name, stamp);
StreamElement numDevicesUpdate = numDevices.upsert(userId, "all", stamp, devices.size() - 1);
CountDownLatch latch = new CountDownLatch(1);
t.commitWrite(Arrays.asList(deviceUpdate, numDevicesUpdate), (succ, exc) -> latch.countDown());
latch.await();
break;
} catch (TransactionRejectedException e) {
// repeat
}
} while (true);
}
Aggregations