// Copyright 2018 Google LLC
//
// Licensed 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.

syntax = "proto3";

import "google/firestore/v1/write.proto";
import "google/protobuf/timestamp.proto";

package firestore.client;

option java_multiple_files = true;
option java_package = "com.google.firebase.firestore.proto";

option objc_class_prefix = "FSTPB";

// Each user gets a single queue of WriteBatches to apply to the server.
// MutationQueue tracks the metadata about the queue.
message MutationQueue {
  // An identifier for the highest numbered batch that has been acknowledged by
  // the server. All WriteBatches in this queue with batch_ids less than or
  // equal to this value are considered to have been acknowledged by the
  // server.
  int32 last_acknowledged_batch_id = 1;

  // A stream token that was previously sent by the server.
  //
  // See StreamingWriteRequest in datastore.proto for more details about usage.
  //
  // After sending this token, earlier tokens may not be used anymore so only a
  // single stream token is retained.
  bytes last_stream_token = 2;
}

// Message containing a batch of user-level writes intended to be sent to
// the server in a single call. Each user-level batch gets a separate
// WriteBatch with a new batch_id.
message WriteBatch {
  // An identifier for this batch, allocated by the mutation queue in a
  // monotonically increasing manner.
  int32 batch_id = 1;

  // A list of writes to apply. All writes will be applied atomically.
  repeated google.firestore.v1.Write writes = 2;

  // The local time at which the write batch was initiated.
  google.protobuf.Timestamp local_write_time = 3;

  // A list of pseudo-writes that represent a partial base state from when this
  // write batch was initially created. When computing the local view batch,
  // these base_writes are applied prior to the real writes in order to
  // override certain document fields from the remote document cache. This is
  // necessary in the case of non-idempotent writes (e.g. numericAdd
  // transforms) to make sure that the local view of the modified documents
  // doesn't flicker if the remote document cache receives the result of the
  // non-idempotent write before the write is removed from the queue.
  //
  // These writes are never sent to the backend.
  repeated google.firestore.v1.Write base_writes = 4;
}
