Exemplo n.º 1
0
        private void OnTransformerChange(DocumentDatabase documentDatabase, TransformerChangeNotification notification)
        {
            var transformerName = notification.Name;

            switch (notification.Type)
            {
            case TransformerChangeTypes.TransformerAdded:
                //if created transformer with the same name as deleted one, we should prevent its deletion replication
                Database.TransactionalStorage.Batch(accessor =>
                                                    accessor.Lists.Remove(Constants.RavenReplicationTransformerTombstones, transformerName));

                break;

            case TransformerChangeTypes.TransformerRemoved:
                //If we don't have any destination to replicate to (we are probably slave node)
                //we shouldn't keep a tombstone since we are not going to remove it anytime
                //and keeping it prevents us from getting that transformer created again.
                if (GetReplicationDestinations().Count == 0)
                {
                    return;
                }

                var metadata = new RavenJObject
                {
                    { Constants.RavenTransformerDeleteMarker, true },
                    { Constants.RavenReplicationSource, Database.TransactionalStorage.Id.ToString() },
                    { Constants.RavenReplicationVersion, ReplicationHiLo.NextId(Database) },
                    { "TransformerVersion", notification.Version }
                };

                Database.TransactionalStorage.Batch(accessor =>
                                                    accessor.Lists.Set(Constants.RavenReplicationTransformerTombstones, transformerName, metadata, UuidType.Transformers));
                break;
            }
        }
Exemplo n.º 2
0
        private void OnIndexChange(DocumentDatabase documentDatabase, IndexChangeNotification eventArgs)
        {
            switch (eventArgs.Type)
            {
            case IndexChangeTypes.IndexAdded:
                //if created index with the same name as deleted one, we should prevent its deletion replication
                database.TransactionalStorage.Batch(
                    accessor =>
                {
                    var li = accessor.Lists.Read(Constants.RavenReplicationIndexesTombstones, eventArgs.Name);
                    if (li == null)
                    {
                        return;
                    }
                    int version;
                    string versionStr = li.Data.Value <string>("IndexVersion");
                    if (int.TryParse(versionStr, out version))
                    {
                        if (eventArgs.Version.HasValue && version < eventArgs.Version.Value)
                        {
                            accessor.Lists.Remove(Constants.RavenReplicationIndexesTombstones, eventArgs.Name);
                        }
                    }
                    else
                    {
                        Log.Error("Failed to parse index version of index {0}", eventArgs.Name);
                    }
                });
                break;

            case IndexChangeTypes.IndexRemoved:
                //If we don't have any destination to replicate to (we are probably slave node)
                //we shouldn't keep a tombstone since we are not going to remove it anytime
                //and keeping it prevents us from getting that index created again.
                if (replication.GetReplicationDestinations().Length == 0)
                {
                    return;
                }
                var metadata = new RavenJObject
                {
                    { Constants.RavenIndexDeleteMarker, true },
                    { Constants.RavenReplicationSource, database.TransactionalStorage.Id.ToString() },
                    { Constants.RavenReplicationVersion, ReplicationHiLo.NextId(database) },
                    { "IndexVersion", eventArgs.Version }
                };

                database.TransactionalStorage.Batch(accessor => accessor.Lists.Set(Constants.RavenReplicationIndexesTombstones, eventArgs.Name, metadata, UuidType.Indexing));
                break;
            }
        }
Exemplo n.º 3
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase) &&             // we don't deal with system documents
                key.StartsWith("Raven/Hilo/", StringComparison.OrdinalIgnoreCase) == false) // except for hilos
            {
                return;
            }

            using (Database.DisableAllTriggersForCurrentThread())
            {
                var documentMetadata = GetDocumentMetadata(key);
                if (documentMetadata != null)
                {
                    var history = new RavenJArray(ReplicationData.GetHistory(documentMetadata));
                    metadata[Constants.RavenReplicationHistory] = history;

                    if (documentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
                        documentMetadata.ContainsKey(Constants.RavenReplicationSource))
                    {
                        var historyEntry = new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, documentMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, documentMetadata[Constants.RavenReplicationSource] }
                        };
                        if (history.Contains(historyEntry, RavenJTokenEqualityComparer.Default) == false)
                        {
                            history.Add(historyEntry);
                        }
                    }
                    else
                    {
                        history.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, 0 },
                            { Constants.RavenReplicationSource, RavenJToken.FromObject(Database.TransactionalStorage.Id) }
                        });
                    }

                    while (history.Length > Constants.ChangeHistoryLength)
                    {
                        history.RemoveAt(0);
                    }
                }

                metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(ReplicationHiLo.NextId(Database));
                metadata[Constants.RavenReplicationSource]  = RavenJToken.FromObject(Database.TransactionalStorage.Id);
            }
        }
Exemplo n.º 4
0
        private void OnIndexChange(DocumentDatabase documentDatabase, IndexChangeNotification notification)
        {
            var indexName = notification.Name;

            switch (notification.Type)
            {
            case IndexChangeTypes.IndexAdded:
                //if we created index with the same name as deleted one, we should prevent its deletion replication
                Database.TransactionalStorage.Batch(accessor =>
                                                    accessor.Lists.Remove(Constants.RavenReplicationIndexesTombstones, indexName));
                break;

            case IndexChangeTypes.IndexRemoved:
                var indexDefinition = Database.IndexDefinitionStorage.GetIndexDefinition(indexName);
                if (indexDefinition != null)
                {
                    //the side by side index was deleted
                    IndexToAdd _;
                    sideBySideIndexesToReplicate.TryRemove(indexDefinition.IndexId, out _);
                }

                //If we don't have any destination to replicate to (we are probably slave node)
                //we shouldn't keep a tombstone since we are not going to remove it anytime
                //and keeping it prevents us from getting that index created again.
                if (GetReplicationDestinations().Count == 0)
                {
                    return;
                }

                var metadata = new RavenJObject
                {
                    { Constants.RavenIndexDeleteMarker, true },
                    { Constants.RavenReplicationSource, Database.TransactionalStorage.Id.ToString() },
                    { Constants.RavenReplicationVersion, ReplicationHiLo.NextId(Database) },
                    { IndexDefinitionStorage.IndexVersionKey, notification.Version }
                };

                Database.TransactionalStorage.Batch(accessor => accessor.Lists.Set(Constants.RavenReplicationIndexesTombstones, indexName, metadata, UuidType.Indexing));
                break;
            }
        }
        private void OnTransformerChange(DocumentDatabase documentDatabase, TransformerChangeNotification eventArgs)
        {
            switch (eventArgs.Type)
            {
            case TransformerChangeTypes.TransformerAdded:
                //if created transformer with the same name as deleted one, we should prevent its deletion replication
                database.TransactionalStorage.Batch(accessor => accessor.Lists.Remove(Constants.RavenReplicationTransformerTombstones, eventArgs.Name));
                break;

            case TransformerChangeTypes.TransformerRemoved:
                var metadata = new RavenJObject
                {
                    { Constants.RavenTransformerDeleteMarker, true },
                    { Constants.RavenReplicationSource, database.TransactionalStorage.Id.ToString() },
                    { Constants.RavenReplicationVersion, ReplicationHiLo.NextId(database) }
                };

                database.TransactionalStorage.Batch(accessor => accessor.Lists.Set(Constants.RavenReplicationTransformerTombstones, eventArgs.Name, metadata, UuidType.Transformers));
                break;
            }
        }
Exemplo n.º 6
0
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase) &&             // we don't deal with system documents
                key.StartsWith("Raven/Hilo/", StringComparison.OrdinalIgnoreCase) == false) // except for hilos
            {
                return;
            }

            using (Database.DisableAllTriggersForCurrentThread())
            {
                var documentMetadata = GetDocumentMetadata(key);
                if (documentMetadata == null)
                {
                    var history = new RavenJArray()
                    {
                        new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, metadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, metadata[Constants.RavenReplicationSource] }
                        }
                    };
                }
                else
                {
                    var history = new RavenJArray(ReplicationData.GetOrCreateHistory(documentMetadata));

                    if (documentMetadata.ContainsKey(Constants.RavenReplicationMergedHistory) == false)
                    {
                        if (documentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
                            documentMetadata.ContainsKey(Constants.RavenReplicationSource))
                        {
                            history.Add(new RavenJObject
                            {
                                { Constants.RavenReplicationVersion, documentMetadata[Constants.RavenReplicationVersion] },
                                { Constants.RavenReplicationSource, documentMetadata[Constants.RavenReplicationSource] }
                            });
                        }
                        else
                        {
                            history.Add(new RavenJObject
                            {
                                { Constants.RavenReplicationVersion, 0 },
                                { Constants.RavenReplicationSource, RavenJToken.FromObject(Database.TransactionalStorage.Id) }
                            });
                        }

                        var sources = new HashSet <RavenJToken>(RavenJTokenEqualityComparer.Default);
                        int pos     = history.Length - 1;
                        for (; pos >= 0; pos--)
                        {
                            var source = ((RavenJObject)history[pos])[Constants.RavenReplicationSource];
                            if (sources.Contains(source))
                            {
                                history.RemoveAt(pos);
                                continue;
                            }
                            sources.Add(source);
                        }
                        metadata[Constants.RavenReplicationMergedHistory] = true;
                        metadata[Constants.RavenReplicationHistory]       = history;
                    }
                    //If we have the flag we must have Constants.RavenReplicationVersion and Constants.RavenReplicationSource too
                    //Here we assume that the replication history is in the form of a "sorted dictionary" so we just need to remove
                    //the entry with the current source id and insert the new version at the end of the history.
                    else
                    {
                        int i = history.Length - 1;
                        for (; i >= 0; i--)
                        {
                            var currentEntry = history[i];
                            if (RavenJTokenEqualityComparer.Default.Equals(((RavenJObject)currentEntry)
                                                                           [Constants.RavenReplicationSource], documentMetadata[Constants.RavenReplicationSource]))
                            {
                                break;
                            }
                        }
                        if (i != -1)
                        {
                            history.RemoveAt(i);
                        }
                        history.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, documentMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, documentMetadata[Constants.RavenReplicationSource] }
                        });
                        metadata[Constants.RavenReplicationHistory] = history;
                    }
                }

                metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(ReplicationHiLo.NextId(Database));
                metadata[Constants.RavenReplicationSource]  = RavenJToken.FromObject(Database.TransactionalStorage.Id);
            }
        }