Ejemplo n.º 1
0
        /// <summary>
        /// Handles documents that have been mutated when change tracking is enabled and adds them to the modified list.
        /// </summary>
        /// <param name="mutatedDocument"></param>
        void ITrackedDocumentNodeCallback.DocumentModified(ITrackedDocumentNode mutatedDocument)
        {
            object document;

            if (_tracked.TryGetValue(((DocumentNode)mutatedDocument).Metadata.Id, out document))
            {
                _modified.AddOrUpdate(((DocumentNode)mutatedDocument).Metadata.Id, document, (k, v) => document);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Flag this node as modified, and trigger any registered <see cref="ITrackedDocumentNodeCallback" /> callbacks.
        /// </summary>
        public virtual void DocumentModified(ITrackedDocumentNode mutatedDocument)
        {
            if (!IsDirty && !IsDeserializing)
            {
                // Don't trigger callbacks if already marked as dirty before
                // And don't mark as dirty or trigger callbacks if still deserializing

                // Set as dirty before triggering callbacks to prevent accidental infinite recursion
                IsDirty = true;

                //Context.Track(this);

                TriggerCallbacks();
            }
        }
Ejemplo n.º 3
0
        public void AddChild(ITrackedDocumentNode child)
        {
            lock (_childDocuments)
            {
                if (_childDocuments.ContainsKey(child))
                {
                    _childDocuments[child] += 1;
                }
                else
                {
                    _childDocuments.Add(child, 1);

                    child.RegisterChangeTracking(this);
                }
            }
        }
Ejemplo n.º 4
0
        public void AddChild(ITrackedDocumentNode child)
        {
            lock (_childDocuments)
            {
                if (_childDocuments.ContainsKey(child))
                {
                    _childDocuments[child] += 1;
                }
                else
                {
                    _childDocuments.Add(child, 1);

                    child.RegisterChangeTracking(this);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Saves the specified document to a Couchbase server cluster. If change tracking is enabled via <see cref="BeginChangeTracking"/>
        /// then the document will be added to the modified list and then saved when <see cref="SubmitChanges()"/> is called.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the document being saved.</typeparam>
        /// <param name="document">The document.</param>
        /// <exception cref="KeyAttributeMissingException">The document id could not be found.</exception>
        /// <exception cref="AmbiguousMatchException">More than one of the requested attributes was found.</exception>
        /// <exception cref="TypeLoadException">A custom attribute type cannot be loaded.</exception>
        /// <exception cref="CouchbaseWriteException">An exception wrapping the <see cref="IOperationResult"/> interface. Use this to determine what failed.</exception>
        public void Save <T>(T document)
        {
            var id = GetDocumentId(document);

            if (ChangeTrackingEnabled)
            {
                var context = this as IChangeTrackableContext;

                ITrackedDocumentNode documentWrapper = document as ITrackedDocumentNode;
                if (documentWrapper == null)
                {
                    // This is a new document, so wrap in NewDocumentWrapper

                    documentWrapper = new NewDocumentWrapper
                    {
                        Value    = document,
                        Metadata = new DocumentMetadata {
                            Id = id
                        },
                        IsDirty = true
                    };

                    context.Track(documentWrapper);
                }
                else
                {
                    documentWrapper.IsDeleted = false;
                    documentWrapper.IsDirty   = true;
                }

                context.Modified(documentWrapper);
            }
            else
            {
                var result = Bucket.Upsert(id, document);
                if (!result.Success)
                {
                    throw new CouchbaseWriteException(result);
                }

                AddToMutationState(result.Token);
            }
        }
Ejemplo n.º 6
0
        public void RemoveChild(ITrackedDocumentNode child)
        {
            lock (_childDocuments)
            {
                int refCount;
                if (_childDocuments.TryGetValue(child, out refCount))
                {
                    refCount -= 1;

                    if (refCount > 0)
                    {
                        _childDocuments[child] = refCount;
                    }
                    else
                    {
                        _childDocuments.Remove(child);

                        child.UnregisterChangeTracking(this);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void RemoveChild(ITrackedDocumentNode child)
        {
            lock (_childDocuments)
            {
                int refCount;
                if (_childDocuments.TryGetValue(child, out refCount))
                {
                    refCount -= 1;

                    if (refCount > 0)
                    {
                        _childDocuments[child] = refCount;
                    }
                    else
                    {
                        _childDocuments.Remove(child);

                        child.UnregisterChangeTracking(this);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Flag this node as modified, and trigger any registered <see cref="ITrackedDocumentNodeCallback" /> callbacks.
        /// </summary>
        public virtual void DocumentModified(ITrackedDocumentNode mutatedDocument)
        {
            if (!IsDirty && !IsDeserializing)
            {
                // Don't trigger callbacks if already marked as dirty before
                // And don't mark as dirty or trigger callbacks if still deserializing

                // Set as dirty before triggering callbacks to prevent accidental infinite recursion
                IsDirty = true;

                //Context.Track(this);

                TriggerCallbacks();
            }
        }