/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
namespace java org.apache.iotdb.service.rpc.thrift

// The return status code and message in each response.
struct TSStatus {
  1: required i32 code
  2: optional string message
}

struct TSExecuteStatementResp {
	1: required TSStatus status
	2: optional i64 queryId
  // Column names in select statement of SQL
	3: optional list<string> columns
	4: optional string operationType
	5: optional bool ignoreTimeStamp
  // Data type list of columns in select statement of SQL
  6: optional list<string> dataTypeList
  7: optional TSQueryDataSet queryDataSet
  // for disable align statements, queryDataSet is null and nonAlignQueryDataSet is not null
  8: optional TSQueryNonAlignDataSet nonAlignQueryDataSet
  9: optional map<string, i32> columnNameIndexMap
}

enum TSProtocolVersion {
  IOTDB_SERVICE_PROTOCOL_V1,
  IOTDB_SERVICE_PROTOCOL_V2,//V2 is the first version that we can check version compatibility
}

struct TSOpenSessionResp {
  1: required TSStatus status

  // The protocol version that the server is using.
  2: required TSProtocolVersion serverProtocolVersion = TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V1

  // Session id
  3: optional i64 sessionId

  // The configuration settings for this session.
  4: optional map<string, string> configuration
}

// OpenSession()
// Open a session (connection) on the server against which operations may be executed.
struct TSOpenSessionReq {
  1: required TSProtocolVersion client_protocol = TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V2
  2: optional string username
  3: optional string password
  4: optional map<string, string> configuration
}

// CloseSession()
// Closes the specified session and frees any resources currently allocated to that session.
// Any open operations in that session will be canceled.
struct TSCloseSessionReq {
  1: required i64 sessionId
}

// ExecuteStatement()
//
// Execute a statement.
// The returned OperationHandle can be used to check on the status of the statement, and to fetch results once the
// statement has finished executing.
struct TSExecuteStatementReq {
  // The session to execute the statement against
  1: required i64 sessionId

  // The statement to be executed (DML, DDL, SET, etc)
  2: required string statement

  // statementId
  3: required i64 statementId

  4: optional i32 fetchSize
}

struct TSExecuteBatchStatementResp{
	1: required list<TSStatus> statusList
}

struct TSExecuteBatchStatementReq{
  // The session to execute the statement against
  1: required i64 sessionId

  // The statements to be executed (DML, DDL, SET, etc)
  2: required list<string> statements
}

struct TSGetOperationStatusReq {
  1: required i64 sessionId
  // Session to run this request against
  2: required i64 queryId
}

// CancelOperation()
//
// Cancels processing on the specified operation handle and frees any resources which were allocated.
struct TSCancelOperationReq {
  1: required i64 sessionId
  // Operation to cancel
  2: required i64 queryId
}

// CloseOperation()
struct TSCloseOperationReq {
  1: required i64 sessionId
  2: optional i64 queryId
  3: optional i64 statementId
}

struct TSFetchResultsReq{
  1: required i64 sessionId
	2: required string statement
	3: required i32 fetchSize
	4: required i64 queryId
	5: required bool isAlign
}

struct TSFetchResultsResp{
	1: required TSStatus status
	2: required bool hasResultSet
  3: required bool isAlign
	4: optional TSQueryDataSet queryDataSet
	5: optional TSQueryNonAlignDataSet nonAlignQueryDataSet
}

struct TSFetchMetadataResp{
		1: required TSStatus status
		2: optional string metadataInJson
		3: optional list<string> columnsList
		4: optional string dataType
}

struct TSFetchMetadataReq{
    1: required i64 sessionId
		2: required string type
		3: optional string columnPath
}

struct TSGetTimeZoneResp {
    1: required TSStatus status
    2: required string timeZone
}

struct TSSetTimeZoneReq {
    1: required i64 sessionId
    2: required string timeZone
}

// for session
struct TSInsertRecordReq {
    1: required i64 sessionId
    2: required string deviceId
    3: required list<string> measurements
    4: required binary values
    5: required i64 timestamp
    6: optional bool inferType
}

struct TSInsertTabletReq {
    1: required i64 sessionId
    2: required string deviceId
    3: required list<string> measurements
    4: required binary values
    5: required binary timestamps
    6: required list<i32> types
    7: required i32 size
}

struct TSInsertTabletsReq {
    1: required i64 sessionId
    2: required list<string> deviceIds
    3: required list<list<string>> measurementsList
    4: required list<binary> valuesList
    5: required list<binary> timestampsList
    6: required list<list<i32>> typesList
    7: required list<i32> sizeList
}

struct TSInsertRecordsReq {
    1: required i64 sessionId
    2: required list<string> deviceIds
    3: required list<list<string>> measurementsList
    4: required list<binary> valuesList
    5: required list<i64> timestamps
    6: optional bool inferType
}

struct TSDeleteDataReq {
    1: required i64 sessionId
    2: required list<string> paths
    3: required i64 timestamp
}

struct TSCreateTimeseriesReq {
  1: required i64 sessionId
  2: required string path
  3: required i32 dataType
  4: required i32 encoding
  5: required i32 compressor
  6: optional map<string, string> props
  7: optional map<string, string> tags
  8: optional map<string, string> attributes
  9: optional string measurementAlias
}

struct TSCreateMultiTimeseriesReq {
  1: required i64 sessionId
  2: required list<string> paths
  3: required list<i32> dataTypes
  4: required list<i32> encodings
  5: required list<i32> compressors
  6: optional list<map<string, string>> propsList
  7: optional list<map<string, string>> tagsList
  8: optional list<map<string, string>> attributesList
  9: optional list<string> measurementAliasList
}

struct ServerProperties {
	1: required string version;
	2: required list<string> supportedTimeAggregationOperations;
	3: required string timestampPrecision;
}

struct TSQueryDataSet{
    // ByteBuffer for time column
    1: required binary time
    // ByteBuffer for each column values
    2: required list<binary> valueList
    // Bitmap for each column to indicate whether it is a null value
    3: required list<binary> bitmapList
}

struct TSQueryNonAlignDataSet{
    // ByteBuffer for each time column
	  1: required list<binary> timeList
	  // ByteBuffer for each column values
    2: required list<binary> valueList
}


service TSIService {
	TSOpenSessionResp openSession(1:TSOpenSessionReq req);

	TSStatus closeSession(1:TSCloseSessionReq req);

	TSExecuteStatementResp executeStatement(1:TSExecuteStatementReq req);

	TSExecuteBatchStatementResp executeBatchStatement(1:TSExecuteBatchStatementReq req);

	TSExecuteStatementResp executeQueryStatement(1:TSExecuteStatementReq req);

	TSExecuteStatementResp executeUpdateStatement(1:TSExecuteStatementReq req);

	TSFetchResultsResp fetchResults(1:TSFetchResultsReq req)

	TSFetchMetadataResp fetchMetadata(1:TSFetchMetadataReq req)

	TSStatus cancelOperation(1:TSCancelOperationReq req);

	TSStatus closeOperation(1:TSCloseOperationReq req);

	TSGetTimeZoneResp getTimeZone(1:i64 sessionId);

	TSStatus setTimeZone(1:TSSetTimeZoneReq req);

	ServerProperties getProperties();

	TSStatus setStorageGroup(1:i64 sessionId, 2:string storageGroup);

	TSStatus createTimeseries(1:TSCreateTimeseriesReq req);

	TSExecuteBatchStatementResp createMultiTimeseries(1:TSCreateMultiTimeseriesReq req);

  TSStatus deleteTimeseries(1:i64 sessionId, 2:list<string> path)

  TSStatus deleteStorageGroups(1:i64 sessionId, 2:list<string> storageGroup);

  TSStatus insertRecord(1:TSInsertRecordReq req);

  TSExecuteBatchStatementResp insertTablet(1:TSInsertTabletReq req);

  TSExecuteBatchStatementResp insertTablets(1:TSInsertTabletsReq req);

	TSExecuteBatchStatementResp insertRecords(1:TSInsertRecordsReq req);

	TSExecuteBatchStatementResp testInsertTablet(1:TSInsertTabletReq req);

  TSExecuteBatchStatementResp testInsertTablets(1:TSInsertTabletsReq req);

  TSStatus testInsertRecord(1:TSInsertRecordReq req);

  TSExecuteBatchStatementResp testInsertRecords(1:TSInsertRecordsReq req);

	TSStatus deleteData(1:TSDeleteDataReq req);

	i64 requestStatementId(1:i64 sessionId);
}
