aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore.LiteDB/LiteDBDataStoreManager.cs
blob: 25abd91abcbd63c45a871fb130288bcfb2637d37 (plain)
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
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.DataStore.Lite
{
    public class LiteDBDataStoreManager : IDataStoreManager
    {
        private bool _disposed;
        private LiteDatabase _database;

        public String DatabasePath { get; private set; }

        public LiteDBDataStoreManager(String databasePath)
        {
            DatabasePath = databasePath;
            Directory.CreateDirectory(Path.GetDirectoryName(DatabasePath));
            _database = new LiteDatabase($"Filename={DatabasePath}");
            _database.Pragma("TIMEOUT", 10); //Read Timeout
            _database.Pragma("UTC_DATE", true); //Keep time as UTC when getting data
            _database.Commit();
        }

        public IDataStoreCollection GetCollection(string name)
        {
            return new LiteDBDataStoreCollection(_database.GetCollection<IDataStoreItem>(name));
        }

        public List<String> GetCollectionNames()
        {
            return _database.GetCollectionNames().ToList();
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;
                _database.Dispose();
            }
        }
    }
}