All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ytsaurus.client.request.SerializationContext Maven / Gradle / Ivy

The newest version!
package tech.ytsaurus.client.request;

import java.util.Optional;

import javax.annotation.Nullable;

import com.google.protobuf.Message;
import tech.ytsaurus.client.TableAttachmentReader;
import tech.ytsaurus.client.rows.EntitySkiffSerializer;
import tech.ytsaurus.client.rows.WireRowSerializer;
import tech.ytsaurus.core.rows.YTreeRowSerializer;
import tech.ytsaurus.core.rows.YTreeSerializer;
import tech.ytsaurus.core.utils.ProtoUtils;
import tech.ytsaurus.rpcproxy.ERowsetFormat;

import static tech.ytsaurus.client.rows.EntityUtil.isEntityAnnotationPresent;
import static tech.ytsaurus.core.utils.ClassUtils.castToType;

public class SerializationContext {
    @Nullable
    protected WireRowSerializer wireSerializer;
    @Nullable
    protected YTreeSerializer ytreeSerializer;
    @Nullable
    private EntitySkiffSerializer skiffSerializer;
    @Nullable
    private Class objectClass;
    protected ERowsetFormat rowsetFormat = ERowsetFormat.RF_YT_WIRE;
    @Nullable
    protected Format format = null;
    @Nullable
    protected TableAttachmentReader attachmentReader = null;

    protected SerializationContext() {
    }

    public SerializationContext(YTreeSerializer serializer, Format format) {
        if (!(serializer instanceof YTreeRowSerializer)) {
            throw new IllegalArgumentException("YTreeRowSerializer was expected");
        }
        this.ytreeSerializer = serializer;
        this.rowsetFormat = ERowsetFormat.RF_FORMAT;
        this.format = format;
    }

    public SerializationContext(Class objectClass) {
        this.objectClass = objectClass;
        if (isEntityAnnotationPresent(objectClass)) {
            this.skiffSerializer = new EntitySkiffSerializer<>(objectClass);
            this.rowsetFormat = ERowsetFormat.RF_FORMAT;
            this.attachmentReader = TableAttachmentReader.skiff(skiffSerializer);
            return;
        }
        if (Message.class.isAssignableFrom(objectClass)) {
            Message.Builder messageBuilder = ProtoUtils.newBuilder(castToType(objectClass));
            this.rowsetFormat = ERowsetFormat.RF_FORMAT;
            this.format = Format.protobuf(messageBuilder);
            this.attachmentReader = castToType(TableAttachmentReader.protobuf(messageBuilder));
        }
    }

    public SerializationContext(YTreeSerializer serializer) {
        this.ytreeSerializer = serializer;
    }

    public Optional> getWireSerializer() {
        return Optional.ofNullable(this.wireSerializer);
    }

    public Optional> getYtreeSerializer() {
        return Optional.ofNullable(this.ytreeSerializer);
    }

    public Optional> getSkiffSerializer() {
        return Optional.ofNullable(this.skiffSerializer);
    }

    public Optional> getObjectClass() {
        return Optional.ofNullable(this.objectClass);
    }

    public ERowsetFormat getRowsetFormat() {
        return rowsetFormat;
    }

    public Optional getFormat() {
        if (skiffSerializer != null) {
            return skiffSerializer.getFormat();
        }
        return Optional.ofNullable(format);
    }

    public Optional> getAttachmentReader() {
        return Optional.ofNullable(attachmentReader);
    }

    public boolean isProtobufFormat() {
        return objectClass != null && Message.class.isAssignableFrom(objectClass);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy