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

import "table_store.proto";

message TimeseriesTableOptions {
  optional int32 time_to_live = 1;
}

message TimeseriesMetaOptions {
  optional bool allow_update_attributes = 1;
  optional int32 meta_time_to_live = 2;
}

message TimeseriesTableMeta {
  required string table_name = 1;
  optional TimeseriesTableOptions table_options = 2;
  optional string status = 3;
  optional TimeseriesMetaOptions meta_options = 4;
  repeated string timeseries_key_schema = 5;
  repeated PrimaryKeySchema field_primary_key_schema = 6;
  optional bool disable_hash_partition_key = 7;
  optional bool disable_timeseries_meta_index = 8;
}

message LastpointIndexMetaForCreate {
  required string index_table_name = 1;
}

message CreateTimeseriesTableRequest {
  required TimeseriesTableMeta table_meta = 1;
  repeated TimeseriesAnalyticalStore analytical_stores = 3;
  optional bool enable_analytical_store = 4;
  repeated LastpointIndexMetaForCreate lastpoint_index_metas = 5;
}

message CreateTimeseriesTableResponse {
}

message ListTimeseriesTableRequest {
}

message ListTimeseriesTableResponse {
  repeated TimeseriesTableMeta table_metas = 1;
}

message DeleteTimeseriesTableRequest {
  required string table_name = 1;
}

message DeleteTimeseriesTableResponse {
}

message UpdateTimeseriesTableRequest {
  required string table_name = 1;
  optional TimeseriesTableOptions table_options = 2;
  optional TimeseriesMetaOptions meta_options = 3;
}

message UpdateTimeseriesTableResponse {
}

message DescribeTimeseriesTableRequest {
  required string table_name = 1;
}

message DescribeTimeseriesTableResponse {
  required TimeseriesTableMeta table_meta = 1;
  repeated TimeseriesAnalyticalStore analytical_stores = 2;
  repeated TimeseriesLastpointIndex lastpoint_indexes = 3;
}

enum MetaQueryConditionType {
  COMPOSITE_CONDITION = 1;
  MEASUREMENT_CONDITION = 2;
  SOURCE_CONDITION = 3;
  TAG_CONDITION = 4;
  UPDATE_TIME_CONDITION = 5;
  ATTRIBUTE_CONDITION = 6;
}

message MetaQueryCondition {
  required MetaQueryConditionType type = 1;
  required bytes proto_data = 2;
}

enum MetaQueryCompositeOperator {
  OP_AND = 1;
  OP_OR = 2;
  OP_NOT = 3;
}

message MetaQueryCompositeCondition {
  required MetaQueryCompositeOperator op = 1;
  repeated MetaQueryCondition sub_conditions = 2;
}

enum MetaQuerySingleOperator {
  OP_EQUAL = 1;
  OP_GREATER_THAN = 2;
  OP_GREATER_EQUAL = 3;
  OP_LESS_THAN = 4;
  OP_LESS_EQUAL = 5;
  OP_PREFIX = 6;
}

message MetaQueryMeasurementCondition {
  required MetaQuerySingleOperator op = 1;
  required string value = 2;
}

message MetaQuerySourceCondition {
  required MetaQuerySingleOperator op = 1;
  required string value = 2;
}

message MetaQueryTagCondition {
  required MetaQuerySingleOperator op = 1;
  required string tag_name = 2;
  required string value = 3;
}

message MetaQueryAttributeCondition {
  required MetaQuerySingleOperator op = 1;
  required string attr_name = 2;
  required string value = 3;
}

message MetaQueryUpdateTimeCondition {
  required MetaQuerySingleOperator op = 1;
  required int64 value = 2;
}

message TimeseriesTag {
  required string name = 1;
  required string value = 2;
}

message TimeseriesKey {
  optional string measurement = 1;
  optional string source = 2;
  optional string tags = 3;
  repeated TimeseriesTag tag_list = 4;
}

message TimeseriesMeta {
  required TimeseriesKey time_series_key = 1;
  optional string attributes = 2;
  optional int64 update_time = 3;
}

message QueryTimeseriesMetaRequest {
  required string table_name = 1;
  optional MetaQueryCondition condition = 2;
  optional bool get_total_hit = 3;
  optional bytes token = 4;
  optional int32 limit = 5;
  optional int64 supported_table_version = 6;
}

message QueryTimeseriesMetaResponse {
  repeated TimeseriesMeta timeseries_metas = 1;
  optional int64 total_hit = 2;
  optional bytes next_token = 3;
}

enum RowsSerializeType {
  RST_FLAT_BUFFER = 0;
  RST_PLAIN_BUFFER = 1;
}

message TimeseriesRows {
  required RowsSerializeType type = 1;
  required bytes rows_data = 2;
  optional int32 flatbuffer_crc32c = 3;
}

enum MetaUpdateMode {
  MUM_NORMAL = 0;
  MUM_IGNORE = 1;
}

message PutTimeseriesDataRequest {
  required string table_name = 1;
  required TimeseriesRows rows_data = 2;
  optional MetaUpdateMode meta_update_mode = 3;
  optional int64 supported_table_version = 4;
}

message FailedRowInfo {
  required int32 row_index = 1;
  optional string error_code = 2;
  optional string error_message = 3;
}

message MetaUpdateStatus {
  repeated uint32 row_ids = 1;
  repeated uint32 meta_update_times = 2;
}

message PutTimeseriesDataResponse {
  repeated FailedRowInfo failed_rows = 1;
  optional MetaUpdateStatus meta_update_status = 2;
}

message TimeseriesFieldsToGet {
  optional string name = 1;
  optional int32 type = 2;
}

message GetTimeseriesDataRequest {
  required string table_name = 1;
  required TimeseriesKey time_series_key = 2;
  optional int64 begin_time = 3;
  optional int64 end_time = 4;
  optional int64 specific_time = 5;
  optional bytes token = 6;
  optional int32 limit = 7;
  optional bool backward = 8;
  repeated TimeseriesFieldsToGet fields_to_get = 9;
  optional int64 supported_table_version = 10;
}

message GetTimeseriesDataResponse {
  required bytes rows_data = 1;
  optional bytes next_token = 2;
}

message UpdateTimeseriesMetaRequest {
  required string table_name = 1;
  repeated TimeseriesMeta timeseries_meta = 2;
  optional int64 supported_table_version = 3;
}

message UpdateTimeseriesMetaResponse {
  repeated FailedRowInfo failed_rows = 1;
}

message DeleteTimeseriesMetaRequest {
  required string table_name = 1;
  repeated TimeseriesKey timeseries_key = 2;
  optional int64 supported_table_version = 3;
}

message DeleteTimeseriesMetaResponse {
  repeated FailedRowInfo failed_rows = 1;
}

message SplitTimeseriesScanTaskRequest {
  required string table_name = 1;
  optional string measurement_name = 2;
  required int32 split_count_hint = 3;
}

message SplitTimeseriesScanTaskResponse {
  repeated bytes split_infos = 1;
}

message ScanTimeseriesDataRequest {
  required string table_name = 1;
  optional bytes split_info = 2;
  optional int64 start_time_us = 3;
  optional int64 end_time_us = 4;
  repeated TimeseriesFieldsToGet fields_to_get = 5;
  optional int32 limit = 6;
  optional RowsSerializeType data_serialize_type = 7;
  optional bytes token = 8;
  optional int64 supported_table_version = 9;
}

message ScanTimeseriesDataResponse {
  optional RowsSerializeType data_serialize_type = 1;
  required bytes data = 2;
  optional bytes next_token = 3;
}

message TimeseriesAnalyticalStore {
  optional string store_name = 1;	      // Store name
  optional int32 time_to_live = 2;    // Storage data TTL
  optional AnalyticalStoreSyncType sync_option = 3;	// Full or incremental build
}

enum AnalyticalStoreSyncType {
  SYNC_TYPE_FULL = 1;	// Full build
  SYNC_TYPE_INCR = 2;	// Incremental building
}

message AnalyticalStoreSyncStat {
  optional AnalyticalStoreSyncType sync_phase = 1;				// Build status
  optional int64 current_sync_timestamp = 2;// Latest sync time
}

message AnalyticalStoreStorageSize {
  optional int64 size = 1;			// Analyze the storage size occupied by the storage
  optional int64 timestamp = 2;	// Count statistics
}

message CreateTimeseriesAnalyticalStoreRequest {
  required string table_name = 1;
  optional TimeseriesAnalyticalStore analytical_store = 2;	// The time-series analysis storage that needs to be created
}

message CreateTimeseriesAnalyticalStoreResponse {}

message DeleteTimeseriesAnalyticalStoreRequest {
  required string table_name = 1;
  required string store_name = 2;
  optional bool drop_mapping_table = 3;
}

message DeleteTimeseriesAnalyticalStoreResponse {}

message DescribeTimeseriesAnalyticalStoreRequest {
  required string table_name = 1;
  required string store_name = 2;
}

message DescribeTimeseriesAnalyticalStoreResponse {
  optional TimeseriesAnalyticalStore analytical_store = 1;
  optional AnalyticalStoreSyncStat sync_stat = 2;
  optional AnalyticalStoreStorageSize storage_size = 3;
}

message UpdateTimeseriesAnalyticalStoreRequest {
  required string table_name = 1;
  required TimeseriesAnalyticalStore analytical_store = 2;
}

message UpdateTimeseriesAnalyticalStoreResponse {}

message TimeseriesLastpointIndex {
  optional string index_table_name = 1;
}

message CreateTimeseriesLastpointIndexRequest {
  required string main_table_name = 1;
  required string index_table_name = 2;
  optional bool include_base_data = 3;
  optional bool create_on_wide_column_table = 4;
  repeated string index_primary_key_names = 5;
}

message CreateTimeseriesLastpointIndexResponse {
}

message DeleteTimeseriesLastpointIndexRequest {
  required string main_table_name = 1;
  required string index_table_name = 2;
}

message DeleteTimeseriesLastpointIndexResponse {
}
