/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * license agreements; and to You under the Apache License, version 2.0:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * This file is part of the Apache Pekko project, which was derived from Akka.
 */

/*
 * Copyright (C) 2014-2022 Lightbend Inc. <https://www.lightbend.com>
 */

syntax = "proto2";

package org.apache.pekko.cluster.ddata;

option java_package = "org.apache.pekko.cluster.ddata.protobuf.msg";
option optimize_for = SPEED;

message Get {
  required OtherMessage key = 1;
  required sint32 consistency = 2;
  required uint32 timeout = 3;
  optional OtherMessage request = 4;
  optional int32 consistencyMinCap = 5;
  optional int32 consistencyAdditional = 6;
}

message GetSuccess {
  required OtherMessage key = 1;
  required OtherMessage data = 2;
  optional OtherMessage request = 4;
}

message NotFound {
  required OtherMessage key = 1;
  optional OtherMessage request = 2;
}

message GetFailure {
  required OtherMessage key = 1;
  optional OtherMessage request = 2;
}

message Subscribe {
  required OtherMessage key = 1;
  required string ref = 2;
}

message Unsubscribe {
  required OtherMessage key = 1;
  required string ref = 2;
}

message Changed {
  required OtherMessage key = 1;
  required OtherMessage data = 2;
}

message Write {
  required string key = 1;
  required DataEnvelope envelope = 2;
  optional UniqueAddress fromNode = 3;
}

// message WriteAck, via Empty

message Empty {
}

message Read {
  required string key = 1;
  optional UniqueAddress fromNode = 2;
}

message ReadResult {
  optional DataEnvelope envelope = 1;
}

message DataEnvelope {
  message PruningEntry {
    required UniqueAddress removedAddress = 1;
    required UniqueAddress ownerAddress = 2;
    required bool performed = 3;
    repeated Address seen = 4;
    optional sint64 obsoleteTime = 5;
  } 
  
  required OtherMessage data = 1;
  repeated PruningEntry pruning = 2;
  optional VersionVector deltaVersions = 3;
}

message Status {
  message Entry {
    required string key = 1;
    required bytes digest = 2;
  }
  
  required uint32 chunk = 1;
  required uint32 totChunks = 2;
  repeated Entry entries = 3;
  optional sfixed64 toSystemUid = 4;
  optional sfixed64 fromSystemUid = 5;
}

message Gossip {
  message Entry {
    required string key = 1;
    required DataEnvelope envelope = 2;
  }
  
  required bool sendBack = 1;
  repeated Entry entries = 2;
  optional sfixed64 toSystemUid = 3;
  optional sfixed64 fromSystemUid = 4;
}

message DeltaPropagation {
  message Entry {
    required string key = 1;
    required DataEnvelope envelope = 2;
    required int64 fromSeqNr = 3;
    optional int64 toSeqNr = 4; // if not set then same as fromSequenceNr
  }
  
  required UniqueAddress fromNode = 1;
  repeated Entry entries = 2;
  optional bool reply = 3; // no reply if not set
}

message UniqueAddress {
  required Address address = 1;
  required sfixed32 uid = 2;
  // 64 bit uids but with backward wire compatibility
  optional sfixed32 uid2 = 3;
}

message Address {
  required string hostname = 1;
  required uint32 port = 2;
}

message VersionVector {
  message Entry {
    required UniqueAddress node = 1;
    required int64 version = 2;
  }
  repeated Entry entries = 1;
}

message OtherMessage {
  required bytes enclosedMessage = 1;
  required int32 serializerId = 2;
  optional bytes messageManifest = 4;
}

message StringGSet {
  repeated string elements = 1;
}

message DurableDataEnvelope {
  required OtherMessage data = 1;
  repeated DataEnvelope.PruningEntry pruning = 2;
}
