syntax = "proto3";
//Version 5.0
package v5api;

service BTrDB {
  rpc RawValues(RawValuesParams) returns (stream RawValuesResponse);
  rpc ArrowRawValues(ArrowRawValuesParams) returns (stream ArrowRawValuesResponse);
  rpc ArrowMultiValues(ArrowMultiValuesParams) returns (stream ArrowMultiValuesResponse);
  rpc AlignedWindows(AlignedWindowsParams) returns (stream AlignedWindowsResponse);
  rpc ArrowAlignedWindows(AlignedWindowsParams) returns (stream ArrowAlignedWindowsResponse);
  rpc Windows(WindowsParams) returns (stream WindowsResponse);
  rpc ArrowWindows(WindowsParams) returns (stream ArrowWindowsResponse);
  rpc StreamInfo(StreamInfoParams) returns (StreamInfoResponse);
  rpc SetStreamAnnotations(SetStreamAnnotationsParams) returns (SetStreamAnnotationsResponse);
  rpc SetStreamTags(SetStreamTagsParams) returns (SetStreamTagsResponse);
  rpc Create(CreateParams) returns (CreateResponse);
  rpc ListCollections(ListCollectionsParams) returns (stream ListCollectionsResponse);
  rpc LookupStreams(LookupStreamsParams) returns (stream LookupStreamsResponse);
  rpc Nearest(NearestParams) returns (NearestResponse);
  rpc Changes(ChangesParams) returns (stream ChangesResponse);
  rpc Insert(InsertParams) returns (InsertResponse);
  rpc ArrowInsert(ArrowInsertParams) returns (InsertResponse);
  rpc Delete(DeleteParams) returns (DeleteResponse);
  rpc Info(InfoParams) returns (InfoResponse);
  rpc FaultInject(FaultInjectParams) returns (FaultInjectResponse);
  rpc Flush(FlushParams) returns (FlushResponse);
  rpc Obliterate(ObliterateParams) returns (ObliterateResponse);
  rpc GetMetadataUsage(MetadataUsageParams) returns (MetadataUsageResponse);
  rpc GenerateCSV(GenerateCSVParams) returns (stream GenerateCSVResponse);
  rpc SQLQuery(SQLQueryParams) returns (stream SQLQueryResponse);
  rpc Subscribe(stream SubscriptionUpdate) returns (stream SubscriptionResp);
  //rpc SetCompactionConfig(SetCompactionConfigParams) returns (SetCompactionConfigResponse);
  //rpc GetCompactionConfig(GetCompactionConfigParams) returns (GetCompactionConfigResponse);
}

message RawValuesParams {
  bytes uuid = 1;
  sfixed64 start = 2;
  sfixed64 end = 3;
  uint64 versionMajor = 4;
}
message RawValuesResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  repeated RawPoint values = 4;
}
message ArrowRawValuesParams {
  bytes uuid = 1;
  sfixed64 start = 2;
  sfixed64 end = 3;
  uint64 versionMajor = 4;
  bytes templateBytes = 5;
}
message ArrowRawValuesResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  bytes arrowBytes = 4;
}
message ArrowMultiValuesParams {
  repeated bytes uuid = 1;
  repeated uint64 versionMajor = 2;
  sfixed64 start = 3;
  sfixed64 end = 4;
  int64 snapPeriodNs = 5;
  bytes templateBytes = 6;
}
message ArrowMultiValuesResponse {
  Status stat = 1;
  bytes arrowBytes = 2;
}
message RawPointVec {
  sfixed64 time = 1;
  repeated double value = 2;
};
message AlignedWindowsParams {
  bytes uuid = 1;
  sfixed64 start = 2;
  sfixed64 end = 3;
  uint64 versionMajor = 4;
  uint32 pointWidth = 5;
}
message AlignedWindowsResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  repeated StatPoint values = 4;
}
message ArrowAlignedWindowsResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  bytes arrowBytes = 4;
}
message WindowsParams {
  bytes uuid = 1;
  sfixed64 start = 2;
  sfixed64 end = 3;
  uint64 versionMajor = 4;
  uint64 width = 5;
  uint32 depth = 6;
}
message WindowsResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  repeated StatPoint values = 4;
}
message ArrowWindowsResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  bytes arrowBytes = 4;
}
message StreamInfoParams {
  bytes uuid = 1;
  bool omitVersion = 2;
  bool omitDescriptor = 3;
  Role role = 100;
}
message StreamInfoResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  StreamDescriptor descriptor = 4;
}
message StreamDescriptor {
  bytes uuid = 1;
  string collection = 2;
  repeated KeyOptValue tags = 3;
  repeated KeyOptValue annotations = 4;
  uint64 propertyVersion = 5;
}
message SetStreamAnnotationsParams {
  bytes uuid = 1;
  uint64 expectedPropertyVersion = 2;
  repeated KeyOptValue changes = 3;
  repeated string removals = 4;
}
message SetStreamAnnotationsResponse {
  Status stat = 1;
}
message SetStreamTagsParams {
  bytes uuid = 1;
  uint64 expectedPropertyVersion = 2;
  repeated KeyOptValue tags = 3;
  string collection = 4;
  repeated string remove = 5;
}
message SetStreamTagsResponse {
  Status stat = 1;
}
message CreateParams {
  bytes uuid = 1;
  string collection = 2;
  repeated KeyOptValue tags = 3;
  repeated KeyOptValue annotations = 4;
}
message CreateResponse {
  Status stat = 1;
}
message MetadataUsageParams {
  string prefix = 1;
  Role role = 100;
}
message MetadataUsageResponse {
  Status stat = 1;
  repeated KeyCount tags = 2;
  repeated KeyCount annotations = 3;
}
message KeyCount {
  string key = 1;
  uint64 count = 2;
}
message ListCollectionsParams {
  string prefix = 1;
  Role role = 100;
}
message ListCollectionsResponse {
  Status stat = 1;
  repeated string collections = 2;
}
message LookupStreamsParams {
  string collection = 1;
  bool isCollectionPrefix = 2;
  repeated KeyOptValue tags = 3;
  repeated KeyOptValue annotations = 4;
  Role role = 100;
}
message LookupStreamsResponse {
  Status stat = 1;
  repeated StreamDescriptor results = 2;
}
message NearestParams {
  bytes uuid = 1;
  sfixed64 time = 2;
  uint64 versionMajor = 3;
  bool backward = 4;
}
message NearestResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  RawPoint value = 4;
}
message ChangesParams {
  bytes uuid = 1;
  uint64 fromMajor = 2;
  uint64 toMajor = 3;
  uint32 resolution = 4;
}
message ChangesResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
  repeated ChangedRange ranges = 4;
}
enum MergePolicy {
  NEVER = 0;	// Never merge
  EQUAL = 1;	// Merge identical (key, value) pairs
  RETAIN = 2;	// When timestamps are equal, keep old value
  REPLACE = 3;	// When timestamps are equal, keep new value
}
message RoundSpec {
  oneof spec {
    int32 bits = 2;
  }
}
message InsertParams {
  bytes uuid = 1;
  bool sync = 2;
  MergePolicy merge_policy = 4;
  RoundSpec rounding=5;
  repeated RawPoint values = 3;
}
message ArrowInsertParams {
  bytes uuid = 1;
  bool sync = 2;
  MergePolicy merge_policy = 3;
  RoundSpec rounding = 4;
  bytes arrowBytes = 5;
}
message InsertResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
}
message DeleteParams {
  bytes uuid = 1;
  sfixed64 start = 2;
  sfixed64 end = 3;
}
message DeleteResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
}
message InfoParams {

}
message InfoResponse {
  Status stat = 1;
  Mash mash = 2;
  uint32 majorVersion = 3;
  uint32 minorVersion = 4;
  string build = 5;
  ProxyInfo proxy = 6;
}
message ProxyInfo {
  repeated string proxyEndpoints = 1;
}
message FaultInjectParams {
  uint64 type = 1;
  bytes params = 2;
}
message FaultInjectResponse {
  Status stat = 1;
  bytes rv = 2;
}
message FlushParams {
  bytes uuid = 1;
}
message FlushResponse {
  Status stat = 1;
  uint64 versionMajor = 2;
  uint64 versionMinor = 3;
}
message ObliterateParams {
  bytes uuid = 1;
}
message ObliterateResponse {
  Status stat = 1;
}
message RawPoint {
  sfixed64 time = 1;
  double value = 2;
}
message StatPoint {
  sfixed64 time = 1;
  double min = 2;
  double mean = 3;
  double max = 4;
  fixed64 count = 5;
  double stddev = 6;
}
message ChangedRange {
  sfixed64 start = 1;
  sfixed64 end = 2;
}
message Status {
  uint32 code = 1;
  string msg = 2;
  Mash mash = 3;
}
message Mash {
  int64 revision = 1;
  string leader = 2;
  int64 leaderRevision = 3;
  int64 totalWeight = 4;
  bool healthy = 5;
  double unmapped = 6;
  repeated Member members = 7;
}
message Member {
  uint32 hash = 1;
  string nodename = 2;
  bool up = 3;
  bool in = 4;
  bool enabled = 5;
  int64 start = 6;
  int64 end = 7;
  int64 weight = 8;
  double readPreference = 9;
  string httpEndpoints = 10;
  string grpcEndpoints = 11;
}
message KeyOptValue {
  string key = 1;
  OptValue val = 2;
}
message OptValue {
  string value = 1;
}
message KeyValue {
  string key = 1;
  string value = 2;
}

message StreamCSVConfig {
  uint64 version = 1;
  string label = 2;
  bytes uuid = 3;
}
message GenerateCSVParams {
  enum QueryType {
    ALIGNED_WINDOWS_QUERY = 0;
    WINDOWS_QUERY = 1;
    RAW_QUERY = 2;
  }
  QueryType queryType = 1;
  int64 startTime = 2;
  int64 endTime = 3;
  uint64 windowSize = 4;
  uint32 depth = 5;
  bool includeVersions = 6;
  repeated StreamCSVConfig streams = 7;
}
message GenerateCSVResponse {
  Status stat = 1;
  bool isHeader = 2;
  repeated string row = 3;
}
message SQLQueryParams {
  string query = 1;
  repeated string params = 2;
  Role role = 100;
}
message SQLQueryResponse {
  Status stat = 1;
  repeated bytes SQLQueryRow = 2;
}

message Role {
  string name = 1;
}

message SetCompactionConfigParams {
  //Which stream to configure
  bytes uuid = 1;

  //Accessing versions LESS than this is not allowed
  uint64 CompactedVersion = 2;

  //For every timestamp >= Start and < End in this list,
  //we cannot traverse the tree < Resolution.
  // Ranges cannot overlap, and a range can never have its resolution PW increased
  // (it is okay to decrease a range PW and it is okay to merge two adjacent ranges
  // if the new range has <= resolution)
  repeated ReducedResolutionRange reducedResolutionRanges = 3;

  uint64 unused0 = 4;
}

message SetCompactionConfigResponse {
  Status stat = 1;
}

message GetCompactionConfigParams {
  //Which stream to query
  bytes uuid = 1;
}

message GetCompactionConfigResponse {
  Status stat = 1;

  //The latest version of the stream, as returned by StreamInfo
  uint64 LatestMajorVersion = 2;

  //Accessing versions LESS than this is not allowed
  uint64 CompactedVersion = 3;

  //For every timestamp >= Start and < End in this list,
  //we cannot traverse the tree < Resolution.
  // Ranges cannot overlap, and a range can never have its resolution PW increased
  // (it is okay to decrease a range PW and it is okay to merge two adjacent ranges
  // if the new range has <= resolution)
  repeated ReducedResolutionRange reducedResolutionRanges = 4;

  uint64 unused0 = 5;
}

message ReducedResolutionRange {
  int64 Start = 1;
  int64 End = 2;
  uint32 Resolution = 3;
}

enum SubscriptionUpdateOp {
  ADD_UUIDS = 0;
  REMOVE_UUIDS = 1;
}

message SubscriptionUpdate {
  SubscriptionUpdateOp op = 1;
  repeated bytes uuid = 2;
}

message SubscriptionResp {
  Status stat = 1;
  bytes uuid = 2;
  bytes arrowBytes = 3;
}
