Exemplo n.º 1
0
        private List <string> CloneUserWithNewId(IEnumerable <StreamResult <TUser> > users)
        {
            var userIdsToDelete = new List <string>(CountUsers());
            var nextUserId      = this.newUserIdType == UserIdType.Consecutive ?
                                  GetNextIdForUser() :
                                  -1;

            using var bulkInsert = docStore.BulkInsert();
            foreach (var userStream in users)
            {
                var user = userStream.Document;

                // Figure out what the new ID will be.
                var newId = Conventions.UserIdFor(user, this.newUserIdType, this.docStore);
                if (this.newUserIdType == UserIdType.Consecutive && newId != null && newId.EndsWith("|"))
                {
                    // We'll need to specify the full ID, as bulk insert doens't work with "Users|" type IDs.
                    newId = Conventions.CollectionNameWithSeparator <TUser>(docStore) + nextUserId.ToString();
                    nextUserId++;
                }

                // See if we actually need a new ID.
                // If not, we can skip processing this user.
                var needsNewId = !string.Equals(newId, user.Id);
                if (needsNewId)
                {
                    // Strip off the existing ID, otherwise bulk insert will pick it up and use the existing.
                    user.Id = null;
                    userStream.Metadata.Remove("@id");

                    // Clone the user with the new ID.
                    if (newId != null)
                    {
                        bulkInsert.Store(user, newId, userStream.Metadata);
                    }
                    else
                    {
                        bulkInsert.Store(user, userStream.Metadata);
                    }

                    // Step 3a, queue up the delete for the existing user.
                    var isNewId = !string.Equals(user.Id, userStream.Id, StringComparison.OrdinalIgnoreCase);
                    if (isNewId)
                    {
                        userIdsToDelete.Add(userStream.Id);
                    }
                }
            }

            return(userIdsToDelete);
        }