Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy-less committed Oct 4, 2024
1 parent 391cc93 commit b2037bc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 143 deletions.
25 changes: 25 additions & 0 deletions LiteDB.Tests/Issues/Issue2534_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2534_Tests {
[Fact]
public void Test() {
using LiteDatabase database = new(new ConnectionString()
{
Filename = "Demo.db",
Connection = ConnectionType.Shared,
});
ILiteCollection<BsonDocument> accounts = database.GetCollection("Accounts");
if (accounts.Count() < 3)
{
accounts.Insert(new BsonDocument());
accounts.Insert(new BsonDocument());
accounts.Insert(new BsonDocument());
}
foreach (BsonDocument document in accounts.FindAll())
{
accounts.Update(document);
}
}
}
187 changes: 44 additions & 143 deletions LiteDB/Client/Shared/SharedEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public SharedEngine(EngineSettings settings)
/// <summary>
/// Open database in safe mode
/// </summary>
private void OpenDatabase()
/// <returns>true if successfully opened; false if already open</returns>
private bool OpenDatabase()
{
try
{
Expand All @@ -61,13 +62,18 @@ private void OpenDatabase()
try
{
_engine = new LiteEngine(_settings);
return true;
}
catch
{
_mutex.ReleaseMutex();
throw;
}
}
else
{
return false;
}
}

/// <summary>
Expand Down Expand Up @@ -142,39 +148,27 @@ public bool Rollback()

public IBsonDataReader Query(string collection, Query query)
{
this.OpenDatabase();
bool opened = this.OpenDatabase();

var reader = _engine.Query(collection, query);

return new SharedDataReader(reader, () => this.CloseDatabase());
return new SharedDataReader(reader, () =>
{
if (opened)
{
this.CloseDatabase();
}
});
}

public BsonValue Pragma(string name)
{
this.OpenDatabase();

try
{
return _engine.Pragma(name);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Pragma(name));
}

public bool Pragma(string name, BsonValue value)
{
this.OpenDatabase();

try
{
return _engine.Pragma(name, value);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Pragma(name, value));
}

#endregion
Expand All @@ -183,170 +177,62 @@ public bool Pragma(string name, BsonValue value)

public int Checkpoint()
{
this.OpenDatabase();

try
{
return _engine.Checkpoint();
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Checkpoint());
}

public long Rebuild(RebuildOptions options)
{
this.OpenDatabase();

try
{
return _engine.Rebuild(options);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Rebuild(options));
}

public int Insert(string collection, IEnumerable<BsonDocument> docs, BsonAutoId autoId)
{
this.OpenDatabase();

try
{
return _engine.Insert(collection, docs, autoId);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Insert(collection, docs, autoId));
}

public int Update(string collection, IEnumerable<BsonDocument> docs)
{
this.OpenDatabase();

try
{
return _engine.Update(collection, docs);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Update(collection, docs));
}

public int UpdateMany(string collection, BsonExpression extend, BsonExpression predicate)
{
this.OpenDatabase();

try
{
return _engine.UpdateMany(collection, extend, predicate);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.UpdateMany(collection, extend, predicate));
}

public int Upsert(string collection, IEnumerable<BsonDocument> docs, BsonAutoId autoId)
{
this.OpenDatabase();

try
{
return _engine.Upsert(collection, docs, autoId);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Upsert(collection, docs, autoId));
}

public int Delete(string collection, IEnumerable<BsonValue> ids)
{
this.OpenDatabase();

try
{
return _engine.Delete(collection, ids);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.Delete(collection, ids));
}

public int DeleteMany(string collection, BsonExpression predicate)
{
this.OpenDatabase();

try
{
return _engine.DeleteMany(collection, predicate);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.DeleteMany(collection, predicate));
}

public bool DropCollection(string name)
{
this.OpenDatabase();

try
{
return _engine.DropCollection(name);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.DropCollection(name));
}

public bool RenameCollection(string name, string newName)
{
this.OpenDatabase();

try
{
return _engine.RenameCollection(name, newName);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.RenameCollection(name, newName));
}

public bool DropIndex(string collection, string name)
{
this.OpenDatabase();

try
{
return _engine.DropIndex(collection, name);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.DropIndex(collection, name));
}

public bool EnsureIndex(string collection, string name, BsonExpression expression, bool unique)
{
this.OpenDatabase();

try
{
return _engine.EnsureIndex(collection, name, expression, unique);
}
finally
{
this.CloseDatabase();
}
return QueryDatabase(() => _engine.EnsureIndex(collection, name, expression, unique));
}

#endregion
Expand Down Expand Up @@ -374,5 +260,20 @@ protected virtual void Dispose(bool disposing)
}
}
}

private T QueryDatabase<T>(Func<T> Query) {
bool opened = this.OpenDatabase();
try
{
return Query();
}
finally
{
if (opened)
{
this.CloseDatabase();
}
}
}
}
}

0 comments on commit b2037bc

Please sign in to comment.