firebase-cpp-sdk icon indicating copy to clipboard operation
firebase-cpp-sdk copied to clipboard

[FR] Database (Desktop): Add support for special server value Increment

Open alexames opened this issue 4 years ago • 2 comments

Feature proposal

  • Firebase Component: Database

As of last year, Realtime Database supports a special value for incrementing locations in the database atomically without running a transaction, but this feature is not exposed in the C++ (or Unity) implementations.

This is accomplished in Java by assigning a value in the database to ServerValue.increment(). This is similar to how timestamps work, with ServerValue.timestamp(). Both functions return a special value that is treated specially on the server and client.

In C++ we have a ServerTimestamp() function that returns that special value. We should add a new function, something like ServerValueIncrement() or something similar, which generates the special value for the developer. IIRC the special value is just a map containing {".sv":"increment"} or something similar.

This special value would then get special handling in server_values.cc so that the operation can be approximated locally while the data makes its server round trip.

Here is the Java implementation of the server value code. This logic can probably be duplicated in C++ without much trouble. Pay attention in particular to resolveComplextDeferredValue()

alexames avatar Sep 10 '21 18:09 alexames

+1 this.

For now i'm just doing it like this in Unity..

public static Dictionary<string, object> firebaseIncrement(int amount) {

      Dictionary<string, object> increment = new Dictionary<string, object>();

      increment[".sv"] = new Dictionary<string, object>() {
	{ "increment", amount }
      };

      return increment;
}

ubberkid avatar Aug 16 '22 00:08 ubberkid