mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 18:38:27 +03:00
Singleton LiteDatabase (#946)
* update litedb. initialize db as a static singleton instance. * dont need to dispose anymore
This commit is contained in:
parent
1120bff34d
commit
66055f1d7c
2 changed files with 12 additions and 10 deletions
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CsvHelper" Version="15.0.5" />
|
<PackageReference Include="CsvHelper" Version="15.0.5" />
|
||||||
<PackageReference Include="LiteDB" Version="5.0.4" />
|
<PackageReference Include="LiteDB" Version="5.0.8" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
<PackageReference Include="PCLCrypto" Version="2.0.147" />
|
<PackageReference Include="PCLCrypto" Version="2.0.147" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -2,14 +2,16 @@
|
||||||
using LiteDB;
|
using LiteDB;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
public class LiteDbStorageService : IStorageService, IDisposable
|
public class LiteDbStorageService : IStorageService
|
||||||
{
|
{
|
||||||
|
private static LiteDatabase _db;
|
||||||
|
private static readonly object _lock = new object();
|
||||||
|
|
||||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||||
|
@ -17,7 +19,6 @@ namespace Bit.Core.Services
|
||||||
private readonly string _dbPath;
|
private readonly string _dbPath;
|
||||||
private ILiteCollection<JsonItem> _collection;
|
private ILiteCollection<JsonItem> _collection;
|
||||||
private Task _initTask;
|
private Task _initTask;
|
||||||
private LiteDatabase _db;
|
|
||||||
|
|
||||||
public LiteDbStorageService(string dbPath)
|
public LiteDbStorageService(string dbPath)
|
||||||
{
|
{
|
||||||
|
@ -37,8 +38,14 @@ namespace Bit.Core.Services
|
||||||
_initTask = Task.Run(() =>
|
_initTask = Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (_db == null)
|
||||||
{
|
{
|
||||||
_db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
|
_db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
|
||||||
|
}
|
||||||
|
}
|
||||||
_collection = _db.GetCollection<JsonItem>("json_items");
|
_collection = _db.GetCollection<JsonItem>("json_items");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
@ -73,11 +80,6 @@ namespace Bit.Core.Services
|
||||||
_collection.DeleteMany(i => i.Id == key);
|
_collection.DeleteMany(i => i.Id == key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_db?.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
private class JsonItem
|
private class JsonItem
|
||||||
{
|
{
|
||||||
public JsonItem() { }
|
public JsonItem() { }
|
||||||
|
|
Loading…
Reference in a new issue