From 46c97ecf11bee3c822192ee12c431147cc7bd6e8 Mon Sep 17 00:00:00 2001 From: Kevin Gray Date: Thu, 25 Jul 2019 17:45:17 -0400 Subject: [PATCH 1/2] Updated how document Ids are generated to more closely match native implementation --- packages/cloud_firestore/CHANGELOG.md | 4 ++ .../cloud_firestore/lib/cloud_firestore.dart | 2 +- .../lib/src/collection_reference.dart | 2 +- .../lib/src/utils/auto_id_generator.dart | 34 ++++++++++ .../lib/src/utils/push_id_generator.dart | 63 ------------------- 5 files changed, 40 insertions(+), 65 deletions(-) create mode 100644 packages/cloud_firestore/lib/src/utils/auto_id_generator.dart delete mode 100644 packages/cloud_firestore/lib/src/utils/push_id_generator.dart diff --git a/packages/cloud_firestore/CHANGELOG.md b/packages/cloud_firestore/CHANGELOG.md index f3a81406954c..31ce0294d74c 100644 --- a/packages/cloud_firestore/CHANGELOG.md +++ b/packages/cloud_firestore/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.12.8 + +* Updated how document Ids are generated to more closely match native implementations. + ## 0.12.7+1 * Update google-services Android gradle plugin to 4.3.0 in documentation and examples. diff --git a/packages/cloud_firestore/lib/cloud_firestore.dart b/packages/cloud_firestore/lib/cloud_firestore.dart index ec4a21ac2137..fba72b049cb6 100755 --- a/packages/cloud_firestore/lib/cloud_firestore.dart +++ b/packages/cloud_firestore/lib/cloud_firestore.dart @@ -15,7 +15,7 @@ import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; import 'package:flutter/services.dart'; import 'package:meta/meta.dart'; -import 'src/utils/push_id_generator.dart'; +import 'src/utils/auto_id_generator.dart'; part 'src/blob.dart'; part 'src/collection_reference.dart'; diff --git a/packages/cloud_firestore/lib/src/collection_reference.dart b/packages/cloud_firestore/lib/src/collection_reference.dart index ffa5e016acfd..f1de3a8ba4b9 100644 --- a/packages/cloud_firestore/lib/src/collection_reference.dart +++ b/packages/cloud_firestore/lib/src/collection_reference.dart @@ -40,7 +40,7 @@ class CollectionReference extends Query { DocumentReference document([String path]) { List childPath; if (path == null) { - final String key = PushIdGenerator.generatePushChildName(); + final String key = AutoIdGenerator.autoId(); childPath = List.from(_pathComponents)..add(key); } else { childPath = List.from(_pathComponents)..addAll(path.split(('/'))); diff --git a/packages/cloud_firestore/lib/src/utils/auto_id_generator.dart b/packages/cloud_firestore/lib/src/utils/auto_id_generator.dart new file mode 100644 index 000000000000..e2e1cc57138a --- /dev/null +++ b/packages/cloud_firestore/lib/src/utils/auto_id_generator.dart @@ -0,0 +1,34 @@ +// Copyright 2017, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:math'; + +/// Utility class for generating Firebase child node keys. +/// +/// Since the Flutter plugin API is asynchronous, there's no way for us +/// to use the native SDK to generate the node key synchronously and we +/// have to do it ourselves if we want to be able to reference the +/// newly-created node synchronously. +/// +/// This code is based largely on the Android implementation and ported to Dart. + +class AutoIdGenerator { + static const int _AUTO_ID_LENGTH = 20; + + static const String _AUTO_ID_ALPHABET = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + + static final Random _random = Random(); + + static String autoId() { + final StringBuffer stringBuffer = StringBuffer(); + final int maxRandom = _AUTO_ID_ALPHABET.length; + + for (int i = 0; i < _AUTO_ID_LENGTH; ++i) { + stringBuffer.write(_AUTO_ID_ALPHABET[_random.nextInt(maxRandom)]); + } + + return stringBuffer.toString(); + } +} diff --git a/packages/cloud_firestore/lib/src/utils/push_id_generator.dart b/packages/cloud_firestore/lib/src/utils/push_id_generator.dart deleted file mode 100644 index f822f6ce66a5..000000000000 --- a/packages/cloud_firestore/lib/src/utils/push_id_generator.dart +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2017, the Chromium project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'dart:math'; - -/// Utility class for generating Firebase child node keys. -/// -/// Since the Flutter plugin API is asynchronous, there's no way for us -/// to use the native SDK to generate the node key synchronously and we -/// have to do it ourselves if we want to be able to reference the -/// newly-created node synchronously. -/// -/// This code is based on a Firebase blog post and ported to Dart. -/// https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html -class PushIdGenerator { - static const String PUSH_CHARS = - '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; - - static final Random _random = Random(); - - static int _lastPushTime; - - static final List _lastRandChars = List(12); - - static String generatePushChildName() { - int now = DateTime.now().millisecondsSinceEpoch; - final bool duplicateTime = (now == _lastPushTime); - _lastPushTime = now; - - final List timeStampChars = List(8); - for (int i = 7; i >= 0; i--) { - timeStampChars[i] = PUSH_CHARS[now % 64]; - now = (now / 64).floor(); - } - assert(now == 0); - - final StringBuffer result = StringBuffer(timeStampChars.join()); - - if (!duplicateTime) { - for (int i = 0; i < 12; i++) { - _lastRandChars[i] = _random.nextInt(64); - } - } else { - _incrementArray(); - } - for (int i = 0; i < 12; i++) { - result.write(PUSH_CHARS[_lastRandChars[i]]); - } - assert(result.length == 20); - return result.toString(); - } - - static void _incrementArray() { - for (int i = 11; i >= 0; i--) { - if (_lastRandChars[i] != 63) { - _lastRandChars[i] = _lastRandChars[i] + 1; - return; - } - _lastRandChars[i] = 0; - } - } -} From e9068562331242a2499b5240c4e6df8b2f228c76 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Fri, 26 Jul 2019 13:40:04 -0700 Subject: [PATCH 2/2] fix caps --- packages/cloud_firestore/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloud_firestore/CHANGELOG.md b/packages/cloud_firestore/CHANGELOG.md index 31ce0294d74c..3d68c9afe1e5 100644 --- a/packages/cloud_firestore/CHANGELOG.md +++ b/packages/cloud_firestore/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.12.8 -* Updated how document Ids are generated to more closely match native implementations. +* Updated how document ids are generated to more closely match native implementations. ## 0.12.7+1