public SmugglerApi(SmugglerOptions smugglerOptions, IAsyncDatabaseCommands commands, Action<string> output) : base(smugglerOptions) { this.commands = commands; this.output = output; batch = new List<RavenJObject>(); }
private static async Task CreateTransformersAsync(Assembly assemblyToScan, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions) { foreach (var task in GetAllInstancesOfType <AbstractTransformerCreationTask>(assemblyToScan)) { await task.ExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } }
/// <summary> /// Executes the index creation against the specified document store. /// </summary> public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken)) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); if (documentConvention.PrettifyGeneratedLinqExpressions) { var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token); if (serverDef != null) { if (serverDef.Equals(indexDefinition)) { return; } // now we need to check if this is a legacy index... var legacyIndexDefinition = GetLegacyIndexDefinition(documentConvention); if (serverDef.Equals(legacyIndexDefinition)) { return; // if it matches the legacy definition, do not change that (to avoid re-indexing) } } } // This code take advantage on the fact that RavenDB will turn an index PUT // to a noop of the index already exists and the stored definition matches // the new definition. await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true, token); if (Conventions.IndexAndTransformerReplicationMode.HasFlag(IndexAndTransformerReplicationMode.Indexes)) { await ReplicateIndexesIfNeededAsync(asyncDatabaseCommands); } }
public Task <string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var shardId = shardedDocumentStore.ShardStrategy.ShardResolutionStrategy.MetadataShardIdFor(entity); if (shardId == null) { throw new InvalidOperationException(string.Format( "ShardResolutionStrategy.MetadataShardIdFor cannot return null. You must specify where to store the metadata documents for the entity type '{0}'.", entity.GetType().FullName)); } AsyncMultiTypeHiLoKeyGenerator value; if (generatorsByShard.TryGetValue(shardId, out value)) { return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); } lock (this) { if (generatorsByShard.TryGetValue(shardId, out value) == false) { value = new AsyncMultiTypeHiLoKeyGenerator(capacity); generatorsByShard = new Dictionary <string, AsyncMultiTypeHiLoKeyGenerator>(generatorsByShard) { { shardId, value } }; } } return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); }
/// <summary> /// Executes the index creation against the specified document store. /// </summary> public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); if (documentConvention.PrettifyGeneratedLinqExpressions) { var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName); if (serverDef != null) { if (serverDef.Equals(indexDefinition)) { return; } // now we need to check if this is a legacy index... var legacyIndexDefinition = GetLegacyIndexDefinition(documentConvention); if (serverDef.Equals(legacyIndexDefinition)) { return; // if it matches the legacy definition, do not change that (to avoid re-indexing) } } } // This code take advantage on the fact that RavenDB will turn an index PUT // to a noop of the index already exists and the stored definition matches // the new definition. await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true); await UpdateIndexInReplicationAsync(asyncDatabaseCommands, documentConvention, (client, operationMetadata) => client.DirectPutIndexAsync(IndexName, indexDefinition, true, operationMetadata)); }
///<summary> /// Ensures that the database exists, creating it if needed ///</summary> public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false) { self = self.ForDefaultDatabase(); var doc = MultiDatabase.CreateDatabaseDocument(name); var docId = "Raven/Databases/" + name; return(self.GetAsync(docId) .ContinueWith(get => { if (get.Result != null) { return get; } return (Task)self.PutAsync(docId, null, doc, new RavenJObject()); }) .Unwrap() .ContinueWith(x => { if (ignoreFailures == false) { x.Wait(); // will throw on error } var observedException = x.Exception; GC.KeepAlive(observedException); })); }
/// <summary> /// Initializes a new instance of the <see cref="RavenQueryInspector{T}"/> class. /// </summary> public RavenQueryInspector( IRavenQueryProvider provider, RavenQueryStatistics queryStats, string indexName, Expression expression, InMemoryDocumentSessionOperations session #if !SILVERLIGHT , IDatabaseCommands databaseCommands #endif #if !NET35 , IAsyncDatabaseCommands asyncDatabaseCommands #endif ) { if (provider == null) { throw new ArgumentNullException("provider"); } this.provider = provider.For <T>(); this.queryStats = queryStats; this.indexName = indexName; this.session = session; #if !SILVERLIGHT this.databaseCommands = databaseCommands; #endif #if !NET35 this.asyncDatabaseCommands = asyncDatabaseCommands; #endif this.provider.AfterQueryExecuted(queryStats.UpdateQueryStats); this.expression = expression ?? Expression.Constant(this); }
public Task <string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetDynamicTagName(entity); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags { return(CompletedTask.With <string>(null)); } var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); AsyncHiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); } lock (generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); } value = new AsyncHiLoKeyGenerator(tag, capacity); keyGeneratorsByTag.TryAdd(tag, value); } return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; this.documentStore = documentStore; Statistics = new Observable <DatabaseStatistics>(); Status = new Observable <string> { Value = "Offline" }; OnPropertyChanged(() => StatusImage); asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForSystemDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); DocumentChanges.Select(c => Unit.Default).Merge(IndexChanges.Select(c => Unit.Default)) .SampleResponsive(TimeSpan.FromSeconds(2)) .Subscribe(_ => RefreshStatistics()); databaseChanges.ConnectionStatusChanged += (sender, args) => { ApplicationModel.Current.Server.Value.SetConnected(((IDatabaseChanges)sender).Connected); UpdateStatus(); }; RefreshStatistics(); }
///<summary> /// Ensures that the database exists, creating it if needed ///</summary> public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false) { var serverClient = self.ForDefaultDatabase() as AsyncServerClient; if (serverClient == null) { throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self); } var doc = MultiDatabase.CreateDatabaseDocument(name); var docId = "Raven/Databases/" + name; return(serverClient.GetAsync(docId) .ContinueWith(get => { if (get.Result != null) { return get; } var req = serverClient.CreateRequest("PUT", "/admin/databases/" + Uri.EscapeDataString(name)); req.Write(doc.ToString(Formatting.Indented)); return req.ExecuteRequestAsync(); }) .Unwrap() .ContinueWith(x => { if (ignoreFailures == false) { x.Wait(); // will throw on error } var observedException = x.Exception; GC.KeepAlive(observedException); })); }
/// <summary> /// Initializes a new instance of the <see cref="RavenQueryInspector{T}"/> class. /// </summary> public void Init( IRavenQueryProvider provider, RavenQueryStatistics queryStats, RavenQueryHighlightings highlightings, string indexName, Expression expression, InMemoryDocumentSessionOperations session , IDatabaseCommands databaseCommands , IAsyncDatabaseCommands asyncDatabaseCommands, bool isMapReduce ) { if (provider == null) { throw new ArgumentNullException("provider"); } this.provider = provider.For <T>(); this.queryStats = queryStats; this.highlightings = highlightings; this.indexName = indexName; this.session = session; this.databaseCommands = databaseCommands; this.asyncDatabaseCommands = asyncDatabaseCommands; this.isMapReduce = isMapReduce; this.provider.AfterQueryExecuted(this.AfterQueryExecuted); this.expression = expression ?? Expression.Constant(this); }
public DatabaseTask(IAsyncDatabaseCommands databaseCommands, string taskName, string databaseName) { this.databaseCommands = databaseCommands; this.taskName = taskName; this.databaseName = databaseName; OutputItems = new ObservableCollection<DatabaseTaskOutput>(); }
/// <summary> /// Executes the index creation against the specified document store. /// </summary> public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken)) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); if (documentConvention.PrettifyGeneratedLinqExpressions) { var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false); if (serverDef != null && CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition)) { await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false); return; } } // This code take advantage on the fact that RavenDB will turn an index PUT // to a noop of the index already exists and the stored definition matches // the new definition. await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true, token).ConfigureAwait(false); if (Priority != null) { await asyncDatabaseCommands.SetIndexPriorityAsync(IndexName, Priority.Value, token).ConfigureAwait(false); } await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false); }
public Task <string> GenerateDocumentKeyAsync(string dbName, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var generator = generators.GetOrAdd(dbName ?? Constants.SystemDatabase, s => new AsyncMultiTypeHiLoKeyGenerator(capacity)); return(generator.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); }
private async Task <JsonDocument> GetDocumentAsync(IAsyncDatabaseCommands databaseCommands) { var documents = await databaseCommands.GetAsync(new[] { HiLoDocumentKey, RavenKeyServerPrefix }, new string[0]); if (documents.Results.Count == 2 && documents.Results[1] != null) { lastServerPrefix = documents.Results[1].Value <string>("ServerPrefix"); } else { lastServerPrefix = string.Empty; } if (documents.Results.Count == 0 || documents.Results[0] == null) { return(null); } var jsonDocument = documents.Results[0].ToJsonDocument(); foreach (var key in jsonDocument.Metadata.Keys.Where(x => x.StartsWith("@")).ToArray()) { jsonDocument.Metadata.Remove(key); } return(jsonDocument); }
private IAsyncDocumentSession OpenAsyncSessionInternal(string dbName, IAsyncDatabaseCommands asyncDatabaseCommands) { AssertInitialized(); EnsureNotClosed(); var sessionId = Guid.NewGuid(); currentSessionId = sessionId; try { if (AsyncDatabaseCommands == null) { throw new InvalidOperationException("You cannot open an async session because it is not supported on embedded mode"); } var session = new AsyncDocumentSession(dbName, this, asyncDatabaseCommands, Listeners, sessionId) { DatabaseName = dbName ?? DefaultDatabase }; AfterSessionCreated(session); return(session); } finally { currentSessionId = null; } }
public Task<string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var shardId = shardedDocumentStore.ShardStrategy.ShardResolutionStrategy.MetadataShardIdFor(entity); if (shardId == null) throw new InvalidOperationException(string.Format( "ShardResolutionStrategy.MetadataShardIdFor cannot return null. You must specify where to store the metadata documents for the entity type '{0}'.", entity.GetType().FullName)); AsyncMultiTypeHiLoKeyGenerator value; if (generatorsByShard.TryGetValue(shardId, out value)) return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); lock (this) { if (generatorsByShard.TryGetValue(shardId, out value) == false) { value = new AsyncMultiTypeHiLoKeyGenerator(capacity); generatorsByShard = new Dictionary<string, AsyncMultiTypeHiLoKeyGenerator>(generatorsByShard) { {shardId, value} }; } } return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); }
private async Task <RangeValue> HandleConflictsAsync(IAsyncDatabaseCommands databaseCommands, ConflictException e, long minNextMax) { // resolving the conflict by selecting the highest number long highestMax = -1; if (e.ConflictedVersionIds.Length == 0) { throw new InvalidOperationException("Got conflict exception, but no conflicted versions", e); } foreach (var conflictedVersionId in e.ConflictedVersionIds) { var doc = await databaseCommands.GetAsync(conflictedVersionId); highestMax = Math.Max(highestMax, GetMaxFromDocument(doc, minNextMax)); } await PutDocumentAsync(databaseCommands, new JsonDocument { Etag = e.Etag, Metadata = new RavenJObject(), DataAsJson = RavenJObject.FromObject(new { Max = highestMax }), Key = HiLoDocumentKey }); return(await GetNextRangeAsync(databaseCommands)); }
/// <summary> /// Initializes a new instance of the <see cref="RavenQueryProvider{T}"/> class. /// </summary> public RavenQueryProvider( IDocumentQueryGenerator queryGenerator, string indexName, RavenQueryStatistics ravenQueryStatistics #if !SILVERLIGHT , IDatabaseCommands databaseCommands #endif #if !NET_3_5 , IAsyncDatabaseCommands asyncDatabaseCommands #endif ) { FieldsToFetch = new HashSet <string>(); FieldsToRename = new Dictionary <string, string>(); this.queryGenerator = queryGenerator; this.indexName = indexName; this.ravenQueryStatistics = ravenQueryStatistics; #if !SILVERLIGHT this.databaseCommands = databaseCommands; #endif #if !NET_3_5 this.asyncDatabaseCommands = asyncDatabaseCommands; #endif }
internal Subscription(long id, string database, SubscriptionConnectionOptions options, IAsyncDatabaseCommands commands, IDatabaseChanges changes, DocumentConvention conventions, bool open, Func <Task> ensureOpenSubscription) { this.id = id; this.options = options; this.commands = commands; this.changes = changes; this.conventions = conventions; this.ensureOpenSubscription = ensureOpenSubscription; if (typeof(T) != typeof(RavenJObject)) { isStronglyTyped = true; generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(conventions, entity => AsyncHelpers.RunSync(() => conventions.GenerateDocumentKeyAsync(database, commands, entity))); } if (open) { Start(); } else { if (options.Strategy != SubscriptionOpeningStrategy.WaitForFree) { throw new InvalidOperationException("Subscription isn't open while its opening strategy is: " + options.Strategy); } } if (options.Strategy == SubscriptionOpeningStrategy.WaitForFree) { WaitForSubscriptionReleased(); } }
///<summary> /// Ensures that the database exists, creating it if needed ///</summary> public static Task EnsureDatabaseExistsAsync(this IAsyncDatabaseCommands self, string name, bool ignoreFailures = false) { var serverClient = self.ForDefaultDatabase() as AsyncServerClient; if (serverClient == null) { throw new InvalidOperationException("Ensuring database existence requires a Server Client but got: " + self); } var doc = MultiDatabase.CreateDatabaseDocument(name); var docId = "Raven/Databases/" + name; return(serverClient.GetAsync(docId) .ContinueWith(get => { if (get.Result != null) { return get; } return (Task)serverClient.PutAsync(docId, null, doc, new RavenJObject()); }) .Unwrap() .ContinueWith(x => { if (ignoreFailures == false) { x.Wait(); // will throw on error } var observedException = x.Exception; GC.KeepAlive(observedException); })); }
private async Task ReplicateTransformerIfNeededAsync(IAsyncDatabaseCommands databaseCommands) { var serverClient = databaseCommands as AsyncServerClient; if (serverClient == null) { return; } var replicateTransformerUrl = String.Format("/replication/replicate-transformers?transformerName={0}", Uri.EscapeDataString(TransformerName)); using (var replicateTransformerRequest = serverClient.CreateRequest(replicateTransformerUrl, HttpMethods.Post)) { try { await replicateTransformerRequest.ExecuteRawResponseAsync().ContinueWith(t => { t.Result.Dispose(); }).ConfigureAwait(false); } catch (Exception) { // ignoring error } } }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; this.documentStore = documentStore; Tasks = new BindableCollection<TaskModel>(x => x.Name) { new ImportTask(), new ExportTask(), new StartBackupTask(), new IndexingTask(), new SampleDataTask() }; SelectedTask = new Observable<TaskModel> { Value = Tasks.FirstOrDefault() }; Statistics = new Observable<DatabaseStatistics>(); Status = new Observable<string> { Value = "Offline" }; asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForDefaultDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); DocumentChanges.Select(c => Unit.Default).Merge(IndexChanges.Select(c => Unit.Default)) .SampleResponsive(TimeSpan.FromSeconds(2)) .Subscribe(_ => RefreshStatistics(), exception => ApplicationModel.Current.Server.Value.IsConnected.Value = false); RefreshStatistics(); }
public virtual async Task SideBySideExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, Etag minimumEtagBeforeReplace = null, DateTime?replaceTimeUtc = null, CancellationToken token = default(CancellationToken)) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false); if (serverDef != null) { if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition)) { return; } var replaceIndexName = "ReplacementOf/" + IndexName; await asyncDatabaseCommands.PutIndexAsync(replaceIndexName, indexDefinition, token).ConfigureAwait(false); await asyncDatabaseCommands .PutAsync(Constants.IndexReplacePrefix + replaceIndexName, null, RavenJObject.FromObject(new IndexReplaceDocument { IndexToReplace = serverDef.Name, MinimumEtagBeforeReplace = minimumEtagBeforeReplace, ReplaceTimeUtc = replaceTimeUtc }), new RavenJObject(), token).ConfigureAwait(false); } else { // since index doesn't exist yet - create it in normal mode await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, token).ConfigureAwait(false); } }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; this.documentStore = documentStore; Statistics = new Observable<DatabaseStatistics>(); Status = new Observable<string> { Value = "Offline" }; OnPropertyChanged(() => StatusImage); asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForSystemDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); DocumentChanges.Select(c => Unit.Default).Merge(IndexChanges.Select(c => Unit.Default)) .SampleResponsive(TimeSpan.FromSeconds(2)) .Subscribe(_ => RefreshStatistics()); databaseChanges.ConnectionStatusChanged += (sender, args) => { ApplicationModel.Current.Server.Value.SetConnected(((IDatabaseChanges)sender).Connected); UpdateStatus(); }; RefreshStatistics(); }
public virtual async Task AfterExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken)) { if (documentConvention.IndexAndTransformerReplicationMode.HasFlag(IndexAndTransformerReplicationMode.Indexes)) { await ReplicateIndexesIfNeededAsync(asyncDatabaseCommands).ConfigureAwait(false); } }
public SmugglerApi(SmugglerOptions smugglerOptions, IAsyncDatabaseCommands commands, Action <string> output) : base(smugglerOptions) { this.commands = commands; this.output = output; batch = new List <RavenJObject>(); }
/// <summary> /// Initializes a new instance of the <see cref="RavenQueryProvider{T}"/> class. /// </summary> public RavenQueryProvider( IDocumentQueryGenerator queryGenerator, string indexName, RavenQueryStatistics ravenQueryStatistics, RavenQueryHighlightings highlightings #if !SILVERLIGHT , IDatabaseCommands databaseCommands #endif , IAsyncDatabaseCommands asyncDatabaseCommands, bool isMapReduce ) { FieldsToFetch = new HashSet <string>(); FieldsToRename = new List <RenamedField>(); this.queryGenerator = queryGenerator; this.indexName = indexName; this.ravenQueryStatistics = ravenQueryStatistics; this.highlightings = highlightings; #if !SILVERLIGHT this.databaseCommands = databaseCommands; #endif this.asyncDatabaseCommands = asyncDatabaseCommands; this.isMapReduce = isMapReduce; }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; this.documentStore = documentStore; Tasks = new BindableCollection <TaskModel>(x => x.Name) { new ImportTask(), new ExportTask(), new StartBackupTask(), new IndexingTask(), new SampleDataTask() }; SelectedTask = new Observable <TaskModel> { Value = Tasks.FirstOrDefault() }; Statistics = new Observable <DatabaseStatistics>(); Status = new Observable <string> { Value = "Offline" }; asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForDefaultDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); DocumentChanges.Select(c => Unit.Default).Merge(IndexChanges.Select(c => Unit.Default)) .SampleResponsive(TimeSpan.FromSeconds(2)) .Subscribe(_ => RefreshStatistics(), exception => ApplicationModel.Current.Server.Value.IsConnected.Value = false); RefreshStatistics(); }
public virtual async Task SideBySideExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, Etag minimumEtagBeforeReplace = null, DateTime?replaceTimeUtc = null, CancellationToken token = default(CancellationToken)) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); if (minimumEtagBeforeReplace != null && indexDefinition.IsMapReduce) { throw new InvalidOperationException("We do not support side-by-side execution for Map-Reduce indexes when 'minimum last indexed etag' scenario is used."); } var replaceIndexName = Constants.SideBySideIndexNamePrefix + IndexName; //check if side by side index exists var sideBySideDef = await asyncDatabaseCommands.GetIndexAsync(replaceIndexName, token).ConfigureAwait(false); if (sideBySideDef != null) { if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, sideBySideDef, indexDefinition)) { return; } await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention).ConfigureAwait(false); return; } var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false); if (serverDef != null) { if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition)) { return; } switch (serverDef.LockMode) { case IndexLockMode.SideBySide: //keep the SideBySide lock mode from the replaced index indexDefinition.LockMode = IndexLockMode.SideBySide; break; case IndexLockMode.LockedIgnore: //Nothing to do we just ignore this index return; case IndexLockMode.LockedError: throw new InvalidOperationException(string.Format("Can't replace locked index {0} its lock mode is set to: LockedError", serverDef.IndexId)); } await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention).ConfigureAwait(false); } else { // since index doesn't exist yet - create it in normal mode await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, token).ConfigureAwait(false); await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false); } }
/// <summary> /// Initializes a new instance of the <see cref="DocumentQuery{T}"/> class. /// </summary> public DocumentQuery(InMemoryDocumentSessionOperations session , IDatabaseCommands databaseCommands , IAsyncDatabaseCommands asyncDatabaseCommands, string indexName, string[] fieldsToFetch, string[] projectionFields, IDocumentQueryListener[] queryListeners, bool isMapReduce) : base(session , databaseCommands , asyncDatabaseCommands, indexName, fieldsToFetch, projectionFields, queryListeners, isMapReduce) { }
private Task <RangeValue> GetNextRangeAsync(IAsyncDatabaseCommands databaseCommands) { var calculatedCapacity = Interlocked.Read(ref capacity); ModifyCapacityIfRequired(ref calculatedCapacity); return(GetNextMaxAsyncInner(databaseCommands, calculatedCapacity)); }
/// <summary> /// Initializes a new instance of the <see cref="AsyncDocumentSession"/> class. /// </summary> public AsyncDocumentSession(DocumentStore documentStore, IAsyncDatabaseCommands asyncDatabaseCommands, DocumentSessionListeners listeners, Guid id) : base(documentStore, listeners, id) { AsyncDatabaseCommands = asyncDatabaseCommands; }
private async Task <RangeValue> GetNextMaxAsyncInner(IAsyncDatabaseCommands databaseCommands) { var minNextMax = Range.Max; using (databaseCommands.ForceReadFromMaster()) while (true) { try { ConflictException ce = null; JsonDocument document; try { document = await GetDocumentAsync(databaseCommands).ConfigureAwait(false); } catch (ConflictException e) { ce = e; document = null; } if (ce != null) { return(await HandleConflictsAsync(databaseCommands, ce, minNextMax).ConfigureAwait(false)); } long min, max; if (document == null) { min = minNextMax + 1; max = minNextMax + capacity; document = new JsonDocument { Etag = Etag.Empty, // sending empty etag means - ensure the that the document does NOT exists Metadata = new RavenJObject(), DataAsJson = RavenJObject.FromObject(new { Max = max }), Key = HiLoDocumentKey }; } else { var oldMax = GetMaxFromDocument(document, minNextMax); min = oldMax + 1; max = oldMax + capacity; document.DataAsJson["Max"] = max; } await PutDocumentAsync(databaseCommands, document).ConfigureAwait(false); return(new RangeValue(min, max)); } catch (ConcurrencyException) { //expected & ignored, will retry this } } }
/// <summary> /// Asynchronously creates an index /// </summary> /// <typeparam name="T">The type that defines the index to be create.</typeparam> /// <param name="commands">The hook to the database commands.</param> /// <param name="overwrite">Should the index be overwritten if it already exists.</param> /// <returns></returns> public static Task <string> PutIndexAsync <T>(this IAsyncDatabaseCommands commands, bool overwrite) where T : AbstractIndexCreationTask, new() { var indexCreationTask = new T(); return(commands.PutIndexAsync( indexCreationTask.IndexName, indexCreationTask.CreateIndexDefinition(), overwrite)); }
/// <summary> /// Creates the indexes found in the specified catalog /// </summary> public static Task CreateIndexesAsync(ExportProvider catalogToGetnIndexingTasksFrom, IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention conventions) { var tasks = catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractIndexCreationTask>(); Task[] array = tasks.Select(task => task.ExecuteAsync(asyncDatabaseCommands, conventions)).ToArray(); var indexesAsync = new Task(() => Task.WaitAll(array)); indexesAsync.Start(); return indexesAsync; }
public DatabaseModel(string name, IAsyncDatabaseCommands asyncDatabaseCommands) { Name = name; this.asyncDatabaseCommands = asyncDatabaseCommands; Statistics = new Observable<DatabaseStatistics>(); Collections = new DatabaseCollectionsModel(asyncDatabaseCommands); RecentDocuments = new BindableCollection<ViewableDocument>(new PrimaryKeyComparer<ViewableDocument>(document => document.Id)); RecentDocuments = new BindableCollection<ViewableDocument>(new PrimaryKeyComparer<ViewableDocument>(document=>document.Id)); }
public DatabaseListItemModel(string name) { this.name = name; Statistics = new Observable<DatabaseStatistics>(); var documentStore = ApplicationModel.Current.Server.Value.DocumentStore; asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForDefaultDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); }
/// <summary> /// Executes the index creation against the specified document store. /// </summary> public virtual Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention) { Conventions = documentConvention; var transformerDefinition = CreateTransformerDefinition(); // This code take advantage on the fact that RavenDB will turn an index PUT // to a noop of the index already exists and the stored definition matches // the new definition. return asyncDatabaseCommands.PutTransformerAsync(TransformerName, transformerDefinition) .ContinueWith(task => UpdateIndexInReplicationAsync(asyncDatabaseCommands, documentConvention, (client, url) => client.DirectPutTransformerAsync(TransformerName, transformerDefinition, url))) .Unwrap(); }
public ExportDatabaseTask(IAsyncDatabaseCommands databaseCommands, string databaseName, bool includeAttachements, bool includeDocuments, bool includeIndexes, bool includeTransformers, bool shouldExcludeExpired, int batchSize, string transformScript, List<FilterSetting> filterSettings) : base(databaseCommands, databaseName, "Export Database") { this.includeAttachements = includeAttachements; this.includeDocuments = includeDocuments; this.includeIndexes = includeIndexes; this.includeTransformers = includeTransformers; this.shouldExcludeExpired = shouldExcludeExpired; this.batchSize = batchSize; this.transformScript = transformScript; this.filterSettings = filterSettings; }
public ImportImpl(StreamReader reader, string file, TaskModel taskModel, Action<string> output, IAsyncDatabaseCommands databaseCommands) { this.taskModel = taskModel; this.output = output; this.databaseCommands = databaseCommands; csvReader = new CsvReader(reader); header = csvReader.ReadHeaderRecord(); entity = Path.GetFileNameWithoutExtension(file); sw = Stopwatch.StartNew(); enumerator = csvReader.DataRecords.GetEnumerator(); }
public DatabaseModel(string name, IAsyncDatabaseCommands asyncDatabaseCommands) { Name = name; this.asyncDatabaseCommands = asyncDatabaseCommands; Tasks = new BindableCollection<TaskModel>(x => x.Name) { new ImportTask(asyncDatabaseCommands), new ExportTask(asyncDatabaseCommands) }; SelectedTask = new Observable<TaskModel> {Value = Tasks.FirstOrDefault()}; Statistics = new Observable<DatabaseStatistics>(); }
internal static async Task AfterExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, string indexName, ScriptedIndexResults scripts, CancellationToken token) { var documentId = GetScriptedIndexResultsDocumentId(indexName); scripts.Id = documentId; var oldDocument = await asyncDatabaseCommands.GetAsync(documentId, token).ConfigureAwait(false); var newDocument = RavenJObject.FromObject(scripts); if (oldDocument != null && RavenJToken.DeepEquals(oldDocument.DataAsJson, newDocument)) return; await asyncDatabaseCommands.PutAsync(documentId, null, newDocument, null, token).ConfigureAwait(false); await asyncDatabaseCommands.ResetIndexAsync(indexName, token).ConfigureAwait(false); }
public ImportImpl(StreamReader reader, string file, TaskModel taskModel, Action<string> output, IAsyncDatabaseCommands databaseCommands) { this.taskModel = taskModel; this.output = output; this.databaseCommands = databaseCommands; csvReader = new CsvReader(reader); header = csvReader.ReadHeaderRecord(); entity = Inflector.Pluralize(CSharpClassName.ConvertToValidClassName(Path.GetFileNameWithoutExtension(file))); if (entity.Length > 0 && char.IsLower(entity[0])) entity = char.ToUpper(entity[0]) + entity.Substring(1); sw = Stopwatch.StartNew(); enumerator = csvReader.DataRecords.GetEnumerator(); }
public BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners, BulkInsertOptions options, IDatabaseChanges changes) { this.documentStore = documentStore; database = database ?? MultiDatabase.GetDatabaseName(documentStore.Url); // Fitzchak: Should not be ever null because of the above code, please refactor this. DatabaseCommands = database == null ? documentStore.AsyncDatabaseCommands.ForSystemDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(database); generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore, entity => documentStore.Conventions.GenerateDocumentKeyAsync(database, DatabaseCommands, entity).ResultUnwrap()); Operation = GetBulkInsertOperation(options, DatabaseCommands, changes); entityToJson = new EntityToJson(documentStore, listeners); }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; Tasks = new BindableCollection<TaskModel>(x => x.Name) { new ImportTask(), new ExportTask(), new StartBackupTask(), }; SelectedTask = new Observable<TaskModel> {Value = Tasks.FirstOrDefault()}; Statistics = new Observable<DatabaseStatistics>(); asyncDatabaseCommands = name.Equals(DefaultDatabaseName, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForDefaultDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); }
///<summary> /// Create the next id (numeric) ///</summary> public Task<long> NextIdAsync(IAsyncDatabaseCommands databaseCommands) { var myRange = Range; // thread safe copy long incrementedCurrent = Interlocked.Increment(ref myRange.Current); if (incrementedCurrent <= myRange.Max) { return CompletedTask.With(incrementedCurrent); } bool lockTaken = false; try { generatorLock.Enter(ref lockTaken); if (Range != myRange) { // Lock was contended, and the max has already been changed. Just get a new id as usual. generatorLock.Exit(); return NextIdAsync(databaseCommands); } // Get a new max, and use the current value. return GetNextRangeAsync(databaseCommands) .ContinueWith(task => { try { Range = task.Result; } finally { generatorLock.Exit(); } return NextIdAsync(databaseCommands); }).Unwrap(); } catch { // We only unlock in exceptional cases (and not in a finally clause) because non exceptional cases will either have already // unlocked or will have started a task that will unlock in the future. if (lockTaken) generatorLock.Exit(); throw; } }
public DatabaseModel(string name, DocumentStore documentStore) { this.name = name; this.documentStore = documentStore; Tasks = new BindableCollection<TaskModel>(x => x.Name) { new ImportTask(), new ExportTask(), new StartBackupTask(), new IndexingTask(), new SampleDataTask(), new CsvImportTask() }; if (name == null || name == Constants.SystemDatabase) Tasks.Insert(3, new StartRestoreTask()); SelectedTask = new Observable<TaskModel> { Value = Tasks.FirstOrDefault() }; Statistics = new Observable<DatabaseStatistics>(); Status = new Observable<string> { Value = "Offline" }; OnPropertyChanged(() => StatusImage); asyncDatabaseCommands = name.Equals(Constants.SystemDatabase, StringComparison.OrdinalIgnoreCase) ? documentStore.AsyncDatabaseCommands.ForDefaultDatabase() : documentStore.AsyncDatabaseCommands.ForDatabase(name); DocumentChanges.Select(c => Unit.Default).Merge(IndexChanges.Select(c => Unit.Default)) .SampleResponsive(TimeSpan.FromSeconds(2)) .Subscribe(_ => RefreshStatistics()); databaseChanges.ConnectionStatusChanged += (sender, args) => { ApplicationModel.Current.Server.Value.SetConnected(((IDatabaseChanges)sender).Connected); UpdateStatus(); }; RefreshStatistics(); }
/// <summary> /// Creates the indexes found in the specified catalog /// </summary> public static async Task CreateIndexesAsync(ExportProvider catalogToGetnIndexingTasksFrom, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions) { var indexCompilationExceptions = new List<IndexCompilationException>(); var failed = false; try { var tasks = catalogToGetnIndexingTasksFrom .GetExportedValues<AbstractIndexCreationTask>() .ToList(); var indexesToAdd = CreateIndexesToAdd(tasks, conventions); await databaseCommands.PutIndexesAsync(indexesToAdd).ConfigureAwait(false); foreach (var task in tasks) await task.AfterExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } // For old servers that don't have the new endpoint for executing multiple indexes catch (Exception) { failed = true; } if (failed) { foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractIndexCreationTask>()) { try { await task.ExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } catch (IndexCompilationException e) { indexCompilationExceptions.Add(new IndexCompilationException("Failed to compile index name = " + task.IndexName, e)); } } } await CreateTransformersAsync(catalogToGetnIndexingTasksFrom, databaseCommands, conventions).ConfigureAwait(false); if (indexCompilationExceptions.Any()) throw new AggregateException("Failed to create one or more indexes. Please see inner exceptions for more details.", indexCompilationExceptions); }
public Task<string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetTypeTagName(entity.GetType()); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags return CompletedTask.With<string>(null); var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); AsyncHiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); lock(generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); value = new AsyncHiLoKeyGenerator(tag, capacity); keyGeneratorsByTag.TryAdd(tag, value); } return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); }
/// <summary> /// Creates the indexes found in the specified catalog /// </summary> public static async Task CreateIndexesAsync(ExportProvider catalogToGetnIndexingTasksFrom, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions) { var indexCompilationExceptions = new List<IndexCompilationException>(); foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractIndexCreationTask>()) { try { await task.ExecuteAsync(databaseCommands, conventions); } catch (IndexCompilationException e) { indexCompilationExceptions.Add(new IndexCompilationException("Failed to compile index name = " + task.IndexName, e)); } } foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractTransformerCreationTask>()) { await task.ExecuteAsync(databaseCommands, conventions); } if (indexCompilationExceptions.Any()) throw new AggregateException("Failed to create one or more indexes. Please see inner exceptions for more details.", indexCompilationExceptions); }
/// <summary> /// Creates the indexes found in the specified catalog in side-by-side mode. /// </summary> public static async Task SideBySideCreateIndexesAsync(ExportProvider catalogToGetnIndexingTasksFrom, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, Etag minimumEtagBeforeReplace = null, DateTime? replaceTimeUtc = null) { var indexCompilationExceptions = new List<IndexCompilationException>(); foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractIndexCreationTask>()) { try { await task.SideBySideExecuteAsync(databaseCommands, conventions, minimumEtagBeforeReplace, replaceTimeUtc).ConfigureAwait(false); } catch (IndexCompilationException e) { indexCompilationExceptions.Add(new IndexCompilationException("Failed to compile index name = " + task.IndexName, e)); } } foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractTransformerCreationTask>()) { await task.ExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } if (indexCompilationExceptions.Any()) throw new AggregateException("Failed to create one or more indexes. Please see inner exceptions for more details.", indexCompilationExceptions); }
public CreateSampleDataCommand(HomeModel model, IAsyncDatabaseCommands databaseCommands) { this.model = model; this.databaseCommands = databaseCommands; }
private Task<RangeValue> GetNextRangeAsync(IAsyncDatabaseCommands databaseCommands) { ModifyCapacityIfRequired(); return GetNextMaxAsyncInner(databaseCommands); }
/// <summary> /// Generates the document key. /// </summary> /// <param name="convention">The convention.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public Task<string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention convention, object entity) { return NextIdAsync(databaseCommands).ContinueWith(task => GetDocumentKeyFromId(convention, task.Result)); }
private Task<JsonDocument> GetDocumentAsync(IAsyncDatabaseCommands databaseCommands) { return databaseCommands.GetAsync(new[] { HiLoDocumentKey, RavenKeyServerPrefix }, new string[0]) .ContinueWith(task => { var documents = task.Result; if (documents.Results.Count == 2 && documents.Results[1] != null) { lastServerPrefix = documents.Results[1].Value<string>("ServerPrefix"); } else { lastServerPrefix = string.Empty; } if (documents.Results.Count == 0 || documents.Results[0] == null) return (JsonDocument)null; var jsonDocument = documents.Results[0].ToJsonDocument(); foreach (var key in jsonDocument.Metadata.Keys.Where(x => x.StartsWith("@")).ToArray()) { jsonDocument.Metadata.Remove(key); } return jsonDocument; }); }
private Task PutDocumentAsync(IAsyncDatabaseCommands databaseCommands, JsonDocument document) { return databaseCommands.PutAsync(HiLoDocumentKey, document.Etag, document.DataAsJson, document.Metadata); }
private Task<RangeValue> GetNextMaxAsyncInner(IAsyncDatabaseCommands databaseCommands) { var minNextMax = Range.Max; return GetDocumentAsync(databaseCommands) .ContinueWith(task => { try { JsonDocument document; try { document = task.Result; } catch (ConflictException e) { // resolving the conflict by selecting the highest number var highestMax = e.ConflictedVersionIds .Select(conflictedVersionId => databaseCommands.GetAsync(conflictedVersionId) .ContinueWith(t => GetMaxFromDocument(t.Result, minNextMax))) .AggregateAsync(Enumerable.Max); return highestMax .ContinueWith(t => PutDocumentAsync(databaseCommands, new JsonDocument { Etag = e.Etag, Metadata = new RavenJObject(), DataAsJson = RavenJObject.FromObject(new {Max = t.Result}), Key = HiLoDocumentKey })) .Unwrap() .ContinueWithTask(() => GetNextRangeAsync(databaseCommands)); } long min, max; if (document == null) { min = minNextMax + 1; max = minNextMax + capacity; document = new JsonDocument { Etag = Guid.Empty, // sending empty guid means - ensure the that the document does NOT exists Metadata = new RavenJObject(), DataAsJson = RavenJObject.FromObject(new {Max = max}), Key = HiLoDocumentKey }; } else { var oldMax = GetMaxFromDocument(document, minNextMax); min = oldMax + 1; max = oldMax + capacity; document.DataAsJson["Max"] = max; } return PutDocumentAsync(databaseCommands, document).WithResult(new RangeValue(min, max)); } catch (ConcurrencyException) { return GetNextMaxAsyncInner(databaseCommands); } }).Unwrap(); }
/// <summary> /// Creates the indexes found in the specified catalog /// </summary> public static async Task CreateIndexesAsync(ExportProvider catalogToGetnIndexingTasksFrom, IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions) { var indexCompilationExceptions = new List<IndexCompilationException>(); bool failed = false; try { var tasks = catalogToGetnIndexingTasksFrom .GetExportedValues<AbstractIndexCreationTask>() .ToList(); var indexesNames = tasks.Select(x => x.IndexName).ToArray(); var definitions = tasks.Select(x => x.CreateIndexDefinition()).ToArray(); var priorities = tasks.Select(x => x.Priority ?? IndexingPriority.Normal).ToArray(); await databaseCommands.PutIndexesAsync(indexesNames, definitions, priorities).ConfigureAwait(false); foreach (var task in tasks) await task.AfterExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } // For old servers that don't have the new entrypoint for executing multiple indexes catch (Exception) { failed = true; } if (failed) { foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractIndexCreationTask>()) { try { await task.ExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } catch (IndexCompilationException e) { indexCompilationExceptions.Add(new IndexCompilationException("Failed to compile index name = " + task.IndexName, e)); } } } foreach (var task in catalogToGetnIndexingTasksFrom.GetExportedValues<AbstractTransformerCreationTask>()) { await task.ExecuteAsync(databaseCommands, conventions).ConfigureAwait(false); } if (indexCompilationExceptions.Any()) throw new AggregateException("Failed to create one or more indexes. Please see inner exceptions for more details.", indexCompilationExceptions); }
private static IAsyncDatabaseCommands SetupCommandsAsync(IAsyncDatabaseCommands databaseCommands, string database, ICredentials credentialsForSession, OpenSessionOptions options) { if (database != null) databaseCommands = databaseCommands.ForDatabase(database); if (credentialsForSession != null) databaseCommands = databaseCommands.With(credentialsForSession); if (options.ForceReadFromMaster) databaseCommands.ForceReadFromMaster(); return databaseCommands; }