Does the path of a Document have any bearing on the random ID auto-generated by Firestore?












0















If I want to know the (random) ID of a document before saving it to Firestore (without writing custom code), I can do the following:



String id = db.collection("collection-name").document().getId();


Does it make a difference if I give "collection-name" in the code above but use that id to save the document to collection "some-other-collection"?



In other words, does the collection name (or more generally, path of the document) have any bearing on the random ID generated by Firestore?



Are Firestore IDs generated similarly to what is described in The 2^120 Ways to Ensure Unique Identifiers?



How good would the following code be for auto-generating known IDs for Firestore documents:



private static SecureRandom RANDOMIZER = new SecureRandom();
.
.
.
byte randomId = new byte[120];
RANDOMIZER.nextBytes(randomId);
// Base64-encode randomId









share|improve this question





























    0















    If I want to know the (random) ID of a document before saving it to Firestore (without writing custom code), I can do the following:



    String id = db.collection("collection-name").document().getId();


    Does it make a difference if I give "collection-name" in the code above but use that id to save the document to collection "some-other-collection"?



    In other words, does the collection name (or more generally, path of the document) have any bearing on the random ID generated by Firestore?



    Are Firestore IDs generated similarly to what is described in The 2^120 Ways to Ensure Unique Identifiers?



    How good would the following code be for auto-generating known IDs for Firestore documents:



    private static SecureRandom RANDOMIZER = new SecureRandom();
    .
    .
    .
    byte randomId = new byte[120];
    RANDOMIZER.nextBytes(randomId);
    // Base64-encode randomId









    share|improve this question



























      0












      0








      0








      If I want to know the (random) ID of a document before saving it to Firestore (without writing custom code), I can do the following:



      String id = db.collection("collection-name").document().getId();


      Does it make a difference if I give "collection-name" in the code above but use that id to save the document to collection "some-other-collection"?



      In other words, does the collection name (or more generally, path of the document) have any bearing on the random ID generated by Firestore?



      Are Firestore IDs generated similarly to what is described in The 2^120 Ways to Ensure Unique Identifiers?



      How good would the following code be for auto-generating known IDs for Firestore documents:



      private static SecureRandom RANDOMIZER = new SecureRandom();
      .
      .
      .
      byte randomId = new byte[120];
      RANDOMIZER.nextBytes(randomId);
      // Base64-encode randomId









      share|improve this question
















      If I want to know the (random) ID of a document before saving it to Firestore (without writing custom code), I can do the following:



      String id = db.collection("collection-name").document().getId();


      Does it make a difference if I give "collection-name" in the code above but use that id to save the document to collection "some-other-collection"?



      In other words, does the collection name (or more generally, path of the document) have any bearing on the random ID generated by Firestore?



      Are Firestore IDs generated similarly to what is described in The 2^120 Ways to Ensure Unique Identifiers?



      How good would the following code be for auto-generating known IDs for Firestore documents:



      private static SecureRandom RANDOMIZER = new SecureRandom();
      .
      .
      .
      byte randomId = new byte[120];
      RANDOMIZER.nextBytes(randomId);
      // Base64-encode randomId






      java google-app-engine google-cloud-firestore id-generation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 14:05







      markvgti

















      asked Jan 2 at 13:37









      markvgtimarkvgti

      1,57242234




      1,57242234
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The Document IDs generated by Cloud Firestore are generated client-side, completely random, and not dependent on the collection that you generate them in.



          You can see this for yourself if you dig a bit into the (open-source) SDKs. For example, in the Android SDK, here's the source for CollectionReference.add():



          final DocumentReference ref = document();
          return ref.set(data)


          So that leaves the ID generation to the document method:



          public DocumentReference document() {
          return document(Util.autoId());
          }


          Which delegates to Util.autoId():



          private static final int AUTO_ID_LENGTH = 20;

          private static final String AUTO_ID_ALPHABET =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

          private static final Random rand = new Random();

          public static String autoId() {
          StringBuilder builder = new StringBuilder();
          int maxRandom = AUTO_ID_ALPHABET.length();
          for (int i = 0; i < AUTO_ID_LENGTH; i++) {
          builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
          }
          return builder.toString();
          }


          As said: pure client-side randomness with enough entropy to ensure global uniqueness.






          share|improve this answer
























          • This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

            – markvgti
            Jan 2 at 15:22













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007373%2fdoes-the-path-of-a-document-have-any-bearing-on-the-random-id-auto-generated-by%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The Document IDs generated by Cloud Firestore are generated client-side, completely random, and not dependent on the collection that you generate them in.



          You can see this for yourself if you dig a bit into the (open-source) SDKs. For example, in the Android SDK, here's the source for CollectionReference.add():



          final DocumentReference ref = document();
          return ref.set(data)


          So that leaves the ID generation to the document method:



          public DocumentReference document() {
          return document(Util.autoId());
          }


          Which delegates to Util.autoId():



          private static final int AUTO_ID_LENGTH = 20;

          private static final String AUTO_ID_ALPHABET =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

          private static final Random rand = new Random();

          public static String autoId() {
          StringBuilder builder = new StringBuilder();
          int maxRandom = AUTO_ID_ALPHABET.length();
          for (int i = 0; i < AUTO_ID_LENGTH; i++) {
          builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
          }
          return builder.toString();
          }


          As said: pure client-side randomness with enough entropy to ensure global uniqueness.






          share|improve this answer
























          • This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

            – markvgti
            Jan 2 at 15:22


















          1














          The Document IDs generated by Cloud Firestore are generated client-side, completely random, and not dependent on the collection that you generate them in.



          You can see this for yourself if you dig a bit into the (open-source) SDKs. For example, in the Android SDK, here's the source for CollectionReference.add():



          final DocumentReference ref = document();
          return ref.set(data)


          So that leaves the ID generation to the document method:



          public DocumentReference document() {
          return document(Util.autoId());
          }


          Which delegates to Util.autoId():



          private static final int AUTO_ID_LENGTH = 20;

          private static final String AUTO_ID_ALPHABET =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

          private static final Random rand = new Random();

          public static String autoId() {
          StringBuilder builder = new StringBuilder();
          int maxRandom = AUTO_ID_ALPHABET.length();
          for (int i = 0; i < AUTO_ID_LENGTH; i++) {
          builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
          }
          return builder.toString();
          }


          As said: pure client-side randomness with enough entropy to ensure global uniqueness.






          share|improve this answer
























          • This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

            – markvgti
            Jan 2 at 15:22
















          1












          1








          1







          The Document IDs generated by Cloud Firestore are generated client-side, completely random, and not dependent on the collection that you generate them in.



          You can see this for yourself if you dig a bit into the (open-source) SDKs. For example, in the Android SDK, here's the source for CollectionReference.add():



          final DocumentReference ref = document();
          return ref.set(data)


          So that leaves the ID generation to the document method:



          public DocumentReference document() {
          return document(Util.autoId());
          }


          Which delegates to Util.autoId():



          private static final int AUTO_ID_LENGTH = 20;

          private static final String AUTO_ID_ALPHABET =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

          private static final Random rand = new Random();

          public static String autoId() {
          StringBuilder builder = new StringBuilder();
          int maxRandom = AUTO_ID_ALPHABET.length();
          for (int i = 0; i < AUTO_ID_LENGTH; i++) {
          builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
          }
          return builder.toString();
          }


          As said: pure client-side randomness with enough entropy to ensure global uniqueness.






          share|improve this answer













          The Document IDs generated by Cloud Firestore are generated client-side, completely random, and not dependent on the collection that you generate them in.



          You can see this for yourself if you dig a bit into the (open-source) SDKs. For example, in the Android SDK, here's the source for CollectionReference.add():



          final DocumentReference ref = document();
          return ref.set(data)


          So that leaves the ID generation to the document method:



          public DocumentReference document() {
          return document(Util.autoId());
          }


          Which delegates to Util.autoId():



          private static final int AUTO_ID_LENGTH = 20;

          private static final String AUTO_ID_ALPHABET =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

          private static final Random rand = new Random();

          public static String autoId() {
          StringBuilder builder = new StringBuilder();
          int maxRandom = AUTO_ID_ALPHABET.length();
          for (int i = 0; i < AUTO_ID_LENGTH; i++) {
          builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
          }
          return builder.toString();
          }


          As said: pure client-side randomness with enough entropy to ensure global uniqueness.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 15:00









          Frank van PuffelenFrank van Puffelen

          243k29387414




          243k29387414













          • This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

            – markvgti
            Jan 2 at 15:22





















          • This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

            – markvgti
            Jan 2 at 15:22



















          This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

          – markvgti
          Jan 2 at 15:22







          This implementation is statistically certain to ensure no collision of IDs in a project? Where does the entropy come from? AFA I can see, it's just java.util.Random's implementation that is providing the entropy.

          – markvgti
          Jan 2 at 15:22






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007373%2fdoes-the-path-of-a-document-have-any-bearing-on-the-random-id-auto-generated-by%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          The term 'EXEC' is not recognized as the name of a cmdlet Powershell

          NPM command prompt closes immediately [closed]

          Error binding properties and functions in emscripten