Search in sources :

Example 1 with Builder

use of cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder in project proxima-platform by O2-Czech-Republic.

the class JsonProtoSerializerFactory method createSerializer.

@SuppressWarnings("unchecked")
private static ValueSerializer createSerializer(URI uri) {
    return new ValueSerializer() {

        private static final long serialVersionUID = 1L;

        final String protoClass = uri.getSchemeSpecificPart();

        final boolean strictScheme = Optional.ofNullable(UriUtil.parseQuery(uri).get("strictScheme")).map(Boolean::valueOf).orElse(false);

        @Nullable
        transient AbstractMessage defVal = null;

        @Nullable
        transient Method builder = null;

        @Nullable
        transient JsonFormat.Parser parser = null;

        @Override
        public Optional deserialize(byte[] input) {
            try {
                AbstractMessage.Builder newBuilder = newBuilder(builder());
                if (input.length == 0) {
                    input = new byte[] { '{', '}' };
                }
                parser().merge(new String(input, CHARSET), newBuilder);
                return Optional.of(newBuilder.build());
            } catch (InvalidProtocolBufferException ex) {
                log.warn("Failed to parse input {}", new String(input), ex);
                return Optional.empty();
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public byte[] serialize(Object value) {
            try {
                StringBuilder sb = new StringBuilder();
                JsonFormat.printer().appendTo((MessageOrBuilder) value, sb);
                return sb.toString().getBytes(CHARSET);
            } catch (IOException ex) {
                throw new SerializationException(ex.getMessage(), ex);
            }
        }

        @Override
        public Object getDefault() {
            if (defVal == null) {
                defVal = ProtoSerializerFactory.getDefaultInstance(protoClass);
            }
            return defVal;
        }

        private synchronized Method builder() {
            if (builder == null) {
                builder = getBuilder(protoClass);
            }
            return builder;
        }

        private synchronized JsonFormat.Parser parser() {
            if (parser == null) {
                parser = strictScheme ? JsonFormat.parser() : JsonFormat.parser().ignoringUnknownFields();
            }
            return parser;
        }
    };
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) SerializationException(cz.o2.proxima.scheme.SerializationException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Method(java.lang.reflect.Method) IOException(java.io.IOException) JsonFormat(com.google.protobuf.util.JsonFormat) ValueSerializer(cz.o2.proxima.scheme.ValueSerializer)

Example 2 with Builder

use of cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder in project proxima-platform by O2-Czech-Republic.

the class ConfigRepository method createEntityTransaction.

private void createEntityTransaction(Config transactionsConfig) {
    String schemeProviderScheme = "java";
    if (transactionsConfig.hasPath(SCHEME_PROVIDER)) {
        schemeProviderScheme = transactionsConfig.getString(SCHEME_PROVIDER);
    }
    Optional<ValueSerializerFactory> factory = getValueSerializerFactory(schemeProviderScheme);
    if (!factory.isPresent() || !factory.get().canProvideTransactionSerializer()) {
        throw new IllegalArgumentException("Scheme provider for transactions " + schemeProviderScheme + ", is either missing or unable to provide transaction schemes.");
    }
    TransactionSerializerSchemeProvider schemeProvider = factory.get().createTransactionSerializerSchemeProvider();
    EntityDescriptor.Builder builder = EntityDescriptor.newBuilder().setName(TRANSACTION_ENTITY);
    loadRegular(TRANSACTION_ENTITY, REQUEST_ATTRIBUTE, Collections.singletonMap(SCHEME, schemeProvider.getRequestScheme()), builder);
    loadRegular(TRANSACTION_ENTITY, RESPONSE_ATTRIBUTE, Collections.singletonMap(SCHEME, schemeProvider.getResponseScheme()), builder);
    loadRegular(TRANSACTION_ENTITY, STATE_ATTRIBUTE, Collections.singletonMap(SCHEME, schemeProvider.getStateScheme()), builder);
    loadRegular(TRANSACTION_ENTITY, COMMIT_ATTRIBUTE, Collections.singletonMap(SCHEME, schemeProvider.getCommitScheme()), builder);
    entitiesByName.put(TRANSACTION_ENTITY, builder.build());
}
Also used : ValueSerializerFactory(cz.o2.proxima.scheme.ValueSerializerFactory) TransactionSerializerSchemeProvider(cz.o2.proxima.transaction.TransactionSerializerSchemeProvider)

Example 3 with Builder

use of cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder in project proxima-platform by O2-Czech-Republic.

the class JsonFormat method toJsonElement.

@VisibleForTesting
static JsonElement toJsonElement(StreamElement element) {
    @SuppressWarnings("unchecked") ValueSerializer<Object> serializer = (ValueSerializer<Object>) element.getAttributeDescriptor().getValueSerializer();
    Builder builder = JsonElement.newBuilder().setAttribute(element.getAttribute()).setUuid(element.getUuid()).setStamp(element.getStamp()).setKey(element.getKey());
    if (element.isDeleteWildcard()) {
        return builder.setDeleteWildcard(true).setDelete(true).build();
    }
    if (element.isDelete()) {
        return builder.setDelete(true).build();
    }
    Optional<?> maybeValue = element.getParsed();
    Preconditions.checkArgument(maybeValue.isPresent(), "Cannot deserialize value in [%s]", element);
    return builder.setValue(serializer.asJsonValue(maybeValue.get())).build();
}
Also used : Builder(cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder) ValueSerializer(cz.o2.proxima.scheme.ValueSerializer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Builder

use of cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder in project proxima-platform by O2-Czech-Republic.

the class ProtoMessageValueAccessor method createFrom.

@Override
@SuppressWarnings("unchecked")
public T createFrom(StructureValue input) {
    final Builder builder = getDefaultValue().newBuilderForType();
    for (FieldDescriptor fieldDescriptor : getDefaultValue().getDescriptorForType().getFields()) {
        final String fieldName = fieldDescriptor.getName();
        if (input.containsKey(fieldName)) {
            final AttributeValueAccessor<Object, Object> fieldAccessor = (AttributeValueAccessor<Object, Object>) fieldAccessors.get(fieldName);
            Preconditions.checkNotNull(fieldAccessor, "Unable to get accessor for field %s.", fieldName);
            final Object value = prepareFieldValue(fieldAccessor, input.get(fieldName));
            if (fieldDescriptor.isRepeated()) {
                final List<Object> listValues = (List<Object>) fieldAccessor.createFrom(value);
                listValues.forEach(v -> builder.addRepeatedField(fieldDescriptor, v));
            } else {
                if (List.class.isAssignableFrom(value.getClass())) {
                    builder.setField(fieldDescriptor, fieldAccessor.createFrom(Iterables.getLast((List<?>) value)));
                } else {
                    builder.setField(fieldDescriptor, fieldAccessor.createFrom(value));
                }
            }
        }
    }
    return (T) builder.build();
}
Also used : Builder(com.google.protobuf.Message.Builder) AttributeValueAccessor(cz.o2.proxima.scheme.AttributeValueAccessor) List(java.util.List) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 5 with Builder

use of cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder in project proxima-platform by O2-Czech-Republic.

the class MultiAccessBuilder method build.

/**
 * Create {@link RandomAccessReader} for attributes and/or families specified in this builder.
 *
 * @return {@link RandomAccessReader} capable of reading from multiple attribute families.
 */
public RandomAccessReader build() {
    final Map<AttributeDescriptor<?>, RandomAccessReader> attrMapToReader = materializeReaders(repo);
    final EntityDescriptor entity = getSingleEntityOrNull(attrMapToReader);
    return new RandomAccessReader() {

        @Override
        public RandomOffset fetchOffset(RandomAccessReader.Listing type, String key) {
            if (type == Listing.ENTITY) {
                throw new UnsupportedOperationException("Please use specific attribute family to scan entities.");
            }
            Map<RandomAccessReader, RandomOffset> offsets = attrMapToReader.values().stream().distinct().map(ra -> Pair.of(ra, ra.fetchOffset(type, key))).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
            return new SequentialOffset(offsets);
        }

        @Override
        public <T> Optional<KeyValue<T>> get(String key, String attribute, AttributeDescriptor<T> desc, long stamp) {
            return Optional.ofNullable(attrMapToReader.get(desc)).map(ra -> ra.get(key, attribute, desc, stamp)).orElseGet(() -> {
                log.warn("Missing family for attribute {} in MultiAccessBuilder", desc);
                return Optional.empty();
            });
        }

        @SuppressWarnings("unchecked")
        @Override
        public void scanWildcardAll(String key, RandomOffset offset, long stamp, int limit, Consumer<KeyValue<?>> consumer) {
            AtomicInteger missing = new AtomicInteger(limit);
            SequentialOffset soff = (SequentialOffset) offset;
            final AtomicReference<SequentialOffset> current = new AtomicReference<>();
            if (soff != null) {
                current.set(new SequentialOffset(soff));
            } else {
                Map<RandomAccessReader, RandomOffset> m = new HashMap<>();
                attrMapToReader.values().stream().distinct().forEach(ra -> m.put(ra, null));
                current.set(new SequentialOffset(m));
            }
            current.get().offsetMap.entrySet().forEach(e -> e.getKey().scanWildcardAll(key, e.getValue(), stamp, missing.get(), kv -> {
                missing.decrementAndGet();
                current.set(new SequentialOffset(current.get()).update(e.getKey(), kv.getOffset()));
                KeyValue<?> mapped = KeyValue.of(kv.getEntityDescriptor(), (AttributeDescriptor) kv.getAttributeDescriptor(), kv.getKey(), kv.getAttribute(), current.get(), kv.getValue(), kv.getValue(), kv.getStamp());
                consumer.accept(mapped);
            }));
        }

        @Override
        public <T> void scanWildcard(String key, AttributeDescriptor<T> wildcard, RandomOffset offset, long stamp, int limit, Consumer<KeyValue<T>> consumer) {
            Optional.ofNullable(attrMapToReader.get(wildcard)).ifPresent(ra -> ra.scanWildcard(key, wildcard, offset, stamp, limit, consumer));
        }

        @Override
        public void listEntities(RandomOffset offset, int limit, Consumer<Pair<RandomOffset, String>> consumer) {
            throw new UnsupportedOperationException("Not supported. Please select specific family to list entities from.");
        }

        @Override
        public EntityDescriptor getEntityDescriptor() {
            if (entity != null) {
                return entity;
            }
            throw new IllegalArgumentException("Multiple options. This is compound reader that can work " + "on multiple entities.");
        }

        @Override
        public Factory<?> asFactory() {
            return repo -> {
                MultiAccessBuilder builder = new MultiAccessBuilder(repo, context);
                attrMapToFactory.forEach((attr, factory) -> builder.addAttributes(factory.apply(repo), attr));
                return builder.build();
            };
        }

        @Override
        public void close() {
            attrMapToReader.values().forEach(this::closeQuietly);
        }

        private void closeQuietly(RandomAccessReader c) {
            try {
                c.close();
            } catch (IOException ex) {
                log.warn("Failed to close {}", c, ex);
            }
        }
    };
}
Also used : Context(cz.o2.proxima.direct.core.Context) Iterables(com.google.common.collect.Iterables) Consumer(cz.o2.proxima.functional.Consumer) Repository(cz.o2.proxima.repository.Repository) Stable(cz.o2.proxima.annotations.Stable) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Set(java.util.Set) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) IOException(java.io.IOException) HashMap(java.util.HashMap) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Pair(cz.o2.proxima.util.Pair) Map(java.util.Map) Optional(java.util.Optional) Nullable(javax.annotation.Nullable) HashMap(java.util.HashMap) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Consumer(cz.o2.proxima.functional.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

ValueSerializer (cz.o2.proxima.scheme.ValueSerializer)2 IOException (java.io.IOException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Iterables (com.google.common.collect.Iterables)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 ByteString (com.google.protobuf.ByteString)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Builder (com.google.protobuf.Message.Builder)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 Stable (cz.o2.proxima.annotations.Stable)1 Context (cz.o2.proxima.direct.core.Context)1 Consumer (cz.o2.proxima.functional.Consumer)1 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)1 AttributeFamilyDescriptor (cz.o2.proxima.repository.AttributeFamilyDescriptor)1 Builder (cz.o2.proxima.repository.AttributeFamilyDescriptor.Builder)1 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)1 Repository (cz.o2.proxima.repository.Repository)1 AttributeValueAccessor (cz.o2.proxima.scheme.AttributeValueAccessor)1 SerializationException (cz.o2.proxima.scheme.SerializationException)1