use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class ConsistentResourceStore method register.
@Override
public boolean register(List<? extends Resource> resources) {
checkNotNull(resources);
if (log.isTraceEnabled()) {
resources.forEach(r -> log.trace("registering {}", r));
}
// Retry the transaction until successful.
while (true) {
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
// the order is preserved by LinkedHashMap
Map<DiscreteResource, List<Resource>> resourceMap = resources.stream().filter(x -> x.parent().isPresent()).collect(groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.<Resource>toList()));
TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx);
TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx);
for (Map.Entry<DiscreteResource, List<Resource>> entry : resourceMap.entrySet()) {
DiscreteResourceId parentId = entry.getKey().id();
if (!discreteTxStore.lookup(parentId).isPresent()) {
return abortTransaction(tx);
}
if (!register(discreteTxStore, continuousTxStore, parentId, entry.getValue())) {
return abortTransaction(tx);
}
}
try {
CommitStatus status = commitTransaction(tx);
if (status == CommitStatus.SUCCESS) {
log.trace("Transaction commit succeeded on registration: resources={}", resources);
List<ResourceEvent> events = resources.stream().filter(x -> x.parent().isPresent()).map(x -> new ResourceEvent(RESOURCE_ADDED, x)).collect(Collectors.toList());
notifyDelegate(events);
return true;
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.warn("Transaction commit failed on registration", e);
return false;
}
}
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class TransactionalMapTestGetCommand method doExecute.
@Override
protected void doExecute() {
StorageService storageService = get(StorageService.class);
TransactionContext context;
context = storageService.transactionContextBuilder().build();
context.begin();
try {
map = context.getTransactionalMap(mapName, serializer);
String response = map.get(key);
context.commit();
if (response == null) {
print("Key %s not found.", key);
} else {
print("Key-value pair (%s, %s) found.", key, response);
}
} catch (Exception e) {
context.abort();
throw e;
}
}
use of org.onosproject.store.service.TransactionContext in project onos by opennetworkinglab.
the class TransactionalMapTestPutCommand method doExecute.
@Override
protected void doExecute() {
StorageService storageService = get(StorageService.class);
TransactionContext context;
context = storageService.transactionContextBuilder().build();
context.begin();
try {
map = context.getTransactionalMap(mapName, serializer);
for (int i = 1; i <= numKeys; i++) {
String key = prefix + i;
String response = map.put(key, value);
if (response == null) {
print("Created Key %s with value %s.", key, value);
} else {
print("Put %s into key %s. The old value was %s.", value, key, response);
}
}
context.commit();
} catch (Exception e) {
context.abort();
throw e;
}
}
Aggregations