Search in sources :

Example 26 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class CassandraLogReader method processSinglePartition.

private boolean processSinglePartition(CassandraPartition partition, List<AttributeDescriptor<?>> attributes, TerminationContext terminationContext, BatchLogObserver observer) {
    ResultSet result;
    try (ClusterHolder holder = accessor.acquireCluster()) {
        Session session = accessor.ensureSession();
        result = accessor.execute(accessor.getCqlFactory().scanPartition(attributes, partition, session));
        for (Row row : result) {
            if (terminationContext.isCancelled()) {
                return false;
            }
            String key = row.getString(0);
            int field = 1;
            for (AttributeDescriptor<?> attribute : attributes) {
                String attributeName = attribute.getName();
                if (attribute.isWildcard()) {
                    // FIXME: this is wrong
                    // need mapping between attribute and accessor
                    String suffix = accessor.asString(row.getObject(field++));
                    attributeName = attribute.toAttributePrefix() + suffix;
                }
                ByteBuffer bytes = row.getBytes(field++);
                if (bytes != null) {
                    byte[] array = bytes.slice().array();
                    StreamElement element = accessor.getCqlFactory().toKeyValue(accessor.getEntityDescriptor(), attribute, key, attributeName, System.currentTimeMillis(), Offsets.empty(), array);
                    if (!observer.onNext(element, BatchLogObservers.defaultContext(partition))) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) StreamElement(cz.o2.proxima.storage.StreamElement) Row(com.datastax.driver.core.Row) ClusterHolder(cz.o2.proxima.direct.cassandra.CassandraDBAccessor.ClusterHolder) ByteBuffer(java.nio.ByteBuffer) Session(com.datastax.driver.core.Session)

Example 27 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class ConfigRepository method loadSingleFamily.

private void loadSingleFamily(String name, boolean overwrite, Map<String, Object> cfg) throws URISyntaxException {
    cfg = flatten(cfg);
    boolean isDisabled = Optional.ofNullable(cfg.get(DISABLED)).map(Object::toString).map(Boolean::valueOf).orElse(false);
    if (isDisabled) {
        log.info("Skipping load of disabled family {}", name);
        return;
    }
    final String entity = Objects.requireNonNull(cfg.get(ENTITY)).toString();
    final String filter = toString(cfg.get(FILTER));
    final boolean isTransactional = entity.equals(TRANSACTION_ENTITY);
    final StorageType type = getStorageType(cfg, isTransactional);
    final AccessType access = getAccessType(cfg, isTransactional);
    final List<String> attributes = toList(Objects.requireNonNull(cfg.get(ATTRIBUTES), () -> String.format("Missing required field `%s' in attributeFamily %s", ATTRIBUTES, name)));
    final URI storageUri = new URI(Objects.requireNonNull(cfg.get(STORAGE), () -> String.format("Missing required field `%s' in attribute family %s", STORAGE, name)).toString());
    final EntityDescriptor entDesc = findEntityRequired(entity);
    AttributeFamilyDescriptor.Builder family = AttributeFamilyDescriptor.newBuilder().setEntity(entDesc).setName(name).setType(type).setAccess(access).setStorageUri(storageUri).setCfg(withTransactionalPartitioner(isTransactional, cfg)).setSource((String) cfg.get(FROM));
    Collection<AttributeDescriptor<?>> allAttributes = new HashSet<>();
    for (String attr : attributes) {
        // attribute descriptors affected by this settings
        final List<AttributeDescriptor<?>> attrDescs;
        attrDescs = searchAttributesMatching(attr, entDesc, false, false);
        allAttributes.addAll(attrDescs);
    }
    if (!filter.isEmpty() && !readonly) {
        insertFilterIfPossible(allAttributes, type, filter, name, family);
    }
    allAttributes.forEach(family::addAttribute);
    insertFamily(family.build(), overwrite);
}
Also used : StorageType(cz.o2.proxima.storage.StorageType) URI(java.net.URI) ConfigObject(com.typesafe.config.ConfigObject) AccessType(cz.o2.proxima.storage.AccessType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 28 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class ConfigRepository method readTransformations.

private void readTransformations(Config cfg) {
    if (entitiesByName.isEmpty()) {
        // no loaded entities, no more stuff to read
        return;
    }
    Map<String, Object> cfgTransforms = Optional.ofNullable(cfg.root().get("transformations")).map(v -> toMap("transformations", v.unwrapped())).orElse(null);
    if (cfgTransforms == null) {
        log.info("Skipping empty transformations configuration.");
        return;
    }
    cfgTransforms.forEach((name, v) -> {
        Map<String, Object> transformation = toMap(name, v);
        boolean disabled = Optional.ofNullable(transformation.get(DISABLED)).map(d -> Boolean.valueOf(d.toString())).orElse(false);
        if (disabled) {
            log.info("Skipping load of disabled transformation {}", name);
            return;
        }
        EntityDescriptor entity = findEntity(readStr(ENTITY, transformation, name)).orElseThrow(() -> new IllegalArgumentException(String.format("Entity `%s` doesn't exist", transformation.get(ENTITY))));
        Transformation t = Classpath.newInstance(readStr("using", transformation, name), Transformation.class);
        List<AttributeDescriptor<?>> attrs = readList(ATTRIBUTES, transformation, name).stream().flatMap(a -> searchAttributesMatching(a, entity, true, true).stream()).collect(Collectors.toList());
        TransformationDescriptor.Builder desc = TransformationDescriptor.newBuilder().setName(name).addAttributes(attrs).setTransformation(t);
        Optional.ofNullable(transformation.get(FILTER)).map(Object::toString).map(s -> Classpath.newInstance(s, StorageFilter.class)).ifPresent(desc::setFilter);
        TransformationDescriptor transformationDescriptor = desc.build();
        setupTransform(transformationDescriptor.getTransformation(), transformation);
        this.transformations.put(name, transformationDescriptor);
    });
}
Also used : Arrays(java.util.Arrays) ConfigConstants(cz.o2.proxima.repository.ConfigConstants) URISyntaxException(java.net.URISyntaxException) ObjectInputStream(java.io.ObjectInputStream) StorageType(cz.o2.proxima.storage.StorageType) Pair(cz.o2.proxima.util.Pair) UnaryFunction(cz.o2.proxima.functional.UnaryFunction) Map(java.util.Map) AccessType(cz.o2.proxima.storage.AccessType) URI(java.net.URI) TransactionSerializerSchemeProvider(cz.o2.proxima.transaction.TransactionSerializerSchemeProvider) Transformation(cz.o2.proxima.transform.Transformation) BiFunction(cz.o2.proxima.functional.BiFunction) ConfigValue(com.typesafe.config.ConfigValue) Collection(java.util.Collection) ConfigObject(com.typesafe.config.ConfigObject) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) TransactionPartitioner(cz.o2.proxima.transaction.TransactionPartitioner) Objects(java.util.Objects) ValueSerializer(cz.o2.proxima.scheme.ValueSerializer) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) Optional(java.util.Optional) RenameTransformation(cz.o2.proxima.transform.RenameTransformation) Pattern(java.util.regex.Pattern) VersionedCaching(cz.o2.proxima.repository.RepositoryFactory.VersionedCaching) Iterables(com.google.common.collect.Iterables) Getter(lombok.Getter) HashMap(java.util.HashMap) CamelCase(cz.o2.proxima.util.CamelCase) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Value(lombok.Value) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ConfigFactory(com.typesafe.config.ConfigFactory) ObjectOutputStream(java.io.ObjectOutputStream) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Classpath(cz.o2.proxima.util.Classpath) Config(com.typesafe.config.Config) ElementWiseProxyTransform(cz.o2.proxima.transform.ElementWiseProxyTransform) ProxyTransform(cz.o2.proxima.transform.ProxyTransform) StorageFilter(cz.o2.proxima.storage.StorageFilter) IOException(java.io.IOException) LocalInstance(cz.o2.proxima.repository.RepositoryFactory.LocalInstance) ObjectStreamException(java.io.ObjectStreamException) DataOperatorAware(cz.o2.proxima.transform.DataOperatorAware) ProxySetupContext(cz.o2.proxima.transform.ElementWiseProxyTransform.ProxySetupContext) ValueSerializerFactory(cz.o2.proxima.scheme.ValueSerializerFactory) TransactionTransformProvider(cz.o2.proxima.transaction.TransactionTransformProvider) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Transformation(cz.o2.proxima.transform.Transformation) RenameTransformation(cz.o2.proxima.transform.RenameTransformation) ConfigObject(com.typesafe.config.ConfigObject)

Example 29 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class ConfigRepositoryTest method testProxySetupContext.

@Test
public void testProxySetupContext() {
    EntityDescriptor entity = repo.getEntity("proxied");
    AttributeDescriptor<Object> attribute = entity.getAttribute("raw.*");
    AttributeProxyDescriptor<Object> proxy = entity.getAttribute("event.*").asProxy();
    ProxySetupContext context = ConfigRepository.asProxySetupContext(proxy, attribute, true, false);
    assertTrue(context.isReadTransform());
    assertFalse(context.isWriteTransform());
    assertFalse(context.isSymmetric());
    assertEquals(attribute, context.getTargetAttribute());
    assertEquals(proxy, context.getProxyAttribute());
}
Also used : ProxySetupContext(cz.o2.proxima.transform.ElementWiseProxyTransform.ProxySetupContext) Test(org.junit.Test)

Example 30 with AttributeDescriptor

use of cz.o2.proxima.repository.AttributeDescriptor in project proxima-platform by O2-Czech-Republic.

the class StreamElement method dump.

/**
 * Dump in more verbose way into given stream.
 *
 * @return string representing the dumped element
 */
public String dump() {
    @SuppressWarnings("unchecked") AttributeDescriptor<Object> attrDesc = (AttributeDescriptor<Object>) getAttributeDescriptor();
    Optional<Object> parsedValue = getParsed();
    return MoreObjects.toStringHelper(getClass()).add("uuid", uuid).add("entityDesc", entityDescriptor).add("attributeDesc", attributeDescriptor).add("key", key).add("attribute", attribute).add("stamp", new Date(stamp)).add("value", parsedValue.isPresent() ? attrDesc.getValueSerializer().getLogString(parsedValue.get()) : "(null)").toString();
}
Also used : AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Date(java.util.Date)

Aggregations

EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)71 Test (org.junit.Test)59 StreamElement (cz.o2.proxima.storage.StreamElement)51 ArrayList (java.util.ArrayList)46 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 CountDownLatch (java.util.concurrent.CountDownLatch)33 List (java.util.List)29 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)24 Slf4j (lombok.extern.slf4j.Slf4j)24 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)22 Map (java.util.Map)22 Collectors (java.util.stream.Collectors)22 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)21 RandomAccessReader (cz.o2.proxima.direct.randomaccess.RandomAccessReader)21 AttributeFamilyDescriptor (cz.o2.proxima.repository.AttributeFamilyDescriptor)20 Arrays (java.util.Arrays)20 Collections (java.util.Collections)20 Optional (java.util.Optional)20 Set (java.util.Set)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20