Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom TypeDescriptor field #1982 #1983

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteDB/Client/Mapper/BsonMapper.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public object Deserialize(Type type, BsonValue value)
var doc = value.AsDocument;

// test if value is object and has _type
if (doc.TryGetValue("_type", out var typeField) && typeField.IsString)
if (doc.TryGetValue(BsonMapper.Global.TypeDescriptor, out var typeField) && typeField.IsString)
{
type = _typeNameBinder.GetType(typeField.AsString);

Expand Down
3 changes: 2 additions & 1 deletion LiteDB/Client/Mapper/BsonMapper.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private BsonDocument SerializeObject(Type type, object obj, int depth)
// adding _type only where property Type is not same as object instance type
if (type != t)
{
doc["_type"] = new BsonValue(_typeNameBinder.GetName(t));
doc[BsonMapper.Global.TypeDescriptor] = new BsonValue(_typeNameBinder.GetName(t));
}

foreach (var member in entity.Members.Where(x => x.Getter != null))
Expand All @@ -194,6 +194,7 @@ private BsonDocument SerializeObject(Type type, object obj, int depth)
var value = member.Getter(obj);

if (value == null && this.SerializeNullValues == false && member.FieldName != "_id") continue;
// Console.WriteLine(member.FieldName + " " + member.DataType.Name);

// if member has a custom serialization, use it
if (member.Serialize != null)
Expand Down
11 changes: 7 additions & 4 deletions LiteDB/Client/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public partial class BsonMapper
/// </summary>
public int MaxDepth { get; set; }

public string TypeDescriptor { get; set; }

/// <summary>
/// A custom callback to change MemberInfo behavior when converting to MemberMapper.
/// Use mapper.ResolveMember(Type entity, MemberInfo property, MemberMapper documentMappedField)
Expand All @@ -119,6 +121,7 @@ public BsonMapper(Func<Type, object> customTypeInstantiator = null, ITypeNameBin
this.ResolveCollectionName = (t) => Reflection.IsEnumerable(t) ? Reflection.GetListItemType(t).Name : t.Name;
this.IncludeFields = false;
this.MaxDepth = 20;
this.TypeDescriptor = "_type";

_typeInstantiator = customTypeInstantiator ?? ((Type t) => null);
_typeNameBinder = typeNameBinder ?? DefaultTypeNameBinder.Instance;
Expand Down Expand Up @@ -524,7 +527,7 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
doc["_id"] = idRef;
if (doc.ContainsKey("$type"))
{
doc["_type"] = bson["$type"];
doc[BsonMapper.Global.TypeDescriptor] = bson["$type"];
}

return m.Deserialize(entity.ForType, doc);
Expand All @@ -534,7 +537,7 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
{
return m.Deserialize(entity.ForType,
doc.ContainsKey("$type") ?
new BsonDocument { ["_id"] = idRef, ["_type"] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef, [BsonMapper.Global.TypeDescriptor] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef }); // if has $id, deserialize object using only _id object
}

Expand Down Expand Up @@ -609,7 +612,7 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT
item["_id"] = idRef;
if (item.AsDocument.ContainsKey("$type"))
{
item["_type"] = item["$type"];
item[BsonMapper.Global.TypeDescriptor] = item["$type"];
}

result.Add(item);
Expand All @@ -620,7 +623,7 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT

if (item.AsDocument.ContainsKey("$type"))
{
bsonDocument["_type"] = item["$type"];
bsonDocument[BsonMapper.Global.TypeDescriptor] = item["$type"];
}

result.Add(bsonDocument);
Expand Down
30 changes: 13 additions & 17 deletions LiteDB/Client/Shared/SharedEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace LiteDB
public class SharedEngine : ILiteEngine
{
private readonly EngineSettings _settings;
private readonly Mutex _mutex;
private readonly Semaphore _semaphore;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not part of the issue. Revert this please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

private LiteEngine _engine;
private int _stack = 0;

Expand All @@ -28,20 +28,20 @@ public SharedEngine(EngineSettings settings)
try
{
#if NETFRAMEWORK
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);
var allowEveryoneRule = new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
SemaphoreRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
var securitySettings = new SemaphoreSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

_mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings);
_semaphore = new Semaphore(1, 1, "Global\\" + name + ".Semaphore", out _, securitySettings);
#else
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
_semaphore = new Semaphore(1, 1, "Global\\" + name + ".Semaphore");
#endif
}
catch (NotSupportedException ex)
{
throw new PlatformNotSupportedException("Shared mode is not supported in platforms that do not implement named mutex.", ex);
throw new PlatformNotSupportedException("Shared mode is not supported in platforms that do not implement named semaphore.", ex);
}
}

Expand All @@ -50,25 +50,21 @@ public SharedEngine(EngineSettings settings)
/// </summary>
private void OpenDatabase()
{
lock (_mutex)
lock (_semaphore)
{
_stack++;

if (_stack == 1)
{
try
{
_mutex.WaitOne();
}
catch (AbandonedMutexException) { }
_semaphore.WaitOne();

try
{
_engine = new LiteEngine(_settings);
}
catch
{
_mutex.ReleaseMutex();
_semaphore.Release();
_stack = 0;
throw;
}
Expand All @@ -81,7 +77,7 @@ private void OpenDatabase()
/// </summary>
private void CloseDatabase()
{
lock (_mutex)
lock (_semaphore)
{
_stack--;

Expand All @@ -90,7 +86,7 @@ private void CloseDatabase()
_engine.Dispose();
_engine = null;

_mutex.ReleaseMutex();
_semaphore.Release();
}
}
}
Expand Down Expand Up @@ -381,7 +377,7 @@ protected virtual void Dispose(bool disposing)
{
_engine.Dispose();

_mutex.ReleaseMutex();
_semaphore.Release();
}
}
}
Expand Down