Mapper: implement Refresh(..)
parent
c64736e8e0
commit
67eae8f9df
|
@ -6,6 +6,7 @@ using ln.types.odb.ng.index;
|
|||
using System.Linq;
|
||||
using System.Collections;
|
||||
using ln.types.odb.ng.storage;
|
||||
using ln.types.odb.ng.mappings;
|
||||
|
||||
namespace ln.types.odb.ng
|
||||
{
|
||||
|
@ -15,40 +16,70 @@ namespace ln.types.odb.ng
|
|||
BTree<Guid, CachedObject> forwardCache = new BTree<Guid, CachedObject>();
|
||||
Dictionary<object, CachedObject> reverseCache = new Dictionary<object, CachedObject>();
|
||||
|
||||
public IEnumerable<T> Load<T>() => Load(typeof(T)).Cast<T>();
|
||||
public IEnumerable Load(Type type)
|
||||
public IEnumerable<T> Load<T>() => Load(typeof(T),false).Cast<T>();
|
||||
public IEnumerable<T> Load<T>(bool refresh) => Load(typeof(T),refresh).Cast<T>();
|
||||
public IEnumerable Load(Type type) => Load(type, false);
|
||||
public IEnumerable Load(Type type,bool refresh)
|
||||
{
|
||||
return new MappedObjectEnumeration(this, type, GetDocumentIDs(type));
|
||||
return new MappedObjectEnumeration(this, type, GetDocumentIDs(type),refresh);
|
||||
}
|
||||
|
||||
public T Load<T>(Guid documentID) => (T)Load(typeof(T), documentID);
|
||||
public object Load(Type type, Guid documentID)
|
||||
public T Load<T>(Guid documentID) => (T)Load(typeof(T), documentID, false);
|
||||
public T Load<T>(Guid documentID,bool refresh) => (T)Load(typeof(T), documentID, refresh);
|
||||
public object Load(Type type, Guid documentID) => Load(type, documentID, false);
|
||||
public object Load(Type type, Guid documentID,bool refresh)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (forwardCache.ContainsKey(documentID))
|
||||
return forwardCache[documentID].Instance;
|
||||
{
|
||||
CachedObject cachedObject = forwardCache[documentID];
|
||||
if (refresh)
|
||||
Refresh(type, cachedObject.Instance);
|
||||
return cachedObject.Instance;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
IStorage storage = StorageContainer.GetStorage(type.FullName);
|
||||
Document document = storage.Load(documentID);
|
||||
IStorage storage = StorageContainer.GetStorage(type.FullName);
|
||||
Document document = storage.Load(documentID);
|
||||
|
||||
object instance = ObjectMapping.UnmapValue(this, document);
|
||||
object instance = ObjectMapping.UnmapValue(this, document);
|
||||
|
||||
CachedObject cachedObject = new CachedObject(document, instance);
|
||||
forwardCache.Add(cachedObject.Document.ID, cachedObject);
|
||||
reverseCache.Add(cachedObject.Instance, cachedObject);
|
||||
CachedObject cachedObject = new CachedObject(document, instance);
|
||||
forwardCache.Add(cachedObject.Document.ID, cachedObject);
|
||||
reverseCache.Add(cachedObject.Instance, cachedObject);
|
||||
|
||||
return cachedObject.Instance;
|
||||
return cachedObject.Instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
public IEnumerable<T> Load<T>(Query query) => Load(typeof(T), query).Cast<T>();
|
||||
public IEnumerable Load(Type type,Query query)
|
||||
public IEnumerable<T> Load<T>(Query query) => Load(typeof(T), query, false).Cast<T>();
|
||||
public IEnumerable<T> Load<T>(Query query,bool refresh) => Load(typeof(T), query, refresh).Cast<T>();
|
||||
public IEnumerable Load(Type type, Query query) => Load(type, query, false);
|
||||
public IEnumerable Load(Type type,Query query,bool refresh)
|
||||
{
|
||||
IEnumerable<Guid> matchedIDs = GetDocumentIDs(type,query);
|
||||
|
||||
return new MappedObjectEnumeration(this, type, matchedIDs);
|
||||
return new MappedObjectEnumeration(this, type, matchedIDs,refresh);
|
||||
}
|
||||
|
||||
public bool Refresh<T>(T instance) => Refresh(typeof(T), instance);
|
||||
public bool Refresh(Type type,object instance)
|
||||
{
|
||||
if (!reverseCache.TryGetValue(instance, out CachedObject cachedObject))
|
||||
return false;
|
||||
|
||||
IStorage storage = StorageContainer.GetStorage(type.FullName);
|
||||
if (storage.Refresh(cachedObject.Document))
|
||||
{
|
||||
(GetMapping(type) as ClassMapping).Apply(this,cachedObject.Document,cachedObject.Instance);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void Save<T>(T instance) => Save(typeof(T), instance);
|
||||
public void Save(Type type, object instance)
|
||||
{
|
||||
|
@ -157,18 +188,20 @@ namespace ln.types.odb.ng
|
|||
Mapper mapper;
|
||||
Type type;
|
||||
IEnumerable<Guid> documentIDs;
|
||||
bool refresh;
|
||||
|
||||
public MappedObjectEnumeration(Mapper mapper,Type type,IEnumerable<Guid> documentIDs)
|
||||
public MappedObjectEnumeration(Mapper mapper,Type type,IEnumerable<Guid> documentIDs,bool refresh)
|
||||
{
|
||||
this.mapper = mapper;
|
||||
this.type = type;
|
||||
this.documentIDs = documentIDs;
|
||||
this.refresh = refresh;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
foreach (Guid documentID in documentIDs)
|
||||
yield return mapper.Load(type, documentID);
|
||||
yield return mapper.Load(type, documentID, refresh);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -72,10 +72,17 @@ namespace ln.types.odb.ng.mappings
|
|||
Document document = oval as Document;
|
||||
object o = GetObjectForDocument(document);
|
||||
|
||||
Apply(mapper, document, o);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
public void Apply(Mapper mapper,Document document,object o)
|
||||
{
|
||||
foreach (FieldInfo fieldInfo in mappedFields)
|
||||
{
|
||||
object fv = mapper.UnmapValue(fieldInfo.FieldType, document[fieldInfo.Name]);
|
||||
if (!object.ReferenceEquals(fv,null))
|
||||
if (!object.ReferenceEquals(fv, null))
|
||||
{
|
||||
Type st = fv.GetType();
|
||||
if (st != fieldInfo.FieldType)
|
||||
|
@ -86,13 +93,12 @@ namespace ln.types.odb.ng.mappings
|
|||
if (st != fieldInfo.FieldType)
|
||||
fv = Convert.ChangeType(fv, fieldInfo.FieldType);
|
||||
}
|
||||
if (!object.ReferenceEquals(null,fv))
|
||||
if (!object.ReferenceEquals(null, fv))
|
||||
fieldInfo.SetValue(o, fv);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
public Document MapDocument(Mapper mapper,Guid documentID,object value)
|
||||
public Document MapDocument(Mapper mapper,Guid documentID,object value)
|
||||
{
|
||||
Document document = new Document(documentID);
|
||||
document["__asm__"] = new ODBStringValue(value.GetType().Assembly.GetName().Name);
|
||||
|
|
Loading…
Reference in New Issue