/
LiteDatabase.cs
386 lines (318 loc) · 12.9 KB
/
LiteDatabase.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using LiteDB.Engine;
using static LiteDB.Constants;
namespace LiteDB
{
/// <summary>
/// The LiteDB database. Used for create a LiteDB instance and use all storage resources. It's the database connection
/// </summary>
public partial class LiteDatabase : ILiteDatabase
{
#region Properties
private readonly ILiteEngine _engine;
private readonly BsonMapper _mapper;
private readonly bool _disposeOnClose;
/// <summary>
/// Get current instance of BsonMapper used in this database instance (can be BsonMapper.Global)
/// </summary>
public BsonMapper Mapper => _mapper;
#endregion
#region Ctor
/// <summary>
/// Starts LiteDB database using a connection string for file system database
/// </summary>
public LiteDatabase(string connectionString, BsonMapper mapper = null)
: this(new ConnectionString(connectionString), mapper)
{
}
/// <summary>
/// Starts LiteDB database using a connection string for file system database
/// </summary>
public LiteDatabase(ConnectionString connectionString, BsonMapper mapper = null)
{
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString));
if (connectionString.Upgrade == true)
{
// try upgrade if need
LiteEngine.Upgrade(connectionString.Filename, connectionString.Password, connectionString.Collation);
}
_engine = connectionString.CreateEngine();
_mapper = mapper ?? BsonMapper.Global;
_disposeOnClose = true;
}
/// <summary>
/// Starts LiteDB database using a generic Stream implementation (mostly MemoryStream).
/// </summary>
/// <param name="stream">DataStream reference </param>
/// <param name="mapper">BsonMapper mapper reference</param>
/// <param name="logStream">LogStream reference </param>
public LiteDatabase(Stream stream, BsonMapper mapper = null, Stream logStream = null)
{
var settings = new EngineSettings
{
DataStream = stream ?? throw new ArgumentNullException(nameof(stream)),
LogStream = logStream
};
_engine = new LiteEngine(settings);
_mapper = mapper ?? BsonMapper.Global;
_disposeOnClose = true;
}
/// <summary>
/// Start LiteDB database using a pre-exiting engine. When LiteDatabase instance dispose engine instance will be disposed too
/// </summary>
public LiteDatabase(ILiteEngine engine, BsonMapper mapper = null, bool disposeOnClose = true)
{
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
_mapper = mapper ?? BsonMapper.Global;
_disposeOnClose = disposeOnClose;
}
#endregion
#region Collections
/// <summary>
/// Get a collection using a entity class as strong typed document. If collection does not exist, create a new one.
/// </summary>
/// <param name="name">Collection name (case insensitive)</param>
/// <param name="autoId">Define autoId data type (when object contains no id field)</param>
public ILiteCollection<T> GetCollection<T>(string name, BsonAutoId autoId = BsonAutoId.ObjectId)
{
return new LiteCollection<T>(name, autoId, _engine, _mapper);
}
/// <summary>
/// Get a collection using a name based on typeof(T).Name (BsonMapper.ResolveCollectionName function)
/// </summary>
public ILiteCollection<T> GetCollection<T>()
{
return this.GetCollection<T>(null);
}
/// <summary>
/// Get a collection using a name based on typeof(T).Name (BsonMapper.ResolveCollectionName function)
/// </summary>
public ILiteCollection<T> GetCollection<T>(BsonAutoId autoId)
{
return this.GetCollection<T>(null, autoId);
}
/// <summary>
/// Get a collection using a generic BsonDocument. If collection does not exist, create a new one.
/// </summary>
/// <param name="name">Collection name (case insensitive)</param>
/// <param name="autoId">Define autoId data type (when document contains no _id field)</param>
public ILiteCollection<BsonDocument> GetCollection(string name, BsonAutoId autoId = BsonAutoId.ObjectId)
{
if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(name));
return new LiteCollection<BsonDocument>(name, autoId, _engine, _mapper);
}
#endregion
#region Transaction
/// <summary>
/// Initialize a new transaction. Transaction are created "per-thread". There is only one single transaction per thread.
/// Return true if transaction was created or false if current thread already in a transaction.
/// </summary>
public bool BeginTrans() => _engine.BeginTrans();
/// <summary>
/// Commit current transaction
/// </summary>
public bool Commit() => _engine.Commit();
/// <summary>
/// Rollback current transaction
/// </summary>
public bool Rollback() => _engine.Rollback();
#endregion
#region FileStorage
private ILiteStorage<string> _fs = null;
/// <summary>
/// Returns a special collection for storage files/stream inside datafile. Use _files and _chunks collection names. FileId is implemented as string. Use "GetStorage" for custom options
/// </summary>
public ILiteStorage<string> FileStorage
{
get { return _fs ?? (_fs = this.GetStorage<string>()); }
}
/// <summary>
/// Get new instance of Storage using custom FileId type, custom "_files" collection name and custom "_chunks" collection. LiteDB support multiples file storages (using different files/chunks collection names)
/// </summary>
public ILiteStorage<TFileId> GetStorage<TFileId>(string filesCollection = "_files", string chunksCollection = "_chunks")
{
return new LiteStorage<TFileId>(this, filesCollection, chunksCollection);
}
#endregion
#region Shortcut
/// <summary>
/// Get all collections name inside this database.
/// </summary>
public IEnumerable<string> GetCollectionNames()
{
// use $cols system collection with type = user only
var cols = this.GetCollection("$cols")
.Query()
.Where("type = 'user'")
.ToDocuments()
.Select(x => x["name"].AsString)
.ToArray();
return cols;
}
/// <summary>
/// Checks if a collection exists on database. Collection name is case insensitive
/// </summary>
public bool CollectionExists(string name)
{
if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(name));
return this.GetCollectionNames().Contains(name, StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Drop a collection and all data + indexes
/// </summary>
public bool DropCollection(string name)
{
if (name.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(name));
return _engine.DropCollection(name);
}
/// <summary>
/// Rename a collection. Returns false if oldName does not exists or newName already exists
/// </summary>
public bool RenameCollection(string oldName, string newName)
{
if (oldName.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(oldName));
if (newName.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(newName));
return _engine.RenameCollection(oldName, newName);
}
#endregion
#region Execute SQL
/// <summary>
/// Execute SQL commands and return as data reader.
/// </summary>
public IBsonDataReader Execute(TextReader commandReader, BsonDocument parameters = null)
{
if (commandReader == null) throw new ArgumentNullException(nameof(commandReader));
var tokenizer = new Tokenizer(commandReader);
var sql = new SqlParser(_engine, tokenizer, parameters);
var reader = sql.Execute();
return reader;
}
/// <summary>
/// Execute SQL commands and return as data reader
/// </summary>
public IBsonDataReader Execute(string command, BsonDocument parameters = null)
{
if (command == null) throw new ArgumentNullException(nameof(command));
var tokenizer = new Tokenizer(command);
var sql = new SqlParser(_engine, tokenizer, parameters);
var reader = sql.Execute();
return reader;
}
/// <summary>
/// Execute SQL commands and return as data reader
/// </summary>
public IBsonDataReader Execute(string command, params BsonValue[] args)
{
var p = new BsonDocument();
var index = 0;
foreach (var arg in args)
{
p[index.ToString()] = arg;
index++;
}
return this.Execute(command, p);
}
#endregion
#region Checkpoint/Rebuild
/// <summary>
/// Do database checkpoint. Copy all commited transaction from log file into datafile.
/// </summary>
public void Checkpoint()
{
_engine.Checkpoint();
}
/// <summary>
/// Rebuild all database to remove unused pages - reduce data file
/// </summary>
public long Rebuild(RebuildOptions options = null)
{
return _engine.Rebuild(options);
}
#endregion
#region Pragmas
/// <summary>
/// Get value from internal engine variables
/// </summary>
public BsonValue Pragma(string name)
{
return _engine.Pragma(name);
}
/// <summary>
/// Set new value to internal engine variables
/// </summary>
public BsonValue Pragma(string name, BsonValue value)
{
return _engine.Pragma(name, value);
}
/// <summary>
/// Get/Set database user version - use this version number to control database change model
/// </summary>
public int UserVersion
{
get => _engine.Pragma(Pragmas.USER_VERSION);
set => _engine.Pragma(Pragmas.USER_VERSION, value);
}
/// <summary>
/// Get/Set database timeout - this timeout is used to wait for unlock using transactions
/// </summary>
public TimeSpan Timeout
{
get => TimeSpan.FromSeconds(_engine.Pragma(Pragmas.TIMEOUT).AsInt32);
set => _engine.Pragma(Pragmas.TIMEOUT, (int)value.TotalSeconds);
}
/// <summary>
/// Get/Set if database will deserialize dates in UTC timezone or Local timezone (default: Local)
/// </summary>
public bool UtcDate
{
get => _engine.Pragma(Pragmas.UTC_DATE);
set => _engine.Pragma(Pragmas.UTC_DATE, value);
}
/// <summary>
/// Get/Set database limit size (in bytes). New value must be equals or larger than current database size
/// </summary>
public long LimitSize
{
get => _engine.Pragma(Pragmas.LIMIT_SIZE);
set => _engine.Pragma(Pragmas.LIMIT_SIZE, value);
}
/// <summary>
/// Get/Set in how many pages (8 Kb each page) log file will auto checkpoint (copy from log file to data file). Use 0 to manual-only checkpoint (and no checkpoint on dispose)
/// Default: 1000 pages
/// </summary>
public int CheckpointSize
{
get => _engine.Pragma(Pragmas.CHECKPOINT);
set => _engine.Pragma(Pragmas.CHECKPOINT, value);
}
/// <summary>
/// Get database collection (this options can be changed only in rebuild proces)
/// </summary>
public Collation Collation
{
get => new Collation(_engine.Pragma(Pragmas.COLLATION).AsString);
}
#endregion
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
~LiteDatabase()
{
this.Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing && _disposeOnClose)
{
_engine.Dispose();
}
}
}
}