Skip to content

Instantly share code, notes, and snippets.

@benerdin
Last active December 14, 2015 02:49
Show Gist options
  • Select an option

  • Save benerdin/5016633 to your computer and use it in GitHub Desktop.

Select an option

Save benerdin/5016633 to your computer and use it in GitHub Desktop.
Creates a Raven DocumentStore and creates static indexes on the primary and all secondary servers (static indexes are not replicated by design).
/// <summary>
/// Gets the document store.
/// </summary>
/// <returns>The document store.</returns>
private static IDocumentStore GetDocumentStore()
{
// Create the DocumentStore (expensive operation).
IDocumentStore documentStore = new DocumentStore
{
ConnectionStringName = "RavenDB",
Credentials = System.Net.CredentialCache.DefaultNetworkCredentials // For "trusted connections": see comments at http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore
};
// Read from and write to all servers.
documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers
| FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries;
// Initialize the store (must be done before creating indexes)
documentStore = documentStore.Initialize();
// Create static indexes
CreateRavenStaticIndexes(documentStore);
// Return document store
return documentStore;
}
/// <summary>
/// Creates static indexes on all Raven servers.
/// </summary>
/// <remarks>
/// Static indexes are not replicated in Raven.
/// </remarks>
private static void CreateRavenStaticIndexes(IDocumentStore documentStore)
{
try
{
// Create static indexes on primary server
IndexCreation.CreateIndexes(typeof(AllCommentsIndex).Assembly, documentStore);
// Create static indexes on secondary servers
CreateRavenStaticIndexesOnSecondaryServers(documentStore);
}
catch (Exception ex)
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.FatalException(ex.ToString(), ex);
}
}
/// <summary>
/// Creates static indexes programmatically on all secondary Raven servers.
/// </summary>
private static void CreateRavenStaticIndexesOnSecondaryServers(IDocumentStore documentStore)
{
var connections = GetReplicationConnectionsFromServer(documentStore);
Parallel.ForEach(connections, connection =>
{
try
{
// Initialize document store for secondary server
var docStore = new DocumentStore
{
Url = connection.Url,
DefaultDatabase = connection.DefaultDatabase,
ApiKey = connection.ApiKey,
Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
}.Initialize();
// Create static indexes
IndexCreation.CreateIndexes(typeof(AllCommentsIndex).Assembly, docStore);
}
catch (System.Exception ex)
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.FatalException(ex.ToString(), ex);
}
}
);
}
/// <summary>
/// Returns a collection of connections for all secondary Raven servers.
/// </summary>
private static IEnumerable<RavenConnection> GetReplicationConnectionsFromServer(IDocumentStore documentStore)
{
try
{
var connections = new List<RavenConnection>();
var rawResult = documentStore.DatabaseCommands.Get("Raven/Replication/Destinations");
connections.AddRange(rawResult.DataAsJson.SelectToken("Destinations").Values()
.Select(i => new RavenConnection
{
Url = i.Value<string>("Url"),
ApiKey = i.Value<string>("ApiKey"),
DefaultDatabase = i.Value<string>("Database")
})
.ToList());
return connections;
}
catch (Exception ex)
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.FatalException(ex.ToString(), ex);
return new List<RavenConnection>();
}
}
/// <summary>
/// Contains Raven datastore connection values.
/// </summary>
class RavenConnection
{
public string Url { get; set; }
public string DefaultDatabase { get; set; }
public string ApiKey { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment