public Etag AddAttachment(string key, Etag etag, Stream data, RavenJObject headers) { Api.JetSetCurrentIndex(session, Files, "by_name"); Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ); if (isUpdate) { var existingEtag = Etag.Parse(Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"])); if (existingEtag != etag && etag != null) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag }; } } else { if (data == null) throw new InvalidOperationException("When adding new attachment, the attachment data must be specified"); if (Api.TryMoveFirst(session, Details)) Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1); } Etag newETag = uuidGenerator.CreateSequentialUuid(UuidType.Attachments); using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode); if (data != null) { long written; using (var columnStream = new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"])) { if (isUpdate) columnStream.SetLength(0); using (var stream = new BufferedStream(columnStream)) { data.CopyTo(stream); written = stream.Position; stream.Flush(); } } if (written == 0) // empty attachment { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["data"], new byte[0]); } } Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting()); Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode); update.Save(); } logger.Debug("Adding attachment {0}", key); return newETag; }
public bool TryResolveConflict(string id, RavenJObject metadata, byte[] data, Attachment existingAttachment, Func<string, Attachment> getAttachment, out RavenJObject metadataToSave, out byte[] dataToSave) { var success = TryResolve(id, metadata, data, existingAttachment, getAttachment, out metadataToSave, out dataToSave); if (success == false) return false; var metaToSave = metadataToSave; log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on attachment with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing metadata:")); if (existingAttachment != null && existingAttachment.Metadata != null) builder.AppendLine(existingAttachment.Metadata.ToString()); builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) builder.AppendLine(metadata.ToString()); builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) builder.AppendLine(metaToSave.ToString()); return builder.ToString(); }); return true; }
public void Basic() { var ravenJObject = new RavenJObject { {"Val", 3.3f} }; var s = ravenJObject.ToString(Formatting.None); Assert.Equal("{\"Val\":3.3}", s); }
public bool TryResolveConflict(string id, RavenJObject metadata, RavenJObject document, JsonDocument existingDoc, Func <string, JsonDocument> getDocument, out RavenJObject metadataToSave, out RavenJObject documentToSave) { var success = TryResolve(id, metadata, document, existingDoc, getDocument, out metadataToSave, out documentToSave); if (success == false) { return(false); } var docToSave = documentToSave; var metaToSave = metadataToSave; if (log.IsDebugEnabled) { log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on document with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing document:")); if (existingDoc != null && existingDoc.DataAsJson != null) { builder.AppendLine(existingDoc.DataAsJson.ToString()); } builder.AppendLine(string.Format("Existing metadata:")); if (existingDoc != null && existingDoc.Metadata != null) { builder.AppendLine(existingDoc.Metadata.ToString()); } builder.AppendLine(string.Format("Incoming document:")); if (document != null) { builder.AppendLine(document.ToString()); } builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) { builder.AppendLine(metadata.ToString()); } builder.AppendLine(string.Format("Output document:")); if (docToSave != null) { builder.AppendLine(docToSave.ToString()); } builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) { builder.AppendLine(metaToSave.ToString()); } return(builder.ToString()); }); } return(true); }
public void Basic() { var ravenJObject = new RavenJObject { { "Val", 3.3f } }; var s = ravenJObject.ToString(Formatting.None); Assert.Equal("{\"Val\":3.3}", s); }
/// <summary> /// Puts the document with the specified key in the database /// </summary> /// <param name="key">The key.</param> /// <param name="etag">The etag.</param> /// <param name="document">The document.</param> /// <param name="metadata">The metadata.</param> public Task <PutResult> PutAsync(string key, Guid?etag, RavenJObject document, RavenJObject metadata) { if (metadata == null) { metadata = new RavenJObject(); } var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; if (etag != null) { metadata["ETag"] = new RavenJValue(etag.Value.ToString()); } var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention); request.AddOperationHeaders(OperationsHeaders); return(request.WriteAsync(document.ToString()) .ContinueWith(task => { if (task.Exception != null) { throw new InvalidOperationException("Unable to write to server", task.Exception); } return request.ReadResponseStringAsync() .ContinueWith(task1 => { try { return JsonConvert.DeserializeObject <PutResult>(task1.Result, new JsonEnumConverter(), new JsonToJsonConverter()); } catch (AggregateException e) { var webexception = e.ExtractSingleInnerException() as WebException; if (ShouldThrowForPutAsync(webexception)) { throw; } throw ThrowConcurrencyException(webexception); } catch (WebException e) { if (ShouldThrowForPutAsync(e)) { throw; } throw ThrowConcurrencyException(e); } }); }) .Unwrap()); }
private void UpdateMetadata(RavenJObject metadataAsJson) { metadata = metadataAsJson.ToDictionary(x => x.Key, x => { if (x.Value.Type == JTokenType.String) { return(x.Value.Value <string>()); } return(x.Value.ToString(Formatting.None)); }); OnPropertyChanged(() => Metadata); JsonMetadata = metadataAsJson.ToString(Formatting.Indented); }
/// <summary> /// Puts the document with the specified key in the database /// </summary> /// <param name="key">The key.</param> /// <param name="etag">The etag.</param> /// <param name="document">The document.</param> /// <param name="metadata">The metadata.</param> public Task <PutResult> PutAsync(string key, Guid?etag, RavenJObject document, RavenJObject metadata) { if (metadata == null) { metadata = new RavenJObject(); } var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; if (etag != null) { metadata["ETag"] = new RavenJValue(etag.Value.ToString()); } var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention); request.AddOperationHeaders(OperationsHeaders); return(Task.Factory.FromAsync(request.BeginWrite, request.EndWrite, document.ToString(), null) .ContinueWith(task => { if (task.Exception != null) { throw new InvalidOperationException("Unable to write to server"); } return request.ReadResponseJsonAsync() .ContinueWith(task1 => { try { return convention.CreateSerializer().Deserialize <PutResult>(new RavenJTokenReader(task1.Result)); } catch (AggregateException e) { var we = e.ExtractSingleInnerException() as WebException; if (we == null) { throw; } var httpWebResponse = we.Response as HttpWebResponse; if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.Conflict) { throw; } throw ThrowConcurrencyException(we); } }); }) .Unwrap()); }
public void WhenCloningWillRetainAllValues() { var newBlog = new Blog() { Tags = new[]{ new BlogTag() { Name = "SuperCallaFragalisticExpealadocious" } } }; var expected = RavenJObject.FromObject(newBlog); var actual = new RavenJObject(expected); Assert.Equal(expected.ToString(Formatting.None), actual.ToString(Formatting.None)); }
public bool TryResolveConflict(string id, RavenJObject metadata, byte[] data, Attachment existingAttachment, Func <string, Attachment> getAttachment, out RavenJObject metadataToSave, out byte[] dataToSave) { var success = TryResolve(id, metadata, data, existingAttachment, getAttachment, out metadataToSave, out dataToSave); if (success == false) { return(false); } // here we make sure that we keep a deleted attachment deleted, rather than "reviving" it. var ravenDeleteMarker = existingAttachment.Metadata.Value <string>("Raven-Delete-Marker"); bool markerValue; if (ravenDeleteMarker != null && bool.TryParse(ravenDeleteMarker, out markerValue) && markerValue) { existingAttachment.Metadata["Raven-Remove-Document-Marker"] = true; } var metaToSave = metadataToSave; if (log.IsDebugEnabled) { log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on attachment with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing metadata:")); if (existingAttachment != null && existingAttachment.Metadata != null) { builder.AppendLine(existingAttachment.Metadata.ToString()); } builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) { builder.AppendLine(metadata.ToString()); } builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) { builder.AppendLine(metaToSave.ToString()); } return(builder.ToString()); }); } return(true); }
public static void WriteData(this IHttpContext context, RavenJObject data, RavenJObject headers, Etag etag) { var str = data.ToString(Formatting.None); var jsonp = context.Request.QueryString["jsonp"]; if (string.IsNullOrEmpty(jsonp) == false) { str = jsonp + "(" + str + ");"; context.Response.AddHeader("Content-Type", "application/javascript; charset=utf-8"); } else { context.Response.AddHeader("Content-Type", "application/json; charset=utf-8"); } WriteData(context, defaultEncoding.GetBytes(str), headers, etag); }
public void WhenCloningWillRetainAllValues() { var newBlog = new Blog { Tags = new[] { new BlogTag { Name = "SuperCallaFragalisticExpealadocious" } } }; var expected = RavenJObject.FromObject(newBlog); var actual = new RavenJObject(expected); Assert.Equal(expected.ToString(Formatting.None), actual.ToString(Formatting.None)); }
/// <summary> /// Puts the document with the specified key in the database /// </summary> /// <param name="key">The key.</param> /// <param name="etag">The etag.</param> /// <param name="document">The document.</param> /// <param name="metadata">The metadata.</param> public Task <PutResult> PutAsync(string key, Guid?etag, RavenJObject document, RavenJObject metadata) { if (metadata == null) { metadata = new RavenJObject(); } var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; if (etag != null) { metadata["ETag"] = new RavenJValue(etag.Value.ToString()); } var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention); request.AddOperationHeaders(OperationsHeaders); var bytes = Encoding.UTF8.GetBytes(document.ToString()); return(Task.Factory.FromAsync(request.BeginWrite, request.EndWrite, bytes, null) .ContinueWith(task => { if (task.Exception != null) { throw new InvalidOperationException("Unable to write to server"); } return Task.Factory.FromAsync <string>(request.BeginReadResponseString, request.EndReadResponseString, null) .ContinueWith(task1 => { try { return JsonConvert.DeserializeObject <PutResult>(task1.Result, Default.Converters); } catch (WebException e) { var httpWebResponse = e.Response as HttpWebResponse; if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.Conflict) { throw; } throw ThrowConcurrencyException(e); } }); }) .Unwrap()); }
private static async Task ConvertSagaType(DocumentStore docStore, SagaConversion conversion) { log.Info(""); log.Info(""); foreach (var sagaDocument in docStore.AllDocumentsStartingWith(conversion.DocumentPrefix, 1024)) { var data = sagaDocument.DataAsJson; var idString = sagaDocument.Key.Substring(conversion.DocumentPrefix.Length); var sagaId = new Guid(idString); RavenJObject metadata = new RavenJObject(); metadata["OriginalMessageId"] = data["OriginalMessageId"]; metadata["Originator"] = data["Originator"]; var metadataText = metadata.ToString(); data.Remove("OriginalMessageId"); data.Remove("Originator"); var dataText = data.ToString(); var correlationValue = data[conversion.CorrelationId].Value <string>(); using (var connection = Configuration.CreateSqlConnection()) { connection.Open(); using (var cmd = connection.CreateCommand()) { cmd.CommandText = conversion.GetSagaInsertCommandText(); AddParameter(cmd, "Id", sagaId); AddParameter(cmd, "Metadata", metadataText); AddParameter(cmd, "Data", dataText); AddParameter(cmd, "PersistenceVersion", PersistenceVersion.ToString()); AddParameter(cmd, "SagaTypeVersion", new Version(0, 0, 0, 0).ToString()); AddParameter(cmd, "CorrelationId", correlationValue); await cmd.ExecuteNonQueryAsync(); } string output = $"DocPrefix:{conversion.DocumentPrefix} Id: {sagaId} CorrelationId:{correlationValue}"; log.Info(output); Console.WriteLine(output); } } }
public Guid AddAttachment(string key, Guid? etag, Stream data, RavenJObject headers) { Api.JetSetCurrentIndex(session, Files, "by_name"); Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ); if (isUpdate) { var existingEtag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(); if (existingEtag != etag && etag != null) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag.Value }; } } else { if (Api.TryMoveFirst(session, Details)) Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1); } Guid newETag = uuidGenerator.CreateSequentialUuid(); using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode); using (var stream = new BufferedStream(new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"]))) { data.CopyTo(stream); stream.Flush(); } Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting()); Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode); update.Save(); } logger.Debug("Adding attachment {0}", key); return newETag; }
public bool TryResolveConflict(string id, RavenJObject metadata, byte[] data, Attachment existingAttachment, Func <string, Attachment> getAttachment, out RavenJObject metadataToSave, out byte[] dataToSave) { var success = TryResolve(id, metadata, data, existingAttachment, getAttachment, out metadataToSave, out dataToSave); if (success == false) { return(false); } var metaToSave = metadataToSave; if (log.IsDebugEnabled) { log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on attachment with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing metadata:")); if (existingAttachment != null && existingAttachment.Metadata != null) { builder.AppendLine(existingAttachment.Metadata.ToString()); } builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) { builder.AppendLine(metadata.ToString()); } builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) { builder.AppendLine(metaToSave.ToString()); } return(builder.ToString()); }); } return(true); }
protected void InsertItems(string tableName, string pkName, IEnumerable <ItemToReplicate> dataForTable) { tableName = tableName.ToLowerInvariant(); // type names have to be lowercase foreach (var itemToReplicate in dataForTable) { var o = new RavenJObject(); if (database != null) { database.WorkContext.CancellationToken.ThrowIfCancellationRequested(); } foreach (var column in itemToReplicate.Columns.Where(column => column.Key != pkName)) { o[column.Key] = column.Value; } if ("_id".Equals(pkName)) { bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\",\"_id\":\"" + itemToReplicate.DocumentId + "\"}}"); } else { o[pkName] = itemToReplicate.DocumentId; bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\"}}"); } // Setup timestamps, converting from a Javascript notation to an ES/Kibana notation if (o.ContainsKey("$timestamp")) { o["@timestamp"] = o["$timestamp"]; o.Remove("$timestamp"); } else { o["@timestamp"] = DateTime.UtcNow; } bulkCommands.Add(o.ToString(Formatting.None)); } }
/// <summary> /// Periodically poll the database to trigger fail-over (if appropriate). /// </summary> /// <param name="primaryDocumentStore"> /// The primary document store. /// </param> /// <param name="documentId"> /// The Id of the document to load when polling the database. /// </param> /// <returns> /// An <see cref="IDisposable"/> that, when disposed, stops the polling. /// </returns> static IDisposable PollDatabaseForFailOver(DocumentStore primaryDocumentStore, string documentId) { if (primaryDocumentStore == null) { throw new ArgumentNullException(nameof(primaryDocumentStore)); } if (String.IsNullOrWhiteSpace(documentId)) { throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'documentId'.", nameof(documentId)); } // Periodically load a well-known document from the database to trigger replication fail-over (if required). return(Observable.Interval(TimeSpan.FromSeconds(5)).Subscribe(_ => { try { using (IDocumentSession session = primaryDocumentStore.OpenSession()) { RavenJObject document = session.Load <RavenJObject>(documentId); if (document != null) { Log.Information("{DocumentId} = {DocumentJson}", documentId, document.ToString(Formatting.None) ); } else { Log.Warning("{DocumentId} is missing", documentId); } } } catch (Exception eLoadDocument) { Log.Error(eLoadDocument, "Unable to load {DocumentId}: {ErrorMessage}", documentId, eLoadDocument.Message); } })); }
public bool TryResolveConflict(string id, RavenJObject metadata, RavenJObject document, JsonDocument existingDoc, Func<string, JsonDocument> getDocument, out RavenJObject metadataToSave, out RavenJObject documentToSave) { var success = TryResolve(id, metadata, document, existingDoc, getDocument, out metadataToSave, out documentToSave); if (success == false) return false; var docToSave = documentToSave; var metaToSave = metadataToSave; log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on document with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing document:")); if (existingDoc != null && existingDoc.DataAsJson != null) builder.AppendLine(existingDoc.DataAsJson.ToString()); builder.AppendLine(string.Format("Existing metadata:")); if (existingDoc != null && existingDoc.Metadata != null) builder.AppendLine(existingDoc.Metadata.ToString()); builder.AppendLine(string.Format("Incoming document:")); if (document != null) builder.AppendLine(document.ToString()); builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) builder.AppendLine(metadata.ToString()); builder.AppendLine(string.Format("Output document:")); if (docToSave != null) builder.AppendLine(docToSave.ToString()); builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) builder.AppendLine(metaToSave.ToString()); return builder.ToString(); }); return true; }
private PutResult DirectPut(RavenJObject metadata, string key, Guid?etag, RavenJObject document, string operationUrl) { if (metadata == null) { metadata = new RavenJObject(); } var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; AddTransactionInformation(metadata); if (etag != null) { metadata["ETag"] = new RavenJValue(etag.Value.ToString()); } var request = jsonRequestFactory.CreateHttpJsonRequest(this, operationUrl + "/docs/" + key, method, metadata, credentials, convention); request.AddOperationHeaders(OperationsHeaders); request.Write(document.ToString()); string readResponseString; try { readResponseString = request.ReadResponseString(); } catch (WebException e) { var httpWebResponse = e.Response as HttpWebResponse; if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.Conflict) { throw; } throw ThrowConcurrencyException(e); } return(JsonConvert.DeserializeObject <PutResult>(readResponseString, Default.Converters)); }
private void UpdateMetadata(RavenJObject metadataAsJson) { metadata = metadataAsJson.ToDictionary(x => x.Key, x => { if (x.Value.Type == JTokenType.String) return x.Value.Value<string>(); return x.Value.ToString(Formatting.None); }); OnPropertyChanged(() => Metadata); JsonMetadata = metadataAsJson.ToString(Formatting.Indented); }
private static string PrepareRawJsonString(RavenJObject json) { return(json.ToString(Formatting.Indented)); }
private static string PrepareRawJsonString(RavenJObject json) { return json.ToString(Formatting.Indented); }
public Guid AddAttachment(string key, Guid?etag, Stream data, RavenJObject headers) { Api.JetSetCurrentIndex(session, Files, "by_name"); Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ); if (isUpdate) { var existingEtag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(); if (existingEtag != etag && etag != null) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag.Value }; } } else { if (Api.TryMoveFirst(session, Details)) { Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1); } } Guid newETag = uuidGenerator.CreateSequentialUuid(); using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode); using (var stream = new BufferedStream(new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"]))) { data.CopyTo(stream); stream.Flush(); } Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting()); Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode); update.Save(); } logger.Debug("Adding attachment {0}", key); return(newETag); }
public BlogAdminModule(IDocumentSession session) : base("Blog") { var blogConfig = session.Load<BlogConfig>("NSemble/Configs/MyBlog"); Get["/"] = o => { ViewBag.ModulePrefix = AreaRoutePrefix.TrimEnd('/'); Model.RecentPosts = session.Query<BlogPost>() .Where(x => x.CurrentState == BlogPost.State.Public) .OrderByDescending(x => x.PublishedAt) .Take(10) .ToArray(); Model.Drafts = session.Query<BlogPost>() .Where(x => x.CurrentState == BlogPost.State.Draft) .OrderByDescending(x => x.PublishedAt) .Take(10) .ToArray(); return View["Home", Model]; }; Get["/post-new/"] = p => View["Edit", new BlogPost { ContentType = DynamicContentType.Markdown, AllowComments = true, CurrentState = BlogPost.State.Draft, }]; Post["/post-new/"] = p => { var post = this.Bind<BlogPost>(); bool validated = true; if (!validated) { //ModelState.AddModelError("Id", ""); return View["Edit", post]; } // Set some defaults post.AllowComments = true; var identity = (User)Context.CurrentUser; post.AuthorId = identity.Id; string tags = Request.Form.TagsAsString; post.Tags = new HashSet<string>(); if (!String.IsNullOrWhiteSpace(tags)) { foreach (var tag in tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { post.Tags.Add(tag.Trim()); } } if ("Publish".Equals(Request.Form["SubmitAction"])) { post.CurrentState = BlogPost.State.Public; post.PublishedAt = DateTimeOffset.UtcNow; } else { post.CurrentState = BlogPost.State.Draft; post.PublishedAt = DateTimeOffset.MinValue; } // Render and cache the output post.CachedRenderedContent = post.CompiledContent(true).ToHtmlString(); session.Store(post, "BlogPosts/"); session.SaveChanges(); return Response.AsRedirect(post.ToUrl(AreaRoutePrefix.TrimEnd('/'))); }; Get[@"/edit/(?<id>\d+)"] = p => { var blogPost = session.Load<BlogPost>((int)p.id); if (blogPost == null) return 404; return View["Edit", blogPost]; }; Post[@"/edit/(?<id>\d+)"] = p => { var blogPost = session.Load<BlogPost>((int)p.id); if (blogPost == null) return 404; var input = this.Bind<BlogPost>(); bool validated = true; if (!validated) { //ModelState.AddModelError("Id", ""); return View["Edit", input]; } blogPost.Title = input.Title; blogPost.Content = input.Content; string tags = Request.Form.TagsAsString; blogPost.Tags = new HashSet<string>(); if (!String.IsNullOrWhiteSpace(tags)) { foreach (var tag in tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { blogPost.Tags.Add(tag.Trim()); } } blogPost.LastEditedAt = DateTimeOffset.UtcNow; if ("Publish".Equals(Request.Form["SubmitAction"])) { blogPost.CurrentState = BlogPost.State.Public; if (blogPost.PublishedAt == DateTimeOffset.MinValue) { blogPost.PublishedAt = DateTimeOffset.UtcNow; } } // Update the cached rendered page blogPost.CachedRenderedContent = blogPost.CompiledContent(true).ToHtmlString(); session.SaveChanges(); return Response.AsRedirect(input.ToUrl(AreaRoutePrefix.TrimEnd('/'))); }; Get[@"/stats/{days?7}/{type?all}"] = o => { var ret = new RavenJObject(); if (blogConfig != null) { var url = string.Format(@"http://stats.wordpress.com/csv.php?api_key={0}&blog_id={1}&format=json", blogConfig.WordPressAPIKey, blogConfig.WordPressBlogId); using (var webClient = new System.Net.WebClient()) { // TODO async, not UTF8 compatible switch ((string)o.type) { case "searchterms": ret.Add("searchterms", RavenJToken.Parse(webClient.DownloadString(url + "&table=searchterms"))); break; case "clicks": ret.Add("clicks", RavenJToken.Parse(webClient.DownloadString(url + "&table=clicks"))); break; case "referrers": ret.Add("referrers", RavenJToken.Parse(webClient.DownloadString(url + "&table=referrers_grouped"))); break; case "views": default: ret.Add("histogram", RavenJToken.Parse(webClient.DownloadString(url))); break; } if ("all".Equals((string) o.type)) { ret.Add("searchterms", RavenJToken.Parse(webClient.DownloadString(url + "&table=searchterms&days=2"))); ret.Add("clicks", RavenJToken.Parse(webClient.DownloadString(url + "&table=clicks&days=2"))); ret.Add("referrers", RavenJToken.Parse(webClient.DownloadString(url + "&table=referrers_grouped&days=2"))); } } } return Response.AsText(ret.ToString(), "text/json"); }; Get[@"/config"] = o => { using (session.Advanced.DocumentStore.DisableAggressiveCaching()) { var config = session.Load<BlogConfig>("NSemble/Configs/" + AreaConfigs.AreaName); return View["Config", config]; } }; Get[@"/config/widgets"] = o => { using (session.Advanced.DocumentStore.DisableAggressiveCaching()) { var config = session.Load<BlogConfig>("NSemble/Configs/" + AreaConfigs.AreaName); return View["ConfigWidgets", config.Widgets.ToArray()]; } }; }
protected void InsertItems(string tableName, string pkName, IEnumerable<ItemToReplicate> dataForTable) { tableName = tableName.ToLowerInvariant(); // type names have to be lowercase foreach (var itemToReplicate in dataForTable) { var o = new RavenJObject(); if (database != null) database.WorkContext.CancellationToken.ThrowIfCancellationRequested(); foreach (var column in itemToReplicate.Columns.Where(column => column.Key != pkName)) { o[column.Key] = column.Value; } if ("_id".Equals(pkName)) { bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\",\"_id\":\"" + itemToReplicate.DocumentId + "\"}}"); } else { o[pkName] = itemToReplicate.DocumentId; bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\"}}"); } // Setup timestamps, converting from a Javascript notation to an ES/Kibana notation if (o.ContainsKey("$timestamp")) { o["@timestamp"] = o["$timestamp"]; o.Remove("$timestamp"); } else { o["@timestamp"] = DateTime.UtcNow; } bulkCommands.Add(o.ToString(Formatting.None)); } }
/// <summary> /// Puts the document with the specified key in the database /// </summary> /// <param name="key">The key.</param> /// <param name="etag">The etag.</param> /// <param name="document">The document.</param> /// <param name="metadata">The metadata.</param> public Task<PutResult> PutAsync(string key, Guid? etag, RavenJObject document, RavenJObject metadata) { if (metadata == null) metadata = new RavenJObject(); var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; if (etag != null) metadata["ETag"] = new RavenJValue(etag.Value.ToString()); var request = jsonRequestFactory.CreateHttpJsonRequest( new CreateHttpJsonRequestParams(this, url + "/docs/" + key, method, metadata, credentials, convention) .AddOperationHeaders(OperationsHeaders)); return Task.Factory.FromAsync(request.BeginWrite,request.EndWrite,document.ToString(), null) .ContinueWith(task => { if (task.Exception != null) throw new InvalidOperationException("Unable to write to server"); return request.ReadResponseJsonAsync() .ContinueWith(task1 => { try { return convention.CreateSerializer().Deserialize<PutResult>(new RavenJTokenReader(task1.Result)); } catch (AggregateException e) { var we = e.ExtractSingleInnerException() as WebException; if (we == null) throw; var httpWebResponse = we.Response as HttpWebResponse; if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.Conflict) throw; throw ThrowConcurrencyException(we); } }); }) .Unwrap(); }
private void HandleRevicedNotification(RavenJObject ravenJObject) { var value = ravenJObject.Value <RavenJObject>("Value"); var type = ravenJObject.Value <string>("Type"); if (logger.IsDebugEnabled) { logger.Debug("Got notification from {0} id {1} of type {2}", url, id, ravenJObject.ToString()); } switch (type) { case "Disconnect": webSocket.Dispose(); break; case "Confirm": TaskCompletionSource <object> source; if (_sendConfirmations.TryRemove(ravenJObject.Value <int>("CommandId"), out source)) { source.TrySetResult(null); } break; case "Initialized": case "Heartbeat": throw new NotSupportedException(); // Should be deleted default: NotifySubscribers(type, value, Counters.ValuesSnapshot); break; } }
public bool TryResolveConflict(string id, RavenJObject metadata, RavenJObject document, JsonDocument existingDoc, Func <string, JsonDocument> getDocument, out RavenJObject metadataToSave, out RavenJObject documentToSave) { var success = TryResolve(id, metadata, document, existingDoc, getDocument, out metadataToSave, out documentToSave); if (success == false) { return(false); } var history = ReplicationData.GetHistory(metadata); var existingHistory = ReplicationData.GetHistory(existingDoc.Metadata); ReplicationData.SetHistory(metadataToSave, Historian.MergeReplicationHistories(history, existingHistory)); metadataToSave[Constants.RavenReplicationMergedHistory] = true; // here we make sure that we keep a deleted document deleted, rather than "reviving" it. var ravenDeleteMarker = existingDoc.Metadata.Value <string>("Raven-Delete-Marker"); bool markerValue; if (ravenDeleteMarker != null && bool.TryParse(ravenDeleteMarker, out markerValue) && markerValue) { existingDoc.Metadata["Raven-Remove-Document-Marker"] = true; } var docToSave = documentToSave; var metaToSave = metadataToSave; if (log.IsDebugEnabled) { log.Debug(() => { var builder = new StringBuilder(); builder.AppendLine(string.Format("Conflict on document with key '{0}' resolved by '{1}'.", id, GetType().Name)); builder.AppendLine(string.Format("Existing document:")); if (existingDoc != null && existingDoc.DataAsJson != null) { builder.AppendLine(existingDoc.DataAsJson.ToString()); } builder.AppendLine(string.Format("Existing metadata:")); if (existingDoc != null && existingDoc.Metadata != null) { builder.AppendLine(existingDoc.Metadata.ToString()); } builder.AppendLine(string.Format("Incoming document:")); if (document != null) { builder.AppendLine(document.ToString()); } builder.AppendLine(string.Format("Incoming metadata:")); if (metadata != null) { builder.AppendLine(metadata.ToString()); } builder.AppendLine(string.Format("Output document:")); if (docToSave != null) { builder.AppendLine(docToSave.ToString()); } builder.AppendLine(string.Format("Output metadata:")); if (metaToSave != null) { builder.AppendLine(metaToSave.ToString()); } return(builder.ToString()); }); } return(true); }
/// <summary> /// Puts the document with the specified key in the database /// </summary> /// <param name="key">The key.</param> /// <param name="etag">The etag.</param> /// <param name="document">The document.</param> /// <param name="metadata">The metadata.</param> public Task<PutResult> PutAsync(string key, Guid? etag, RavenJObject document, RavenJObject metadata) { if (metadata == null) metadata = new RavenJObject(); var method = String.IsNullOrEmpty(key) ? "POST" : "PUT"; if (etag != null) metadata["ETag"] = new RavenJValue(etag.Value.ToString()); var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention); request.AddOperationHeaders(OperationsHeaders); return Task.Factory.FromAsync(request.BeginWrite,request.EndWrite,document.ToString(), null) .ContinueWith(task => { if (task.Exception != null) throw new InvalidOperationException("Unable to write to server"); return Task.Factory.FromAsync<string>(request.BeginReadResponseString,request.EndReadResponseString, null) .ContinueWith(task1 => { try { return JsonConvert.DeserializeObject<PutResult>(task1.Result, Default.Converters); } catch (WebException e) { var httpWebResponse = e.Response as HttpWebResponse; if (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.Conflict) throw; throw ThrowConcurrencyException(e); } }); }) .Unwrap(); }
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, }; }