135 lines
4.3 KiB
C#
135 lines
4.3 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using ln.bson.mapper;
|
||
|
|
||
|
namespace ln.bson.storage
|
||
|
{
|
||
|
/*
|
||
|
public class MapperSession<T> : IEnumerable<T>, IDisposable
|
||
|
{
|
||
|
private ByteArrayFileStorage _byteArrayFileStorage;
|
||
|
public BsonMapper BsonMapper { get; }
|
||
|
|
||
|
private Dictionary<long, T> instanceCache = new Dictionary<long, T>();
|
||
|
private Dictionary<T, BsonDocument> lookupCache = new Dictionary<T, BsonDocument>();
|
||
|
|
||
|
public MapperSession(ByteArrayFileStorage byteArrayFileStorage)
|
||
|
{
|
||
|
_byteArrayFileStorage = byteArrayFileStorage;
|
||
|
BsonMapper = BsonMapper.DefaultInstance;
|
||
|
}
|
||
|
|
||
|
bool GetInstance(BsonDocument bsonDocument, out T instance)
|
||
|
{
|
||
|
if (!instanceCache.TryGetValue(bsonDocument.StorageTag, out instance))
|
||
|
{
|
||
|
instance = BsonMapper.Unmap<T>(bsonDocument);
|
||
|
instanceCache.Add(bsonDocument.StorageTag, instance);
|
||
|
lookupCache.Add(instance, bsonDocument);
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public bool Save(T instance)
|
||
|
{
|
||
|
BsonDocument newBsonDocument = BsonMapper.Map(instance);
|
||
|
BsonDocument oldBsonDocument = null;
|
||
|
long documentOffset;
|
||
|
|
||
|
if (lookupCache.TryGetValue(instance, out oldBsonDocument))
|
||
|
{
|
||
|
if (oldBsonDocument.Equals(newBsonDocument))
|
||
|
return true;
|
||
|
|
||
|
if (_byteArrayFileStorage.TrySaveDocumentBytes(newBsonDocument.GetBytes(), out documentOffset))
|
||
|
{
|
||
|
newBsonDocument.StorageTag = documentOffset;
|
||
|
instanceCache.Remove(oldBsonDocument.StorageTag);
|
||
|
instanceCache.Add(documentOffset, instance);
|
||
|
lookupCache.Remove(instance);
|
||
|
lookupCache.Add(instance, newBsonDocument);
|
||
|
return _byteArrayFileStorage.Remove(oldBsonDocument.StorageTag);
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (_byteArrayFileStorage.TrySaveDocumentBytes(newBsonDocument.GetBytes(), out documentOffset))
|
||
|
{
|
||
|
instanceCache.Add(documentOffset, instance);
|
||
|
lookupCache.Add(instance, newBsonDocument);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public bool Delete(T instance)
|
||
|
{
|
||
|
if (!lookupCache.TryGetValue(instance, out BsonDocument bsonDocument))
|
||
|
return true;
|
||
|
|
||
|
if (_byteArrayFileStorage.Remove(bsonDocument.StorageTag))
|
||
|
{
|
||
|
lookupCache.Remove(instance);
|
||
|
instanceCache.Remove(bsonDocument.StorageTag);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void Dispose()
|
||
|
{
|
||
|
instanceCache?.Clear();
|
||
|
instanceCache = null;
|
||
|
lookupCache?.Clear();
|
||
|
lookupCache = null;
|
||
|
_byteArrayFileStorage = null;
|
||
|
}
|
||
|
|
||
|
public class Enumerator : IEnumerator<T>
|
||
|
{
|
||
|
private MapperSession<T> MapperSession;
|
||
|
private IEnumerator<BsonDocument> StorageIterator;
|
||
|
private T currentInstance;
|
||
|
public Enumerator(MapperSession<T> mappingSession)
|
||
|
{
|
||
|
MapperSession = mappingSession;
|
||
|
StorageIterator = MapperSession._byteArrayFileStorage.GetEnumerator();
|
||
|
}
|
||
|
|
||
|
public bool MoveNext()
|
||
|
{
|
||
|
if (StorageIterator.MoveNext())
|
||
|
{
|
||
|
if (!MapperSession.GetInstance(StorageIterator.Current, out currentInstance))
|
||
|
throw new Exception();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void Reset()
|
||
|
{
|
||
|
currentInstance = default(T);
|
||
|
StorageIterator.Reset();
|
||
|
}
|
||
|
|
||
|
public T Current => currentInstance;
|
||
|
object IEnumerator.Current => Current;
|
||
|
|
||
|
public void Dispose()
|
||
|
{
|
||
|
Reset();
|
||
|
MapperSession = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public IEnumerator<T> GetEnumerator() => new Enumerator(this);
|
||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||
|
}
|
||
|
*/
|
||
|
}
|