// Copyright 2022 Lightbend Inc.

// gRPC interface for Kalix Views.

syntax = "proto3";

package kalix.component.view;

import "google/protobuf/any.proto";
import "kalix/component/component.proto";

option go_package = "github.com/lightbend/kalix-go-sdk/kalix/view;view";
option java_package = "kalix.protocol";

// Service that the SDK (in the user function) implements to make
// views available to the proxy.
//
// TODO: It is currently only implemented to support one request (ReceiveEvent)
// with two possible responses (Upsert, Delete). The intention, and reason for full-duplex
// streaming, is that there should be able to have an interaction with two main
// types of operations, loads, and updates, and with each load there is an
// associated continuation, which in turn may return more operations, including
// more loads, and so on recursively.
service Views {
  rpc Handle(stream ViewStreamIn) returns (stream ViewStreamOut);
}

// Input message type for the gRPC stream in.
message ViewStreamIn {
  oneof message {
    ReceiveEvent receive = 1;
  }
}

// event for transform_updates where an incoming event is passed to the SDK for user to transform
// into a final state that is stored/indexed for queries in the view
message ReceiveEvent {
  // name of the view service the event is for
  string service_name = 1;
  // method on the view service the event is for
  string command_name = 2;
  // incoming event to transform
  google.protobuf.Any payload = 3;
  // always contains the ce-subject entry which identifies the view entry for the event
  Metadata metadata = 4;
  // initial_table = 5 has been removed
  reserved 5;
  reserved "initial_table";
  // key = 6 has been removed
  reserved 6;
  reserved "key";
  // the existing view entry for the ce-subject if there was one,
  Row by_subject_lookup_result = 7;
}

// Output message type for the gRPC stream out.
message ViewStreamOut {
  oneof message {
    Upsert upsert = 1;
    Delete delete = 2;
  }
}

// to ignore an event and do nothing, use Upsert, but no/empty/null value for row
message Upsert {
  Row row = 1;
}

message Delete {}

// Common messages

// deprecated, no longer used, Key fields are now ignored
message Key {
  repeated KeyPart parts = 1;
}

message KeyPart {
  oneof part {
    string string_part = 1;
    bytes bytes_part = 2;
    int64 integer_part = 3;
    double float_part = 4;
  }
}

message Row {
  // index = 1 has been removed
  reserved 1;
  reserved "index";
  // key = 2 has been removed
  reserved 2;
  reserved "key";
  // view entry
  google.protobuf.Any value = 3; // May be unset if a lookup found no row.
}
