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;
}
};
}
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());
}
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();
}
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();
}
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);
}
}
};
}
Aggregations