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

com.huaweicloud.dws.client.op.Get Maven / Gradle / Ivy


package com.huaweicloud.dws.client.op;

import com.huaweicloud.dws.client.action.GetAction;
import com.huaweicloud.dws.client.collector.ActionCollector;
import com.huaweicloud.dws.client.exception.DwsClientException;
import com.huaweicloud.dws.client.exception.ExceptionCode;
import com.huaweicloud.dws.client.exception.InvalidException;
import com.huaweicloud.dws.client.model.Column;
import com.huaweicloud.dws.client.model.OperationType;
import com.huaweicloud.dws.client.model.Record;
import com.huaweicloud.dws.client.model.TableSchema;
import com.huaweicloud.dws.client.util.AssertUtil;
import com.huaweicloud.dws.client.util.LogUtil;
import com.huaweicloud.dws.client.worker.ExecutionPool;

import lombok.extern.slf4j.Slf4j;

import java.security.InvalidParameterException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @ProjectName: dws-client
 * @Description: Get operation
 * schema 主键必须与 lookup keys 一一对应
 * like:
 * primaryKeys = [id, name]
 * lookupKeys = [id, name]
 * @Date: 2023/09/27
 */
@Slf4j
public class Get extends Operate {
    CompletableFuture future = new CompletableFuture<>();

    // get操作提交到dws的时间
    long startTime = System.nanoTime();

    boolean isFullColumn;

    public Get(TableSchema schema, ActionCollector collector, boolean isFullColumn) {
        super(schema, collector, OperationType.READ);
        this.isFullColumn = isFullColumn;
    }

    public Get(Record record, ActionCollector collector, boolean isFullColumn) {
        super(record, collector);
        this.isFullColumn = isFullColumn;
    }

    public boolean isFullColumn() {
        return isFullColumn;
    }

    public long getStartTime() {
        return startTime;
    }

    public CompletableFuture getFuture() {
        return future;
    }

    public static Builder newBuilder(TableSchema schema, ActionCollector collector) {
        return new Builder(schema, collector);
    }

    public static void check(Get get) throws DwsClientException {
        if (get == null) {
            throw new DwsClientException(ExceptionCode.CONSTRAINT_VIOLATION, "Get cannot be null");
        }
        if (get.getRecord().getTableSchema().getPrimaryKeys().size() == 0) {
            throw new DwsClientException(ExceptionCode.CONSTRAINT_VIOLATION, "Get table must have primary key:" + get.getRecord().getTableSchema().getTableName().getFullName());
        }

        for (int index : get.getRecord().getTableSchema().getKeyIndexList()) {
            if (get.getRecord().getValue(index) == null || !get.getRecord().isSet(index)) {
                throw new DwsClientException(ExceptionCode.CONSTRAINT_VIOLATION, "Get primary key cannot be null:" + get.getRecord().getTableSchema().getColumns().get(index).getName());
            }
        }
    }

    @Override
    public void commit() throws DwsClientException {
        AssertUtil.isTrue(record.getColumnBit().length() != 0, new InvalidException("must set column"));
        ((ActionCollector) collector).appendGet(this);
        collector.getPool().tryException();
    }

    /**
     * 同步提交
     */
    @Override
    public void syncCommit() throws DwsClientException {
        List getList = Collections.singletonList(this);
        GetAction action = new GetAction(getList, collector.getConfig());
        ExecutionPool pool = collector.getPool();
        pool.tryException();
        while (!collector.getPool().submit(action)) {
            LogUtil.withLogSwitch(collector.getConfig(), () -> log.warn("try submit."));
        }
        // 等待结果响应后返回
        action.getResult();
    }

    /**
     * Builder of Get request.
     */
    public static class Builder {
        private final TableSchema schema;

        private final Record record;

        private final ActionCollector collector;

        private boolean isFullColumn = true;

        public Builder(TableSchema schema, ActionCollector collector) {
            this.schema = schema;
            this.collector = collector;
            this.record = new Record(schema, OperationType.READ);
        }

        public Builder setPrimaryKeys(List columns, List values) {
            for (int i = 0; i < columns.size(); i++) {
                this.setPrimaryKey(columns.get(i), values.get(i));
            }
            return this;
        }

        public Builder setPrimaryKey(String columnName, Object value) {
            Integer index = schema.getColumnIndex(columnName);
            if (index == null) {
                throw new InvalidParameterException(
                    String.format("Table %s does not have column %s.", schema.getTableName(), columnName));
            }

            if (!schema.isPrimaryKey(columnName)) {
                throw new InvalidParameterException(String.format("Column %s is not primary key.", columnName));
            }
            if (value == null) {
                throw new InvalidParameterException("Primary key should not be null.");
            }

            record.setValue(index, value);
            return this;
        }

        public Builder withSelectedColumns(String[] columns) {
            for (String columnName : columns) {
                this.withSelectedColumn(columnName);
            }
            return this;
        }

        public Builder withSelectedColumn(String columnName) {
            Integer index = schema.getColumnIndex(columnName);
            if (index == null) {
                throw new InvalidParameterException(
                    String.format("Table %s does not have column %s.", schema.getTableName(), columnName));
            }
            if (!record.isSet(columnName)) {
                record.setValue(index, null);
            }
            this.isFullColumn = false;
            return this;
        }

        public Get build() {
            for (Column column : schema.getPrimaryKeys()) {
                if (record.getValue(column.getName()) == null) {
                    throw new InvalidParameterException("Primary key is not all set.");
                }
            }
            return new Get(record, collector, isFullColumn);
        }
    }
}