Ejemplo n.º 1
0
        private BatchResult Put(PutCommandData command)
        {
            if (command.Etag.HasValue && database.ContainsKey(new Guid(command.Key))) {
                var existing = database[new Guid(command.Key)];
                if (existing.Item2 != command.Etag.Value) {
                    throw new InvalidOperationException("Optimistic Concurrency Exception");
                }
            }

            var ret = new RavenJObject
                {
                    {"Key", command.Key},
                    {"Document", command.Document},
                    {"Metadata", command.Metadata},
                };

            var newEtag = GuidComb.NewGuid();
            var tuple = new Tuple<string, Guid, DateTime>(ret.ToString(Formatting.None), newEtag, DateTime.UtcNow);
            database[new Guid(command.Key)] = tuple;

            return new BatchResult() {
                Key = command.Key,
                Etag = newEtag,
                Method = command.Method,
                Metadata = command.Metadata,
            };
        }
Ejemplo n.º 2
0
        public void PutEntities(List<object> entities)
        {
            var commands = new List<PutCommandData>();
            foreach (var entity in entities)
            {
                var command = new PutCommandData();
                command.Document = RavenJObject.FromObject(entity);
                command.Key = GenerateId(entity);
                command.Metadata = CreateMetadata(entity);

                commands.Add(command);
            }

            var response = httpClient
                .PostAsync(databaseUrl + "/bulk_docs", new JsonContent(RavenJToken.FromObject(commands)))
                .ResultUnwrap();

            if (response.IsSuccessStatusCode == false)
                throw new InvalidOperationException(string.Format("BATCH failed. Code: {0}.", response.StatusCode));
        }
Ejemplo n.º 3
0
        private BatchResult Put(PutCommandData putCommand, StringBuilder batchCommandBuilder, Action<string, object> addParamWithValue, int cmdIndex)
        {
            var newEtag = GuidComb.NewGuid();
            var lastModified = DateTime.UtcNow;
            if (putCommand.Etag.HasValue && putCommand.Etag.Value != Guid.Empty) {
                batchCommandBuilder.AppendLine("UPDATE " + settings.DocumentTableName + " SET [ETag] = @etag" + cmdIndex + ", [LastModified] = @lastModified" + cmdIndex + ", [Document] = @document" + cmdIndex + ", [Metadata] = @metadata" + cmdIndex + " WHERE [Id] = @id" + cmdIndex + " AND [ETag] = @etag_old" + cmdIndex);
                addParamWithValue("@etag_old" + cmdIndex, putCommand.Etag.Value);
            } else {
                batchCommandBuilder.AppendLine("INSERT INTO " + settings.DocumentTableName + " ([Id], [ETag], [LastModified], [Document], [Metadata]) VALUES (@id" + cmdIndex + ", @etag" + cmdIndex + ", @lastModified" + cmdIndex + ", @document" + cmdIndex + ", @metadata" + cmdIndex + ");");
            }
            addParamWithValue("@id" + cmdIndex, new Guid(putCommand.Key));
            addParamWithValue("@etag" + cmdIndex, newEtag);
            addParamWithValue("@lastModified" + cmdIndex, lastModified);
            addParamWithValue("@document" + cmdIndex, putCommand.Document.ToString(Formatting.None));
            addParamWithValue("@metadata" + cmdIndex, putCommand.Metadata.ToString(Formatting.None));

            return new BatchResult() {
                Key = putCommand.Key,
                Etag = newEtag,
                Method = putCommand.Method,
                Metadata = putCommand.Metadata,
            };
        }
        public void Run(ISagaInfo sagaInfo)
        {
            PrintHelper.PrintFunctionCallDetails("PopulateSagaUniqueIdentityRecords", sagaInfo);
            var documentStore = new DocumentStore
            {
                Url = sagaInfo.RavenUrl,
                DefaultDatabase = sagaInfo.DatabaseName
            };

            documentStore.Initialize();

            var existingSagaUniqueIdentityUniqueValues = DocumentHelper.GetExistingSagaUniqueIdentityUniqueValues(documentStore, sagaInfo);
            var commandsList = new List<ICommandData>();
            var dictionarytest = new Dictionary<string, int>();
            const int pageSize = 100;
            for (var start = 0; ; start += pageSize)
            {
                var sagaDocuments = documentStore.DatabaseCommands.GetDocuments(start, pageSize);
                foreach (var document in sagaDocuments)
                {
                    if (document.Key.StartsWith(sagaInfo.SagaKeyPrefix) && DocumentHelper.HasServicebusUniqueValue(document))
                    {
                        var body = document.DataAsJson;
                        var uniqueValue = body.Value<string>(sagaInfo.UniqueField);
                        if (existingSagaUniqueIdentityUniqueValues.Exists(x => x == uniqueValue))
                        {
                            PrintHelper.PrintMessage(string.Format("Skip uniquevalue {0} because it already exists", uniqueValue));
                            continue; // Already has SagaUniqueIdentity record
                        }
                        var sagaId = document.Key.Replace(sagaInfo.SagaKeyPrefix + "/", "");

                        var documentBody = new RavenJObject();
                        documentBody.Add("SagaId", sagaId);
                        documentBody.Add("UniqueValue", uniqueValue);
                        documentBody.Add("SagaDocId", document.Key);

                        var documentMetadata = new RavenJObject();
                        documentMetadata.Add("Content-Type", "application/json; charset=utf-8");
                        documentMetadata.Add("Raven-Entity-Name", "SagaUniqueIdentity");
                        documentMetadata.Add("Raven-Clr-Type",
                                             "NServiceBus.SagaPersisters.Raven.SagaUniqueIdentity, NServiceBus.Core");

                        ICommandData command = new PutCommandData
                        {
                            Key = DocumentHelper.GenerateUniqueIdentityRecordId(uniqueValue, sagaInfo),
                            Document = documentBody,
                            Metadata = documentMetadata,
                        };

                        if (dictionarytest.ContainsKey(uniqueValue))
                        {
                            dictionarytest[uniqueValue] += 1;
                        }
                        else
                        {
                            dictionarytest[uniqueValue] = 1;
                        }

                        commandsList.Add(command);
                    }
                }

                if (sagaDocuments.Length < pageSize) break;
            }
            PrintHelper.PrintMessage(string.Format("Attempt to insert {0} SagaUniqueIdentity documents", commandsList.Count));
            foreach (var item in dictionarytest.Keys)
            {
                if (dictionarytest[item] > 1)
                {
                    PrintHelper.PrintMessage("the following has duplicate " + item + " : " + dictionarytest[item]);
                }
            }
            var results = documentStore.AsyncDatabaseCommands.BatchAsync(commandsList.ToArray());
            PrintHelper.PrintResults("PopulateSagaUniqueIdentityRecords", results);
        }
Ejemplo n.º 5
0
        private IEnumerable<ICommandData> MigratePerson1ToPerson2(RavenJObject doc, RavenJObject metadata)
        {
            var name = doc.Value<string>("Name");
            if (!string.IsNullOrEmpty(name))
            {
                doc["FirstName"] = name.Split(' ')[0];
                doc["LastName"] = name.Split(' ')[1];
            }
            doc.Remove("Name");

            metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person2, RavenMigrations.Tests";

            var foobaz = new FooBaz
            {
                Id = 1,
                Bar = "loaded"
            };

            var foobazDoc = RavenJObject.FromObject(foobaz);
            var meta = new RavenJObject();
            meta[Constants.RavenEntityName] = "FooBazs";
            var cmd = new PutCommandData
            {
                Document = foobazDoc,
                Key = "foobazs/" + foobaz.Id,
                Metadata = meta
            };

            return new[] {cmd};
        }