syntax = "proto2";
package com.aliyun.tablestore.protocol;

message Error {
    required string code = 1;
    optional string message = 2;
}

enum PrimaryKeyType {
    INTEGER = 1;
    STRING = 2;
    BINARY = 3;
}

enum DefinedColumnType {
    DCT_INTEGER = 1;
    DCT_DOUBLE = 2;
    DCT_BOOLEAN = 3;
    DCT_STRING = 4;
    DCT_BLOB = 7;
}

enum PrimaryKeyOption {
    AUTO_INCREMENT = 1;
}

message PrimaryKeySchema {
    required string name = 1;
    required PrimaryKeyType type = 2;
    optional PrimaryKeyOption option = 3; 
}

message DefinedColumnSchema {
    required string name = 1;
    required DefinedColumnType type = 2;
}

message TableOptions {
    optional int32 time_to_live = 1;
    optional int32 max_versions = 2;
    optional int64 deviation_cell_version_in_sec = 5;
    optional bool allow_update = 6;
}

enum IndexUpdateMode {
    IUM_ASYNC_INDEX = 0;
    IUM_SYNC_INDEX = 1;
}

enum IndexType {
    IT_GLOBAL_INDEX = 0;
    IT_LOCAL_INDEX = 1;
}

message IndexMeta {
    required string name = 1;
    repeated string primary_key = 2;
    repeated string defined_column = 3;
    required IndexUpdateMode index_update_mode = 4;
    required IndexType index_type = 5;
}

message TableMeta {
    required string table_name = 1;
    repeated PrimaryKeySchema primary_key = 2;
    repeated DefinedColumnSchema defined_column = 3;
}

enum RowExistenceExpectation {
    IGNORE = 0;
    EXPECT_EXIST = 1;
    EXPECT_NOT_EXIST = 2;
}

message Condition {
    required RowExistenceExpectation row_existence = 1;
    optional bytes column_condition      = 2;
}

message CapacityUnit {
    optional int32 read = 1;
    optional int32 write = 2;
}

message ReservedThroughputDetails {
    required CapacityUnit capacity_unit = 1;
    required int64 last_increase_time = 2;
    optional int64 last_decrease_time = 3;
}

message ReservedThroughput {
    required CapacityUnit capacity_unit = 1;
}

message ConsumedCapacity {
    required CapacityUnit capacity_unit = 1;
}

message StreamSpecification {
    required bool enable_stream = 1;
    optional int32 expiration_time = 2;
}

message StreamDetails {
    required bool enable_stream = 1;
    optional string stream_id = 2;
    optional int32 expiration_time = 3;
    optional int64 last_enable_time = 4;
}

enum SSEKeyType {
    SSE_KMS_SERVICE = 1;
    SSE_BYOK = 2;
}

message SSESpecification {
    required bool enable = 1;
    optional SSEKeyType key_type = 2;
    optional bytes key_id = 3; // only useful when key_type is SSE_BYOK
    optional bytes role_arn = 4; // only useful when key_type is SSE_BYOK
}

message SSEDetails {
    required bool enable = 1;
    optional SSEKeyType key_type = 2;
    optional bytes key_id = 3;
    optional bytes role_arn = 4; // only useful when key_type is SSE_BYOK
}

message CreateTableRequest {
    required TableMeta table_meta = 1;
    required ReservedThroughput reserved_throughput = 2; 
    optional TableOptions table_options = 3;
    optional StreamSpecification stream_spec = 5;
    optional SSESpecification sse_spec = 6;
    repeated IndexMeta index_metas = 7;
}

message CreateTableResponse {
}

message CreateIndexRequest {
    required string main_table_name = 1;
    required IndexMeta index_meta = 2;
    optional bool include_base_data = 3;
}

message CreateIndexResponse {
}

message DropIndexRequest {
    required string main_table_name = 1;
    required string index_name = 2;
}

message DropIndexResponse {
}

message UpdateTableRequest {
    required string table_name = 1;
    optional ReservedThroughput reserved_throughput = 2;
    optional TableOptions table_options = 3;
    optional StreamSpecification stream_spec = 4;
}

message UpdateTableResponse {
    required ReservedThroughputDetails reserved_throughput_details = 1;
    required TableOptions table_options = 2;
    optional StreamDetails stream_details = 3;
}
message DescribeTableRequest {
    required string table_name = 1;
}

message DescribeTableResponse {
    required TableMeta table_meta = 1;
    required ReservedThroughputDetails reserved_throughput_details = 2;
    required TableOptions table_options = 3;
    optional StreamDetails stream_details = 5;
    repeated bytes shard_splits = 6;
    optional SSEDetails sse_details = 7;
    repeated IndexMeta index_metas = 8;
}
message ListTableRequest {
}

message ListTableResponse {
    repeated string table_names = 1;
}
message DeleteTableRequest {
    required string table_name = 1;
}

message DeleteTableResponse {
}
message LoadTableRequest {
    required string table_name = 1;
}

message LoadTableResponse {
}
message UnloadTableRequest {
    required string table_name = 1;
}

message UnloadTableResponse {

}
message TimeRange {
    optional int64 start_time = 1;
    optional int64 end_time = 2;
    optional int64 specific_time = 3;
}

enum ReturnType {
    RT_NONE = 0;
    RT_PK = 1;
    RT_AFTER_MODIFY = 2;
}

message ReturnContent {
    optional ReturnType return_type = 1;
    repeated string return_column_names = 2;
} 

message GetRowRequest {
    required string table_name = 1;
    required bytes primary_key = 2; 
    repeated string columns_to_get = 3; 
    optional TimeRange time_range = 4;
    optional int32 max_versions = 5;
    optional bytes filter = 7;
    optional string start_column = 8;
    optional string end_column = 9;
    optional bytes token = 10;
    optional string transaction_id = 11;
}

message GetRowResponse {
    required ConsumedCapacity consumed = 1;
    required bytes row = 2; 
    optional bytes next_token = 3;
}
message UpdateRowRequest {
    required string table_name = 1;
    required bytes row_change = 2;
    required Condition condition = 3;
    optional ReturnContent return_content = 4;
    optional string transaction_id = 5;
}

message UpdateRowResponse {
    required ConsumedCapacity consumed = 1;
    optional bytes row = 2;
}
message PutRowRequest {
    required string table_name = 1;
    required bytes row = 2; 
    required Condition condition = 3;
    optional ReturnContent return_content = 4;
    optional string transaction_id = 5;
}

message PutRowResponse {
    required ConsumedCapacity consumed = 1;
    optional bytes row = 2;
}
message DeleteRowRequest {
    required string table_name = 1;
    required bytes primary_key = 2; 
    required Condition condition = 3;
    optional ReturnContent return_content = 4;
    optional string transaction_id = 5;
}

message DeleteRowResponse {
    required ConsumedCapacity consumed = 1;
    optional bytes row = 2;
}
message TableInBatchGetRowRequest {
    required string table_name = 1;
    repeated bytes primary_key = 2; 
    repeated bytes token = 3;
    repeated string columns_to_get = 4; 
    optional TimeRange time_range = 5;
    optional int32 max_versions = 6;
    optional bytes filter = 8;
    optional string start_column = 9;
    optional string end_column = 10;
}

message BatchGetRowRequest {
    repeated TableInBatchGetRowRequest tables = 1;
}

message RowInBatchGetRowResponse {
    required bool is_ok = 1;
    optional Error error = 2;
    optional ConsumedCapacity consumed = 3;
    optional bytes row = 4; 
    optional bytes next_token = 5;
}

message TableInBatchGetRowResponse {
    required string table_name = 1;
    repeated RowInBatchGetRowResponse rows = 2;
}

message BatchGetRowResponse {
    repeated TableInBatchGetRowResponse tables = 1;
}

enum OperationType {
    PUT = 1;
    UPDATE = 2;
    DELETE = 3;
}

message RowInBatchWriteRowRequest {
    required OperationType type = 1;
    required bytes row_change = 2; 
    required Condition condition = 3;
    optional ReturnContent return_content = 4; 
}

message TableInBatchWriteRowRequest {
    required string table_name = 1;
    repeated RowInBatchWriteRowRequest rows = 2;
}

message BatchWriteRowRequest {
    repeated TableInBatchWriteRowRequest tables = 1;
    optional string transaction_id = 2;
}

message RowInBatchWriteRowResponse {
    required bool is_ok = 1;
    optional Error error = 2;
    optional ConsumedCapacity consumed = 3;
    optional bytes row = 4; 
}

message TableInBatchWriteRowResponse {
    required string table_name = 1;
    repeated RowInBatchWriteRowResponse rows = 2;
}

message BatchWriteRowResponse {
    repeated TableInBatchWriteRowResponse tables = 1;
}
enum Direction {
    FORWARD = 0;
    BACKWARD = 1;
}

message GetRangeRequest {
    required string table_name = 1;
    required Direction direction = 2;
    repeated string columns_to_get = 3; 
    optional TimeRange time_range = 4;
    optional int32 max_versions = 5;
    optional int32 limit = 6;
    required bytes inclusive_start_primary_key = 7; 
    required bytes exclusive_end_primary_key = 8; 
    optional bytes filter = 10;
    optional string start_column = 11;
    optional string end_column = 12;
    optional bytes token = 13;
    optional string transaction_id = 14;
}

message GetRangeResponse {
    required ConsumedCapacity consumed = 1;
    required bytes rows = 2; 
    optional bytes next_start_primary_key = 3;
    optional bytes next_token = 4;
}

message StartLocalTransactionRequest {
    required string table_name = 1;
    required bytes key = 2; 
}

message StartLocalTransactionResponse {
    required string transaction_id = 1;
};

message CommitTransactionRequest {
    required string transaction_id = 1;
}

message CommitTransactionResponse {
};

message AbortTransactionRequest {
    required string transaction_id = 1;
}

message AbortTransactionResponse {
};


message ListStreamRequest {
    optional string table_name = 1;
}

message Stream {
    required string stream_id = 1;
    required string table_name = 2;
    required int64 creation_time = 3;
}

message ListStreamResponse {
    repeated Stream streams = 1;
}

message StreamShard {
    required string shard_id = 1;
    optional string parent_id = 2;
    optional string parent_sibling_id = 3;
}

enum StreamStatus {
    STREAM_ENABLING = 1;
    STREAM_ACTIVE = 2;
}

message DescribeStreamRequest {
    required string stream_id = 1;
    optional string inclusive_start_shard_id = 2;
    optional int32 shard_limit = 3;
}

message DescribeStreamResponse {
    required string stream_id = 1;
    required int32 expiration_time = 2;
    required string table_name = 3;
    required int64 creation_time = 4;
    required StreamStatus stream_status = 5;
    repeated StreamShard shards = 6;
    optional string next_shard_id = 7;
}

message GetShardIteratorRequest {
    required string stream_id = 1;
    required string shard_id = 2;
}

message GetShardIteratorResponse {
    required string shard_iterator = 1;
}

message GetStreamRecordRequest {
    required string shard_iterator = 1;
    optional int32 limit = 2;
}

enum ActionType {
    PUT_ROW = 1;
    UPDATE_ROW = 2;
    DELETE_ROW = 3;
}

message GetStreamRecordResponse {
    message StreamRecord {
        required ActionType action_type = 1;
        required bytes record = 2;
    }
    repeated StreamRecord stream_records = 1;
    optional string next_shard_iterator = 2;
}

message ComputeSplitPointsBySizeRequest {
    required string table_name = 1;
    required int64 split_size = 2; // in 100MB
    optional int64 split_size_unit_in_byte = 3;
}

message ComputeSplitPointsBySizeResponse {
    required ConsumedCapacity consumed = 1;
    repeated PrimaryKeySchema schema = 2;

    /**
     * Split points between splits, in the increasing order
     *
     * A split is a consecutive range of primary keys,
     * whose data size is about split_size specified in the request.
     * The size could be hard to be precise.
     * 
     * A split point is an array of primary-key column w.r.t. table schema,
     * which is never longer than that of table schema.
     * Tailing -inf will be omitted to reduce transmission payloads.
     */
    repeated bytes split_points = 3;

    /**
     * Locations where splits lies in.
     *
     * By the managed nature of TableStore, these locations are no more than hints.
     * If a location is not suitable to be seen, an empty string will be placed.
     */
     message SplitLocation {
         required string location = 1;
         required sint64 repeat = 2;
     }
     repeated SplitLocation locations = 4;
}

/* SQLQuery */
enum SQLPayloadVersion {
    SQL_PLAIN_BUFFER = 1;
    SQL_FLAT_BUFFERS = 2;
}

enum SQLStatementType {
    SQL_SELECT = 1;
    SQL_CREATE_TABLE = 2;
    SQL_SHOW_TABLE = 3;
    SQL_DESCRIBE_TABLE = 4;
    SQL_DROP_TABLE = 5;
    SQL_ALTER_TABLE = 6;
}

message SQLQueryRequest {
    required string query = 1;
    optional SQLPayloadVersion version = 2;
}

message TableConsumedCapacity {
    optional string table_name = 1;
    optional ConsumedCapacity consumed = 2;
    optional ReservedThroughput reserved_throughput = 3;
    optional bool is_timeseries_meta = 4;
}

message SearchConsumedCapacity {
    optional string table_name = 1;
    optional string index_name = 2;
    optional ConsumedCapacity consumed = 3;
    optional ReservedThroughput reserved_throughput = 4;
}

message SQLQueryResponse {
    repeated TableConsumedCapacity consumes = 1;
    optional bytes rows = 2;
    optional SQLPayloadVersion version = 3;
    optional SQLStatementType type = 4;
    repeated SearchConsumedCapacity search_consumes = 5;
}




/* -------------------------------------- */


