/// <summary> /// Safely add the index to the RavenDB database, protect against possible failures caused by documented /// and undocumented possibilities of failure. /// Will throw iff index registration failed and index doesn't exist or it exists but with a non-current definition. /// </summary> /// <param name="store"></param> /// <param name="index"></param> internal static void SafelyCreateIndex(IDocumentStore store, AbstractIndexCreationTask index) { try { index.Execute(store); } catch (Exception) // Apparently ArgumentException can be thrown as well as a WebException; not taking any chances { var getIndexOp = new GetIndexOperation(index.IndexName); var existingIndex = store.Maintenance.Send(getIndexOp); if (existingIndex == null || !index.CreateIndexDefinition().Equals(existingIndex)) { throw; } } }
/// <summary> /// Executes the index creation against the specified document database using the specified conventions /// </summary> public virtual void Execute(IDocumentStore documentStore, DocumentConvention documentConvention) { Conventions = documentConvention; var indexDefinition = CreateIndexDefinition(); var requestExecuter = documentStore.GetRequestExecuter(documentStore.DefaultDatabase); JsonOperationContext jsonOperationContext; requestExecuter.ContextPool.AllocateOperationContext(out jsonOperationContext); if (documentConvention.PrettifyGeneratedLinqExpressions) { var getIndexOperation = new GetIndexOperation(jsonOperationContext); var getCommand = getIndexOperation.CreateRequest(IndexName); if (getCommand != null) { requestExecuter.Execute(getCommand, jsonOperationContext); getIndexOperation.SetResult(getCommand.Result); } var serverIndexDefinition = getIndexOperation.IndexDefinitionResult; if (serverIndexDefinition != null) //TODO iftah && CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverIndexDefinition, indexDefinition)) { //AfterExecute(databaseCommands, documentConvention); return; } } var putIndexOperation = new PutIndexOperation(jsonOperationContext); var putCommand = putIndexOperation.CreateRequest(documentConvention, IndexName, indexDefinition); if (putCommand != null) { requestExecuter.Execute(putCommand, jsonOperationContext); putIndexOperation.SetResult(putCommand.Result); } /*if (Priority != null) * databaseCommands.SetIndexPriority(IndexName, Priority.Value); * * AfterExecute(databaseCommands, documentConvention);*/ }
void CreateIndexes(IDocumentStore store) { foreach (var index in indexesToCreate) { try { index.Execute(store); } catch (Exception) // Apparently ArgumentException can be thrown as well as a WebException; not taking any chances { var getIndexOp = new GetIndexOperation(index.IndexName); var existingIndex = store.Maintenance.Send(getIndexOp); if (existingIndex == null || !index.CreateIndexDefinition().Equals(existingIndex)) { throw; } } } }