-
Notifications
You must be signed in to change notification settings - Fork 23
/
RavenContext.cs
463 lines (377 loc) · 14.3 KB
/
RavenContext.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Raven.Abstractions.Data;
using Raven.Client;
using Raven.Client.Changes;
using Raven.Client.Connection;
using Raven.Client.Connection.Async;
using Raven.Client.Connection.Profiling;
using System.Reflection;
using System.Net;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Client.Shard;
using RavenLinqpadDriver.Common;
namespace RavenLinqpadDriver
{
public class RavenContext : IDocumentSession, IDocumentStore
{
private readonly IDocumentStore _docStore;
private readonly Lazy<IDocumentSession> _lazySession;
internal TextWriter LogWriter { get; set; }
public RavenContext(RavenConnectionDialogViewModel connInfo)
{
if (connInfo == null) throw new ArgumentNullException("connInfo");
_docStore = CreateDocStore(connInfo).Initialize();
_lazySession = new Lazy<IDocumentSession>(_docStore.OpenSession);
SetupLogWriting();
}
private void SetupLogWriting()
{
// sharded doc stores don't have a jsonrequestfactory,
// so if sharding, get shard doc stores
var shardedDocStore = _docStore as ShardedDocumentStore;
if (shardedDocStore != null)
{
var docStores = from ds in shardedDocStore.ShardStrategy.Shards.Values
where !(ds is ShardedDocumentStore)
select ds;
foreach (var docStore in docStores)
docStore.JsonRequestFactory.LogRequest += LogRequest;
}
else
{
_docStore.JsonRequestFactory.LogRequest += LogRequest;
}
}
void LogRequest(object sender, RequestResultArgs e)
{
if (LogWriter == null) return;
var entry = new StringBuilder().AppendFormat(@"
{0} - {1}
Url: {2}
Duration: {3} milliseconds
Method: {4}
Posted Data: {5}
Http Result: {6}
Result Data: {7}
Total Size: {8:n0}",
e.At,
e.Status,
e.Url,
e.DurationMilliseconds,
e.Method,
e.PostedData,
e.HttpResult,
e.Result,
e.TotalSize);
foreach (var item in e.AdditionalInformation)
entry.AppendFormat("{0}: {1}", item.Key, item.Value);
entry.AppendLine();
LogWriter.WriteLine(entry.ToString());
}
private static IDocumentStore CreateDocStore(RavenConnectionDialogViewModel conn)
{
if (conn == null)
throw new ArgumentNullException("conn", "conn is null.");
var assemblies = conn.AssemblyPaths.Select(Path.GetFileNameWithoutExtension).Select(Assembly.Load);
var docStoreCreatorType = (from a in assemblies
from t in a.TypesImplementing<ICreateDocumentStore>()
let hasDefaultCtor = t.GetConstructor(Type.EmptyTypes) != null
where !t.IsAbstract && hasDefaultCtor
select t).FirstOrDefault();
if (docStoreCreatorType == null)
return conn.CreateDocStore();
var docStoreCreator = (ICreateDocumentStore)Activator.CreateInstance(docStoreCreatorType);
var connectionInfo = new ConnectionInfo
{
Url = conn.Url,
DefaultDatabase = conn.DefaultDatabase,
Credentials = new NetworkCredential
{
UserName = conn.Username,
Password = conn.Password
},
ResourceManagerId = conn.ResourceManagerId,
ApiKey = conn.ApiKey
};
return docStoreCreator.CreateDocumentStore(connectionInfo);
}
public void Dispose()
{
if (_lazySession.IsValueCreated)
_lazySession.Value.Dispose();
if (_docStore != null && !_docStore.WasDisposed)
_docStore.Dispose();
}
public bool WasDisposed
{
get { return _docStore.WasDisposed; }
}
public event EventHandler AfterDispose;
public IDatabaseChanges Changes(string database = null)
{
return _docStore.Changes(database);
}
public IDisposable AggressivelyCacheFor(TimeSpan cacheDuration)
{
return _docStore.AggressivelyCacheFor(cacheDuration);
}
public IDisposable AggressivelyCache()
{
return _docStore.AggressivelyCache();
}
public IDisposable DisableAggressiveCaching()
{
return _docStore.DisableAggressiveCaching();
}
public IDisposable SetRequestsTimeoutFor(TimeSpan timeout)
{
return _docStore.SetRequestsTimeoutFor(timeout);
}
public IDocumentStore Initialize()
{
return _docStore.Initialize();
}
public IAsyncDocumentSession OpenAsyncSession()
{
return _docStore.OpenAsyncSession();
}
public IAsyncDocumentSession OpenAsyncSession(string database)
{
return _docStore.OpenAsyncSession(database);
}
public IAsyncDocumentSession OpenAsyncSession(OpenSessionOptions sessionOptions)
{
return _docStore.OpenAsyncSession(sessionOptions);
}
public IDocumentSession OpenSession()
{
return _docStore.OpenSession();
}
public IDocumentSession OpenSession(string database)
{
return _docStore.OpenSession(database);
}
public IDocumentSession OpenSession(OpenSessionOptions sessionOptions)
{
return _docStore.OpenSession(sessionOptions);
}
public void ExecuteIndex(AbstractIndexCreationTask indexCreationTask)
{
_docStore.ExecuteIndex(indexCreationTask);
}
public Task ExecuteIndexAsync(AbstractIndexCreationTask indexCreationTask)
{
return _docStore.ExecuteIndexAsync(indexCreationTask);
}
public void ExecuteIndexes(List<AbstractIndexCreationTask> indexCreationTasks)
{
_docStore.ExecuteIndexes(indexCreationTasks);
}
public Task ExecuteIndexesAsync(List<AbstractIndexCreationTask> indexCreationTasks)
{
return _docStore.ExecuteIndexesAsync(indexCreationTasks);
}
public void SideBySideExecuteIndex(AbstractIndexCreationTask indexCreationTask, Etag minimumEtagBeforeReplace = null, DateTime? replaceTimeUtc = null)
{
_docStore.SideBySideExecuteIndex(indexCreationTask, minimumEtagBeforeReplace, replaceTimeUtc);
}
public Task SideBySideExecuteIndexAsync(AbstractIndexCreationTask indexCreationTask, Etag minimumEtagBeforeReplace = null, DateTime? replaceTimeUtc = null)
{
return _docStore.SideBySideExecuteIndexAsync(indexCreationTask, minimumEtagBeforeReplace, replaceTimeUtc);
}
public void SideBySideExecuteIndexes(List<AbstractIndexCreationTask> indexCreationTasks, Etag minimumEtagBeforeReplace = null, DateTime? replaceTimeUtc = null)
{
_docStore.SideBySideExecuteIndexes(indexCreationTasks, minimumEtagBeforeReplace, replaceTimeUtc);
}
public Task SideBySideExecuteIndexesAsync(List<AbstractIndexCreationTask> indexCreationTasks, Etag minimumEtagBeforeReplace = null, DateTime? replaceTimeUtc = null)
{
return _docStore.SideBySideExecuteIndexesAsync(indexCreationTasks, minimumEtagBeforeReplace, replaceTimeUtc);
}
public void ExecuteTransformer(AbstractTransformerCreationTask transformerCreationTask)
{
_docStore.ExecuteTransformer(transformerCreationTask);
}
public Task ExecuteTransformerAsync(AbstractTransformerCreationTask transformerCreationTask)
{
return _docStore.ExecuteTransformerAsync(transformerCreationTask);
}
public Etag GetLastWrittenEtag()
{
return _docStore.GetLastWrittenEtag();
}
public BulkInsertOperation BulkInsert(string database = null, BulkInsertOptions options = null)
{
return _docStore.BulkInsert(database, options);
}
public void SetListeners(DocumentSessionListeners listeners)
{
_docStore.SetListeners(listeners);
}
public void InitializeProfiling()
{
_docStore.InitializeProfiling();
}
public ProfilingInformation GetProfilingInformationFor(Guid id)
{
return _docStore.GetProfilingInformationFor(id);
}
public NameValueCollection SharedOperationsHeaders
{
get { return _docStore.SharedOperationsHeaders; }
}
public HttpJsonRequestFactory JsonRequestFactory
{
get { return _docStore.JsonRequestFactory; }
}
public bool HasJsonRequestFactory
{
get { return _docStore.HasJsonRequestFactory; }
}
public string Identifier
{
get { return _docStore.Identifier; }
set { _docStore.Identifier = value; }
}
public IAsyncDatabaseCommands AsyncDatabaseCommands
{
get { return _docStore.AsyncDatabaseCommands; }
}
public IDatabaseCommands DatabaseCommands
{
get { return _docStore.DatabaseCommands; }
}
public DocumentConvention Conventions
{
get { return _docStore.Conventions; }
}
public string Url
{
get { return _docStore.Url; }
}
public IAsyncReliableSubscriptions AsyncSubscriptions
{
get { return _docStore.AsyncSubscriptions; }
}
public IReliableSubscriptions Subscriptions
{
get { return _docStore.Subscriptions; }
}
public DocumentSessionListeners Listeners
{
get { return _docStore.Listeners; }
}
public void Delete<T>(T entity)
{
_lazySession.Value.Delete(entity);
}
public void Delete<T>(ValueType id)
{
_lazySession.Value.Delete<T>(id);
}
public void Delete(string id)
{
_lazySession.Value.Delete(id);
}
public ILoaderWithInclude<object> Include(string path)
{
return _lazySession.Value.Include(path);
}
public ILoaderWithInclude<T> Include<T>(Expression<Func<T, object>> path)
{
return _lazySession.Value.Include(path);
}
public ILoaderWithInclude<T> Include<T, TInclude>(Expression<Func<T, object>> path)
{
return _lazySession.Value.Include<T, TInclude>(path);
}
public T Load<T>(string id)
{
return _lazySession.Value.Load<T>(id);
}
public T[] Load<T>(IEnumerable<string> ids)
{
return _lazySession.Value.Load<T>(ids);
}
public T Load<T>(ValueType id)
{
return _lazySession.Value.Load<T>(id);
}
public T[] Load<T>(params ValueType[] ids)
{
return _lazySession.Value.Load<T>(ids);
}
public T[] Load<T>(IEnumerable<ValueType> ids)
{
return _lazySession.Value.Load<T>(ids);
}
public TResult Load<TTransformer, TResult>(string id, Action<ILoadConfiguration> configure = null) where TTransformer : AbstractTransformerCreationTask, new()
{
return _lazySession.Value.Load<TTransformer, TResult>(id, configure);
}
public TResult[] Load<TTransformer, TResult>(IEnumerable<string> ids, Action<ILoadConfiguration> configure = null) where TTransformer : AbstractTransformerCreationTask, new()
{
return _lazySession.Value.Load<TTransformer, TResult>(ids, configure);
}
public TResult Load<TResult>(string id, string transformer, Action<ILoadConfiguration> configure)
{
return _lazySession.Value.Load<TResult>(id, transformer, configure);
}
public TResult[] Load<TResult>(IEnumerable<string> ids, string transformer, Action<ILoadConfiguration> configure = null)
{
return _lazySession.Value.Load<TResult>(ids, transformer, configure);
}
public TResult Load<TResult>(string id, Type transformerType, Action<ILoadConfiguration> configure = null)
{
return _lazySession.Value.Load<TResult>(id, transformerType, configure);
}
public TResult[] Load<TResult>(IEnumerable<string> ids, Type transformerType, Action<ILoadConfiguration> configure = null)
{
return _lazySession.Value.Load<TResult>(ids, transformerType, configure);
}
public IRavenQueryable<T> Query<T>(string indexName, bool isMapReduce = false)
{
return _lazySession.Value.Query<T>(indexName, isMapReduce);
}
public IRavenQueryable<T> Query<T>()
{
return _lazySession.Value.Query<T>();
}
public IRavenQueryable<T> Query<T, TIndexCreator>() where TIndexCreator : AbstractIndexCreationTask, new()
{
return _lazySession.Value.Query<T, TIndexCreator>();
}
public void SaveChanges()
{
_lazySession.Value.SaveChanges();
}
public void Store(object entity, Etag etag)
{
_lazySession.Value.Store(entity, etag);
}
public void Store(object entity, Etag etag, string id)
{
_lazySession.Value.Store(entity, etag, id);
}
public void Store(object entity)
{
_lazySession.Value.Store(entity);
}
public void Store(object entity, string id)
{
_lazySession.Value.Store(entity, id);
}
public ISyncAdvancedSessionOperation Advanced
{
get { return _lazySession.Value.Advanced; }
}
}
}