/
EmbeddableDocumentStore.cs
176 lines (152 loc) · 5.03 KB
/
EmbeddableDocumentStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//-----------------------------------------------------------------------
// <copyright file="EmbeddableDocumentStore.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using Raven.Abstractions.Data;
using Raven.Client.Document;
using Raven.Database;
using Raven.Database.Config;
using Raven.Database.Server;
namespace Raven.Client.Embedded
{
/// <summary>
/// Manages access to RavenDB and open sessions to work with RavenDB.
/// Also supports hosting RavenDB in an embedded mode
/// </summary>
public class EmbeddableDocumentStore : DocumentStore
{
private RavenConfiguration configuration;
private RavenDbHttpServer httpServer;
private bool wasDisposed;
/// <summary>
/// Raised after this instance has been disposed
/// </summary>
public event Action Disposed = delegate { };
/// <summary>
/// Gets or sets the identifier for this store.
/// </summary>
/// <value>The identifier.</value>
public override string Identifier
{
get { return base.Identifier ?? (RunInMemory ? "memory #" + GetHashCode() : DataDirectory); }
set { base.Identifier = value; }
}
///<summary>
/// Get or set the configuration instance for embedded RavenDB
///</summary>
public RavenConfiguration Configuration
{
get { return configuration ?? (configuration = new RavenConfiguration()); }
}
/// <summary>
/// Run RavenDB in an embedded mode, using in memory only storage.
/// This is useful for unit tests, since it is very fast.
/// </summary>
public bool RunInMemory
{
get { return Configuration.RunInMemory; }
set
{
Configuration.RunInMemory = value;
}
}
/// <summary>
/// Run RavenDB in embedded mode, using the specified directory for data storage
/// </summary>
/// <value>The data directory.</value>
public string DataDirectory
{
get { return Configuration.DataDirectory; }
set { Configuration.DataDirectory = value; }
}
///<summary>
/// Access the embedded instance of RavenDB
///</summary>
public DocumentDatabase DocumentDatabase { get; private set; }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public override void Dispose()
{
if (wasDisposed)
return;
wasDisposed = true;
base.Dispose();
if (DocumentDatabase != null)
DocumentDatabase.Dispose();
if (httpServer != null)
httpServer.Dispose();
var onDisposed = Disposed;
if (onDisposed != null)
onDisposed();
}
/// <summary>
/// Create the connection string parser
/// </summary>
protected override RavenConnectionStringOptions GetConnectionStringOptions()
{
var parser = ConnectionStringParser<EmbeddedRavenConnectionStringOptions>.FromConnectionStringName(ConnectionStringName);
parser.Parse();
return parser.ConnectionStringOptions;
}
/// <summary>
/// Copy the relevant connection string settings
/// </summary>
protected override void SetConnectionStringSettings(RavenConnectionStringOptions options)
{
var embeddedRavenConnectionStringOptions = options as EmbeddedRavenConnectionStringOptions;
if (embeddedRavenConnectionStringOptions == null)
return;
DataDirectory = embeddedRavenConnectionStringOptions.DataDirectory;
RunInMemory = embeddedRavenConnectionStringOptions.RunInMemory;
}
/// <summary>
/// Initialize the document store access method to RavenDB
/// </summary>
protected override void InitializeInternal()
{
if (configuration != null && Url == null)
{
if(configuration.RunInMemory || configuration.RunInUnreliableYetFastModeThatIsNotSuitableForProduction)
{
ResourceManagerId = Guid.NewGuid(); // avoid conflicts
}
DocumentDatabase = new DocumentDatabase(configuration);
DocumentDatabase.SpinBackgroundWorkers();
if (UseEmbeddedHttpServer)
{
httpServer = new RavenDbHttpServer(configuration, DocumentDatabase);
httpServer.Start();
}
databaseCommandsGenerator = () => new EmbeddedDatabaseCommands(DocumentDatabase, Conventions, currentSessionId);
}
else
{
base.InitializeInternal();
}
}
/// <summary>
/// validate the configuration for the document store
/// </summary>
protected override void AssertValidConfiguration()
{
if (RunInMemory)
return;
if(string.IsNullOrEmpty(DataDirectory)) // if we don't have a data dir...
base.AssertValidConfiguration(); // we need to check the configuration for url
}
/// <summary>
/// Expose the internal http server, if used
/// </summary>
public RavenDbHttpServer HttpServer
{
get { return httpServer; }
}
///<summary>
/// Whatever we should also host an HTTP endpoint for the document database
///</summary>
public bool UseEmbeddedHttpServer { get; set; }
}
}