// View Service
//
// Provides MapReduce view query execution against a Couchbase cluster.
// Views are defined in design documents and produce key-value results
// via map and optional reduce functions. Results are server-streamed.
syntax = "proto3";

option go_package = "github.com/couchbase/goprotostellar/genproto/view_v1;view_v1";
option ruby_package = "Couchbase::Protostellar::Generated::View::V1";
option php_namespace = "Couchbase\\Protostellar\\Generated\\View\\V1";
option java_package = "com.couchbase.client.protostellar.view.v1";
option java_multiple_files = true;
option csharp_namespace = "Couchbase.Protostellar.View.V1";

package couchbase.view.v1;

// ViewService provides MapReduce view query execution.
service ViewService {
  // ViewQuery executes a MapReduce view query and streams back result rows
  // and metadata. The final streamed response contains MetaData.
  rpc ViewQuery(ViewQueryRequest) returns (stream ViewQueryResponse) {}
}

// ViewQueryRequest specifies the view to query and all execution options.
message ViewQueryRequest {
  // The bucket containing the design document.
  string bucket_name = 8;

  // The name of the design document (without the '_design/' prefix).
  string design_document_name = 1;

  // The name of the view within the design document.
  string view_name = 2;

  // Number of initial rows to skip before returning results.
  optional uint32 skip = 3;

  // Maximum number of rows to return.
  optional uint32 limit = 4;

  // ScanConsistency controls index freshness for the view query.
  enum ScanConsistency {
    // No consistency required — fastest, may return stale results.
    SCAN_CONSISTENCY_NOT_BOUNDED = 0;

    // Wait for all prior mutations to be indexed.
    SCAN_CONSISTENCY_REQUEST_PLUS = 1;

    // Return current results but trigger an index update after the query.
    SCAN_CONSISTENCY_UPDATE_AFTER = 2;
  }

  // ErrorMode controls behavior when errors occur during view execution.
  enum ErrorMode {
    // Continue processing remaining results even if errors occur.
    ERROR_MODE_CONTINUE = 0;

    // Stop processing immediately on the first error.
    ERROR_MODE_STOP = 1;
  }

  // Namespace selects whether to query production or development views.
  enum Namespace {
    // Query production design documents.
    NAMESPACE_PRODUCTION = 0;

    // Query development design documents (prefixed with 'dev_').
    NAMESPACE_DEVELOPMENT = 1;
  }

  // Order controls the sort order of results.
  enum Order {
    // Return results in ascending key order (default).
    ORDER_ASCENDING = 0;

    // Return results in descending key order.
    ORDER_DESCENDING = 1;
  }

  optional ScanConsistency scan_consistency = 5;

  // When true, execute the view's reduce function on the results.
  optional bool reduce = 6;

  // When true, group reduce results by their keys.
  optional bool group = 7;

  // The number of key components to group by (used with group=true).
  optional uint32 group_level = 9;

  // Fetch only the row with this exact key. JSON-encoded.
  optional bytes key = 10;

  // Fetch only the rows with these exact keys. Each is JSON-encoded.
  repeated bytes keys = 11;

  // Start of the key range (inclusive). JSON-encoded.
  optional bytes start_key = 12;

  // End of the key range. JSON-encoded. Inclusiveness controlled by
  // inclusive_end.
  optional bytes end_key = 13;

  // When true, the end_key is inclusive (default: true).
  optional bool inclusive_end = 14;

  // Start returning results from this document ID within the start_key.
  optional string start_key_doc_id = 15;

  // Stop returning results at this document ID within the end_key.
  optional string end_key_doc_id = 16;

  optional ErrorMode on_error = 17;

  // When true, include debug information in the response metadata.
  optional bool debug = 18;

  optional Namespace namespace = 19;
  optional Order order = 20;
}

// ViewQueryResponse is streamed back. Contains result rows and optional
// metadata.
message ViewQueryResponse {
  // Row represents a single result from the view query.
  message Row {
    // The document ID that produced this row.
    string id = 1;

    // The emitted key from the map function. JSON-encoded.
    bytes key = 2;

    // The emitted value from the map function (or reduce result). JSON-encoded.
    bytes value = 3;
  }

  // The result rows from the view query.
  repeated Row rows = 1;

  // MetaData contains summary information about the view query results.
  message MetaData {
    // Total number of rows in the view (before skip/limit are applied).
    uint64 total_rows = 1;

    // Debug information, if requested. JSON-encoded.
    bytes debug = 2;
  }
  optional MetaData meta_data = 2;
}
