use of cz.o2.proxima.direct.view.CachedView in project proxima-platform by O2-Czech-Republic.
the class DirectAttributeFamilyProxyDescriptor method getPartitionedCachedView.
private static Optional<CachedView> getPartitionedCachedView(AttrLookup lookup, Context context, AttributeFamilyProxyDescriptor desc) {
if (desc.getTargetFamilyRead().getAccess().canReadCommitLog() && !desc.getTargetFamilyWrite().getAccess().isReadonly() && desc.getTargetFamilyRead().getAccess().canCreateCachedView()) {
Optional<CommitLogReader> maybeReader = getCommitLogReader(lookup, context, desc);
Optional<OnlineAttributeWriter> maybeWriter = getWriter(lookup, context, desc).map(AttributeWriterBase::online);
if (maybeReader.isPresent() && maybeWriter.isPresent()) {
return Optional.of(new LocalCachedPartitionedView(desc.getTargetFamilyRead().getEntity(), maybeReader.get(), maybeWriter.get()));
}
}
return Optional.empty();
}
use of cz.o2.proxima.direct.view.CachedView in project proxima-platform by O2-Czech-Republic.
the class DirectDataOperator method getCachedView.
/**
* Retrieve {@link CachedView} for given {@link AttributeDescriptor}s.
*
* @param attrs the attributes to find cached view for
* @return optional cached view
*/
public Optional<CachedView> getCachedView(Collection<AttributeDescriptor<?>> attrs) {
boolean hasTransactions = attrs.stream().anyMatch(a -> a.getTransactionMode() != TransactionMode.NONE);
Optional<CachedView> view = getFamilyForAttributes(attrs, DirectAttributeFamilyDescriptor::hasCachedView).flatMap(DirectAttributeFamilyDescriptor::getCachedView);
if (hasTransactions) {
return view.map(v -> new TransactionalCachedView(this, v));
}
return view;
}
use of cz.o2.proxima.direct.view.CachedView in project proxima-platform by O2-Czech-Republic.
the class TransactionResourceManager method writeResponseAndUpdateState.
@Override
public void writeResponseAndUpdateState(String transactionId, State updateState, String responseId, Response response, CommitCallback callback) {
CachedTransaction cachedTransaction = openTransactionMap.get(transactionId);
if (cachedTransaction != null) {
DirectAttributeFamilyDescriptor responseFamily = cachedTransaction.getResponseFamily();
DirectAttributeFamilyDescriptor stateFamily = cachedTransaction.getStateFamily();
final OnlineAttributeWriter writer = cachedTransaction.getCommitWriter();
final CachedView stateView = cachedTransaction.getStateView();
long now = System.currentTimeMillis();
StreamElement stateUpsert = getStateDesc().upsert(transactionId, now, updateState);
Commit commit = Commit.of(Arrays.asList(new Commit.TransactionUpdate(stateFamily.getDesc().getName(), stateUpsert), new Commit.TransactionUpdate(responseFamily.getDesc().getName(), getResponseDesc().upsert(transactionId, responseId, now, response))));
synchronized (stateView) {
ensureTransactionOpen(transactionId, updateState);
stateView.cache(stateUpsert);
}
synchronized (writer) {
writer.write(getCommitDesc().upsert(transactionId, System.currentTimeMillis(), commit), callback);
}
} else {
log.warn("Transaction {} is not open, don't have a writer to return response {}", transactionId, response);
callback.commit(true, null);
}
}
use of cz.o2.proxima.direct.view.CachedView in project proxima-platform by O2-Czech-Republic.
the class DirectDataOperatorTest method testProxyCachedView.
@Test
public void testProxyCachedView() {
EntityDescriptor proxied = repo.getEntity("proxied");
AttributeDescriptor<?> target = proxied.getAttribute("_e.*", true);
AttributeDescriptor<?> source = proxied.getAttribute("event.*");
CachedView view = direct.getFamiliesForAttribute(source).stream().filter(af -> af.getDesc().getAccess().canCreateCachedView()).findAny().flatMap(DirectAttributeFamilyDescriptor::getCachedView).orElseThrow(() -> new IllegalStateException("Missing cached view for " + source));
RandomAccessReader reader = direct.getFamiliesForAttribute(target).stream().filter(af -> af.getDesc().getAccess().canRandomRead()).findAny().flatMap(DirectAttributeFamilyDescriptor::getRandomAccessReader).orElseThrow(() -> new IllegalStateException("Missing random reader for " + target));
view.assign(Collections.singletonList(() -> 0));
long now = System.currentTimeMillis();
StreamElement update = StreamElement.upsert(proxied, source, UUID.randomUUID().toString(), "key", "event.def", now, "test2".getBytes(StandardCharsets.UTF_8));
assertFalse(reader.get("key", target.toAttributePrefix() + "def", target, now).isPresent());
view.write(update, (succ, exc) -> {
});
assertTrue(reader.get("key", target.toAttributePrefix() + "raw-def", target, now).isPresent());
assertTrue(view.get("key", source.toAttributePrefix() + "def", source, now).isPresent());
}
use of cz.o2.proxima.direct.view.CachedView in project proxima-platform by O2-Czech-Republic.
the class InMemStorage method createAccessor.
@Override
public DataAccessor createAccessor(DirectDataOperator op, AttributeFamilyDescriptor familyDescriptor) {
final EntityDescriptor entity = familyDescriptor.getEntity();
final URI uri = familyDescriptor.getStorageUri();
final Map<String, Object> cfg = familyDescriptor.getCfg();
log.info("Creating accessor {} for URI {}", getClass(), uri);
holder().observers.computeIfAbsent(uri, k -> Collections.synchronizedNavigableMap(new TreeMap<>()));
final int numPartitions = Optional.ofNullable(cfg.get(NUM_PARTITIONS)).map(v -> Integer.parseInt(v.toString())).orElse(1);
final Partitioner partitioner = Optional.ofNullable(cfg.get(ConfigConstants.PARTITIONER)).map(name -> Classpath.newInstance(name.toString(), Partitioner.class)).orElseGet(KeyAttributePartitioner::new);
final Repository opRepo = op.getRepository();
final RepositoryFactory repositoryFactory = opRepo.asFactory();
final OnlineAttributeWriter.Factory<?> writerFactory = new Writer(entity, uri, numPartitions, partitioner).asFactory();
final CommitLogReader.Factory<?> commitLogReaderFactory = new InMemCommitLogReader(entity, uri, op.getContext().getExecutorFactory(), partitioner, numPartitions).asFactory();
final RandomAccessReader.Factory<Reader> randomAccessReaderFactory;
final BatchLogReader.Factory<Reader> batchLogReaderFactory;
final CachedView.Factory cachedViewFactory;
if (numPartitions > 1) {
randomAccessReaderFactory = null;
batchLogReaderFactory = null;
cachedViewFactory = null;
} else {
final ReaderFactory readerFactory = new Reader(entity, uri, op.getContext().getExecutorFactory()).asFactory();
randomAccessReaderFactory = readerFactory;
batchLogReaderFactory = readerFactory;
cachedViewFactory = new LocalCachedPartitionedView(entity, commitLogReaderFactory.apply(opRepo), writerFactory.apply(opRepo)).asFactory();
}
return new DataAccessor() {
private static final long serialVersionUID = 1L;
@Nullable
private transient Repository repo = opRepo;
@Override
public URI getUri() {
return uri;
}
@Override
public Optional<AttributeWriterBase> getWriter(Context context) {
Objects.requireNonNull(context);
return Optional.of(writerFactory.apply(repo()));
}
@Override
public Optional<CommitLogReader> getCommitLogReader(Context context) {
Objects.requireNonNull(context);
return Optional.of(commitLogReaderFactory.apply(repo()));
}
@Override
public Optional<RandomAccessReader> getRandomAccessReader(Context context) {
Objects.requireNonNull(context);
return Optional.ofNullable(randomAccessReaderFactory).map(item -> item.apply(repo()));
}
@Override
public Optional<CachedView> getCachedView(Context context) {
Objects.requireNonNull(context);
return Optional.ofNullable(cachedViewFactory).map(item -> item.apply(repo()));
}
@Override
public Optional<BatchLogReader> getBatchLogReader(Context context) {
Objects.requireNonNull(context);
return Optional.ofNullable(batchLogReaderFactory).map(item -> item.apply(repo()));
}
private Repository repo() {
if (this.repo == null) {
this.repo = repositoryFactory.apply();
}
return this.repo;
}
};
}
Aggregations