public void Store(object entity) { var shardId = shardResolutionStrategy.GenerateShardIdFor(entity, this); DatabaseCommands = Shards[shardId].AsyncDatabaseCommands; database = MultiDatabase.GetDatabaseName(Shards[shardId].Url); string id; if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false) { id = generateEntityIdOnTheClient.GetOrGenerateDocumentKey(entity); } var modifyDocumentId = shardStrategy.ModifyDocumentId(shardedDocumentStore.Conventions, shardId, id); BulkInsertOperation bulkInsertOperation; if (Bulks.TryGetValue(shardId, out bulkInsertOperation) == false) { var shard = Shards[shardId]; bulkInsertOperation = new BulkInsertOperation(database, shard, shard.Listeners, options, shard.Changes()); Bulks.Add(shardId, bulkInsertOperation); } bulkInsertOperation.Store(entity, modifyDocumentId); }
public void Store(object entity) { var shardId = shardResolutionStrategy.GenerateShardIdFor(entity, this); var shard = shards[shardId]; BulkInsertOperation bulkInsertOperation; if (Bulks.TryGetValue(shardId, out bulkInsertOperation) == false) { var actualDatabaseName = database ?? ((dynamic)shard).DefaultDatabase ?? MultiDatabase.GetDatabaseName(shard.Url); bulkInsertOperation = new BulkInsertOperation(actualDatabaseName, shard, shard.Listeners, options, shard.Changes()); Bulks.Add(shardId, bulkInsertOperation); } DatabaseCommands = string.IsNullOrWhiteSpace(database) ? shard.AsyncDatabaseCommands : shard.AsyncDatabaseCommands.ForDatabase(database); string id; if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false) { id = generateEntityIdOnTheClient.GetOrGenerateDocumentKey(entity); } var modifyDocumentId = shardStrategy.ModifyDocumentId(shardedDocumentStore.Conventions, shardId, id); bulkInsertOperation.Store(entity, modifyDocumentId); }
public Task StoreAsync(object entity) { string id; var hasId = GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id); return(StoreAsyncInternal(entity, null, null, forceConcurrencyCheck: hasId == false)); }
/// <summary> /// Creates the put entity command. /// </summary> /// <param name="entity">The entity.</param> /// <param name="documentMetadata">The document metadata.</param> /// <returns></returns> protected ICommandData CreatePutEntityCommand(object entity, DocumentMetadata documentMetadata) { string id; if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) && documentMetadata.Key != null && documentMetadata.Key.Equals(id, StringComparison.InvariantCultureIgnoreCase) == false) { throw new InvalidOperationException("Entity " + entity.GetType().FullName + " had document key '" + documentMetadata.Key + "' but now has document key property '" + id + "'." + Environment.NewLine + "You cannot change the document key property of a entity loaded into the session"); } var json = EntityToJson.ConvertEntityToJson(documentMetadata.Key, entity, documentMetadata.Metadata); var etag = UseOptimisticConcurrency || documentMetadata.ForceConcurrencyCheck ? (documentMetadata.ETag ?? Guid.Empty) : (Guid?)null; return(new PutCommandData { Document = json, Etag = etag, Key = documentMetadata.Key, Metadata = (RavenJObject)documentMetadata.Metadata.CloneToken(), }); }
/// <summary> /// Stores the specified entity in the session. The entity will be saved when SaveChanges is called. /// </summary> public void Store(object entity) { string id; var hasId = GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id); StoreInternal(entity, null, null, forceConcurrencyCheck: hasId == false); }
private DocumentMetadata GetDocumentMetadata <T>(T instance) { DocumentMetadata value; if (entitiesAndMetadata.TryGetValue(instance, out value) == false) { string id; if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(instance, out id) || (instance is IDynamicMetaObjectProvider && GenerateEntityIdOnTheClient.TryGetIdFromDynamic(instance, out id)) ) { AssertNoNonUniqueInstance(instance, id); var jsonDocument = GetJsonDocument(id); entitiesByKey[id] = instance; entitiesAndMetadata[instance] = value = new DocumentMetadata { ETag = UseOptimisticConcurrency ? (Guid?)Guid.Empty : null, Key = id, OriginalMetadata = jsonDocument.Metadata, Metadata = (RavenJObject)jsonDocument.Metadata.CloneToken(), OriginalValue = new RavenJObject() }; } else { throw new InvalidOperationException("Could not find the document key for " + instance); } } return(value); }
/// <summary> /// Determines if the entity have changed. /// </summary> /// <param name="entity">The entity.</param> /// <param name="documentMetadata">The document metadata.</param> /// <returns></returns> protected bool EntityChanged(object entity, DocumentMetadata documentMetadata) { if (documentMetadata == null) { return(true); } string id; if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) && string.Equals(documentMetadata.Key, id, StringComparison.InvariantCultureIgnoreCase) == false) { return(true); } // prevent saves of a modified read only entity if (documentMetadata.OriginalMetadata.ContainsKey(Constants.RavenReadOnly) && documentMetadata.OriginalMetadata.Value <bool>(Constants.RavenReadOnly) && documentMetadata.Metadata.ContainsKey(Constants.RavenReadOnly) && documentMetadata.Metadata.Value <bool>(Constants.RavenReadOnly)) { return(false); } var newObj = EntityToJson.ConvertEntityToJson(documentMetadata.Key, entity, documentMetadata.Metadata); return(RavenJToken.DeepEquals(newObj, documentMetadata.OriginalValue) == false || RavenJToken.DeepEquals(documentMetadata.Metadata, documentMetadata.OriginalMetadata) == false); }
private string GetId(object entity) { string id; if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false) { id = generateEntityIdOnTheClient.GenerateDocumentKeyForStorage(entity); } return(id); }
private string GetId(object entity) { string id; if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false) { id = generateEntityIdOnTheClient.GenerateDocumentKeyForStorage(entity); generateEntityIdOnTheClient.TrySetIdentity(entity, id); //set Id property if it was null } return(id); }
protected Task <string> GetOrGenerateDocumentKeyAsync(object entity) { string id; GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id); Task <string> generator = id != null ? CompletedTask.With(id) : GenerateKeyAsync(entity); return(generator.ContinueWith(task => { if (task.Result != null && task.Result.StartsWith("/")) { throw new InvalidOperationException("Cannot use value '" + id + "' as a document id because it begins with a '/'"); } return task.Result; })); }
public async Task StoreAsync(object entity) { var shardId = shardResolutionStrategy.GenerateShardIdFor(entity, this); var shard = shards[shardId]; BulkInsertOperation bulkInsertOperation; if (Bulks.TryGetValue(shardId, out bulkInsertOperation) == false) { var actualDatabaseName = database ?? ((DocumentStore)shard).DefaultDatabase ?? MultiDatabase.GetDatabaseName(shard.Url); bulkInsertOperation = new BulkInsertOperation(actualDatabaseName, shard, shard.Listeners); Bulks.Add(shardId, bulkInsertOperation); } DatabaseCommands = shard.AsyncDatabaseCommands; string id; if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false) { id = generateEntityIdOnTheClient.GetOrGenerateDocumentKey(entity); } var modifyDocumentId = shardStrategy.ModifyDocumentId(shardedDocumentStore.Conventions, shardId, id); await bulkInsertOperation.StoreAsync(entity, modifyDocumentId).ConfigureAwait(false); }