Esempio n. 1
0
        private bool TryResolveConflictByScript(
            DocumentsOperationContext documentsContext,
            DocumentConflict conflict)
        {
            var collection = conflict.Collection;

            var hasScript = _conflictResolver.ScriptConflictResolversCache.TryGetValue(collection, out ScriptResolver scriptResolver);

            if (!hasScript || scriptResolver == null)
            {
                if (_log.IsInfoEnabled)
                {
                    _log.Info($"Script not found to resolve the {collection} collection");
                }
                return(false);
            }

            var conflictedDocs = new List <DocumentConflict>(documentsContext.DocumentDatabase.DocumentsStorage.ConflictsStorage.GetConflictsFor(documentsContext, conflict.Id));

            if (conflictedDocs.Count == 0)
            {
                var relevantLocalDoc = documentsContext.DocumentDatabase.DocumentsStorage
                                       .GetDocumentOrTombstone(
                    documentsContext,
                    conflict.Id);
                if (relevantLocalDoc.Document != null)
                {
                    conflictedDocs.Add(DocumentConflict.From(documentsContext, relevantLocalDoc.Document));
                }
                else if (relevantLocalDoc.Tombstone != null)
                {
                    conflictedDocs.Add(DocumentConflict.From(relevantLocalDoc.Tombstone));
                }
            }

            if (conflictedDocs.Count == 0)
            {
                InvalidConflictWhenThereIsNone(conflict.Id);
            }

            conflictedDocs.Add(conflict.Clone());
            conflictedDocs.Sort((x, y) => string.Compare(x.ChangeVector, y.ChangeVector, StringComparison.Ordinal));

            if (_conflictResolver.TryResolveConflictByScriptInternal(
                    documentsContext,
                    scriptResolver,
                    conflictedDocs,
                    documentsContext.GetLazyString(collection), out var resolved))
            {
                _conflictResolver.PutResolvedDocument(documentsContext, resolved, resolvedToLatest: false, conflict);
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        private bool TryResolveConflictByScript(
            DocumentsOperationContext documentsContext,
            string id,
            string incomingChangeVector,
            BlittableJsonReaderObject doc)
        {
            var collection = CollectionName.GetCollectionName(id, doc);

            var hasScript = _conflictResolver.ScriptConflictResolversCache.TryGetValue(collection, out ScriptResolver scriptResolver);

            if (!hasScript || scriptResolver == null)
            {
                if (_log.IsInfoEnabled)
                {
                    _log.Info($"Script not found to resolve the {collection} collection");
                }
                return(false);
            }

            var conflictedDocs = new List <DocumentConflict>(documentsContext.DocumentDatabase.DocumentsStorage.ConflictsStorage.GetConflictsFor(documentsContext, id));

            if (conflictedDocs.Count == 0)
            {
                var relevantLocalDoc = documentsContext.DocumentDatabase.DocumentsStorage
                                       .GetDocumentOrTombstone(
                    documentsContext,
                    id);
                if (relevantLocalDoc.Document != null)
                {
                    conflictedDocs.Add(DocumentConflict.From(documentsContext, relevantLocalDoc.Document));
                }
                else if (relevantLocalDoc.Tombstone != null)
                {
                    conflictedDocs.Add(DocumentConflict.From(relevantLocalDoc.Tombstone));
                }
            }

            if (conflictedDocs.Count == 0)
            {
                InvalidConflictWhenThereIsNone(id);
            }

            conflictedDocs.Add(new DocumentConflict
            {
                LowerId      = conflictedDocs[0].LowerId,
                Id           = conflictedDocs[0].Id,
                Collection   = documentsContext.GetLazyStringForFieldWithCaching(collection),
                ChangeVector = incomingChangeVector,
                Doc          = doc
            });

            if (_conflictResolver.TryResolveConflictByScriptInternal(
                    documentsContext,
                    scriptResolver,
                    conflictedDocs,
                    documentsContext.GetLazyString(collection), out var resolved))
            {
                _conflictResolver.PutResolvedDocument(documentsContext, resolved);
                return(true);
            }

            return(false);
        }