PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Database/Client/EmbeddedDocumentStore.cs

https://github.com/barryhagan/ravendb
C# | 390 lines | 210 code | 42 blank | 138 comment | 0 complexity | 67ce13ba7e608e6bb5dcdb0fddebbdd8 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Threading.Tasks;
  4. using Raven.Abstractions.Data;
  5. using Raven.Client;
  6. using Raven.Client.Changes;
  7. using Raven.Client.Connection;
  8. using Raven.Client.Connection.Async;
  9. using Raven.Client.Document;
  10. using Raven.Client.Indexes;
  11. using Raven.Client.Listeners;
  12. using Raven.Database.Config;
  13. using Raven.Json.Linq;
  14. using Raven.Server;
  15. namespace Raven.Database.Client
  16. {
  17. public class EmbeddedDocumentStore : IDocumentStore
  18. {
  19. private readonly RavenDbServer server;
  20. public EmbeddedDocumentStore()
  21. {
  22. server = new RavenDbServer();
  23. }
  24. /// <summary>
  25. /// Whatever we should also host an HTTP endpoint for the document database
  26. /// </summary>
  27. public bool UseEmbeddedHttpServer
  28. {
  29. get { return server.UseEmbeddedHttpServer; }
  30. set { server.UseEmbeddedHttpServer = value; }
  31. }
  32. public InMemoryRavenConfiguration Configuration
  33. {
  34. get { return server.Configuration; }
  35. set { server.Configuration = value; }
  36. }
  37. public DocumentDatabase DocumentDatabase
  38. {
  39. get
  40. {
  41. //TODO oren: what should be returned here?
  42. return server.SystemDatabase;
  43. }
  44. }
  45. public string ConnectionStringName
  46. {
  47. get { return server.DocumentStore.ConnectionStringName; }
  48. set { server.DocumentStore.ConnectionStringName = value; }
  49. }
  50. /// <summary>
  51. /// Run RavenDB in an embedded mode, using in memory only storage.
  52. /// This is useful for unit tests, since it is very fast.
  53. /// </summary>
  54. public bool RunInMemory
  55. {
  56. get { return server.Configuration.RunInMemory; }
  57. set { server.Configuration.RunInMemory = value; }
  58. }
  59. /// <summary>
  60. /// Run RavenDB in embedded mode, using the specified directory for data storage
  61. /// </summary>
  62. /// <value>The data directory.</value>
  63. public string DataDirectory
  64. {
  65. get { return Configuration.DataDirectory; }
  66. set { Configuration.DataDirectory = value; }
  67. }
  68. /// <summary>
  69. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  70. /// </summary>
  71. /// <filterpriority>2</filterpriority>
  72. public void Dispose()
  73. {
  74. server.Dispose();
  75. }
  76. /// <summary>
  77. /// Called after dispose is completed
  78. /// </summary>
  79. public event EventHandler AfterDispose
  80. {
  81. add { server.DocumentStore.AfterDispose += value; }
  82. remove { server.DocumentStore.AfterDispose -= value; }
  83. }
  84. /// <summary>
  85. /// Whatever the instance has been disposed
  86. /// </summary>
  87. public bool WasDisposed
  88. {
  89. get { return server.DocumentStore.WasDisposed; }
  90. }
  91. /// <summary>
  92. /// Subscribe to change notifications from the server
  93. /// </summary>
  94. public IDatabaseChanges Changes(string database = null)
  95. {
  96. return server.DocumentStore.Changes(database);
  97. }
  98. /// <summary>
  99. /// Setup the context for aggressive caching.
  100. /// </summary>
  101. /// <param name="cacheDuration">Specify the aggressive cache duration</param>
  102. /// <remarks>
  103. /// Aggressive caching means that we will not check the server to see whatever the response
  104. /// we provide is current or not, but will serve the information directly from the local cache
  105. /// without touching the server.
  106. /// </remarks>
  107. public IDisposable AggressivelyCacheFor(TimeSpan cacheDuration)
  108. {
  109. return server.DocumentStore.AggressivelyCacheFor(cacheDuration);
  110. }
  111. /// <summary>
  112. /// Setup the context for aggressive caching.
  113. /// </summary>
  114. /// <remarks>
  115. /// Aggressive caching means that we will not check the server to see whatever the response
  116. /// we provide is current or not, but will serve the information directly from the local cache
  117. /// without touching the server.
  118. /// </remarks>
  119. public IDisposable AggressivelyCache()
  120. {
  121. return server.DocumentStore.AggressivelyCache();
  122. }
  123. /// <summary>
  124. /// Setup the context for no aggressive caching
  125. /// </summary>
  126. /// <remarks>
  127. /// This is mainly useful for internal use inside RavenDB, when we are executing
  128. /// queries that has been marked with WaitForNonStaleResults, we temporarily disable
  129. /// aggressive caching.
  130. /// </remarks>
  131. public IDisposable DisableAggressiveCaching()
  132. {
  133. return server.DocumentStore.DisableAggressiveCaching();
  134. }
  135. /// <summary>
  136. /// Setup the WebRequest timeout for the session
  137. /// </summary>
  138. /// <param name="timeout">Specify the timeout duration</param>
  139. /// <remarks>
  140. /// Sets the timeout for the JsonRequest. Scoped to the Current Thread.
  141. /// </remarks>
  142. public IDisposable SetRequestsTimeoutFor(TimeSpan timeout)
  143. {
  144. return server.DocumentStore.SetRequestsTimeoutFor(timeout);
  145. }
  146. /// <summary>
  147. /// Gets the shared operations headers.
  148. /// </summary>
  149. /// <value>The shared operations headers.</value>
  150. public NameValueCollection SharedOperationsHeaders
  151. {
  152. get { return server.DocumentStore.SharedOperationsHeaders; }
  153. }
  154. /// <summary>
  155. /// Get the <see cref="HttpJsonRequestFactory" /> for this store
  156. /// </summary>
  157. public HttpJsonRequestFactory JsonRequestFactory
  158. {
  159. get { return server.DocumentStore.JsonRequestFactory; }
  160. }
  161. /// <summary>
  162. /// Gets or sets the identifier for this store.
  163. /// </summary>
  164. /// <value>The identifier.</value>
  165. public string Identifier
  166. {
  167. get { return server.DocumentStore.Identifier ?? (RunInMemory ? "memory #" + GetHashCode() : DataDirectory); }
  168. set { server.DocumentStore.Identifier = value; }
  169. }
  170. /// <summary>
  171. /// Initializes this instance.
  172. /// </summary>
  173. /// <returns></returns>
  174. public IDocumentStore Initialize()
  175. {
  176. server.Initialize();
  177. return this;
  178. }
  179. /// <summary>
  180. /// Gets the async database commands.
  181. /// </summary>
  182. /// <value>The async database commands.</value>
  183. public IAsyncDatabaseCommands AsyncDatabaseCommands
  184. {
  185. get { return server.DocumentStore.AsyncDatabaseCommands; }
  186. }
  187. /// <summary>
  188. /// Opens the async session.
  189. /// </summary>
  190. /// <returns></returns>
  191. public IAsyncDocumentSession OpenAsyncSession()
  192. {
  193. return server.DocumentStore.OpenAsyncSession();
  194. }
  195. /// <summary>
  196. /// Opens the async session.
  197. /// </summary>
  198. /// <returns></returns>
  199. public IAsyncDocumentSession OpenAsyncSession(string database)
  200. {
  201. return server.DocumentStore.OpenAsyncSession(database);
  202. }
  203. /// <summary>
  204. /// Opens the session.
  205. /// </summary>
  206. /// <returns></returns>
  207. public IDocumentSession OpenSession()
  208. {
  209. return server.DocumentStore.OpenSession();
  210. }
  211. /// <summary>
  212. /// Opens the session for a particular database
  213. /// </summary>
  214. public IDocumentSession OpenSession(string database)
  215. {
  216. return server.DocumentStore.OpenSession(database);
  217. }
  218. /// <summary>
  219. /// Opens the session with the specified options.
  220. /// </summary>
  221. public IDocumentSession OpenSession(OpenSessionOptions sessionOptions)
  222. {
  223. return server.DocumentStore.OpenSession(sessionOptions);
  224. }
  225. /// <summary>
  226. /// Gets the database commands.
  227. /// </summary>
  228. /// <value>The database commands.</value>
  229. public IDatabaseCommands DatabaseCommands
  230. {
  231. get { return server.DocumentStore.DatabaseCommands; }
  232. }
  233. /// <summary>
  234. /// Executes the index creation.
  235. /// </summary>
  236. public void ExecuteIndex(AbstractIndexCreationTask indexCreationTask)
  237. {
  238. server.DocumentStore.ExecuteIndex(indexCreationTask);
  239. }
  240. /// <summary>
  241. /// Executes the index creation.
  242. /// </summary>
  243. /// <param name="indexCreationTask"></param>
  244. public Task ExecuteIndexAsync(AbstractIndexCreationTask indexCreationTask)
  245. {
  246. return server.DocumentStore.ExecuteIndexAsync(indexCreationTask);
  247. }
  248. /// <summary>
  249. /// Executes the transformer creation
  250. /// </summary>
  251. public void ExecuteTransformer(AbstractTransformerCreationTask transformerCreationTask)
  252. {
  253. server.DocumentStore.ExecuteTransformer(transformerCreationTask);
  254. }
  255. public Task ExecuteTransformerAsync(AbstractTransformerCreationTask transformerCreationTask)
  256. {
  257. return server.DocumentStore.ExecuteTransformerAsync(transformerCreationTask);
  258. }
  259. /// <summary>
  260. /// Gets the conventions.
  261. /// </summary>
  262. /// <value>The conventions.</value>
  263. public DocumentConvention Conventions
  264. {
  265. get { return server.DocumentStore.Conventions; }
  266. set { server.DocumentStore.Conventions = value; }
  267. }
  268. /// <summary>
  269. /// Gets the URL.
  270. /// </summary>
  271. public string Url
  272. {
  273. get { return server.Url; }
  274. set { server.Url = value; }
  275. }
  276. /// <summary>
  277. /// Gets the etag of the last document written by any session belonging to this
  278. /// document store
  279. /// </summary>
  280. public Etag GetLastWrittenEtag()
  281. {
  282. return server.DocumentStore.GetLastWrittenEtag();
  283. }
  284. public BulkInsertOperation BulkInsert(string database = null, BulkInsertOptions options = null)
  285. {
  286. return server.DocumentStore.BulkInsert(database, options);
  287. }
  288. public DocumentSessionListeners Listeners { get { return server.DocumentStore.Listeners; } }
  289. public void SetListeners(DocumentSessionListeners listeners)
  290. {
  291. server.DocumentStore.SetListeners(listeners);
  292. }
  293. public string DefaultDatabase
  294. {
  295. get { return server.DocumentStore.DefaultDatabase; }
  296. set { server.DocumentStore.DefaultDatabase = value; }
  297. }
  298. public Guid ResourceManagerId
  299. {
  300. get { return server.DocumentStore.ResourceManagerId; }
  301. set { server.DocumentStore.ResourceManagerId = value; }
  302. }
  303. public bool EnlistInDistributedTransactions
  304. {
  305. get { return server.DocumentStore.EnlistInDistributedTransactions; }
  306. set { server.DocumentStore.EnlistInDistributedTransactions = value; }
  307. }
  308. /// <summary>
  309. /// Registers the store listener.
  310. /// </summary>
  311. /// <param name="documentStoreListener">The document store listener.</param>
  312. public IDocumentStore RegisterListener(IDocumentStoreListener documentStoreListener)
  313. {
  314. return server.DocumentStore.RegisterListener(documentStoreListener);
  315. }
  316. /// <summary>
  317. /// Registers the query listener.
  318. /// </summary>
  319. /// <param name="queryListener">The query listener.</param>
  320. public DocumentStoreBase RegisterListener(IDocumentQueryListener queryListener)
  321. {
  322. return server.DocumentStore.RegisterListener(queryListener);
  323. }
  324. /// <summary>
  325. /// Registers the delete listener.
  326. /// </summary>
  327. /// <param name="deleteListener">The delete listener.</param>
  328. public DocumentStoreBase RegisterListener(IDocumentDeleteListener deleteListener)
  329. {
  330. return server.DocumentStore.RegisterListener(deleteListener);
  331. }
  332. /// <summary>
  333. /// Registers the conversion listener.
  334. /// </summary>
  335. public DocumentStoreBase RegisterListener(IDocumentConversionListener documentConversionListener)
  336. {
  337. return server.DocumentStore.RegisterListener(documentConversionListener);
  338. }
  339. /// <summary>
  340. /// Registers the conflict listener.
  341. /// </summary>
  342. /// <param name="conflictListener">The conflict listener.</param>
  343. public DocumentStoreBase RegisterListener(IDocumentConflictListener conflictListener)
  344. {
  345. return server.DocumentStore.RegisterListener(conflictListener);
  346. }
  347. }
  348. }