83 lines
3.0 KiB
C#
83 lines
3.0 KiB
C#
using ln.collections;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace ln.objects.storage
|
|
{
|
|
class BinaryObjectCache
|
|
{
|
|
BTree<Guid, BTree<int, BinaryObject>> firstLevel = new BTree<Guid, BTree<int, BinaryObject>>();
|
|
|
|
|
|
public BinaryObjectCache() { }
|
|
|
|
BTree<int,BinaryObject> GetSecondLevel(Guid uid)
|
|
{
|
|
if (!firstLevel.TryGet(uid,out BTree<int,BinaryObject> secondLevel))
|
|
{
|
|
secondLevel = new BTree<int, BinaryObject>();
|
|
firstLevel.Add(uid, secondLevel);
|
|
}
|
|
return secondLevel;
|
|
}
|
|
bool TryGetSecondLevel(Guid uid, out BTree<int, BinaryObject> secondLevel) => firstLevel.TryGet(uid, out secondLevel) && !secondLevel.Empty;
|
|
|
|
public void Add(BinaryObject binaryObject)
|
|
{
|
|
BTree<int, BinaryObject> secondLevel = GetSecondLevel(binaryObject.UID);
|
|
secondLevel.Add(binaryObject.Version, binaryObject);
|
|
}
|
|
public void Remove(BinaryObject binaryObject)
|
|
{
|
|
BTree<int, BinaryObject> secondLevel = GetSecondLevel(binaryObject.UID);
|
|
secondLevel.Remove(binaryObject.Version);
|
|
}
|
|
|
|
public bool Contains(Guid uid) => GetSecondLevel(uid).ContainsKey(uid);
|
|
public IEnumerable<Guid> UIDs => firstLevel.Keys;
|
|
|
|
public IEnumerable<BinaryObject> GetBinaryObjects(Guid uid) => GetSecondLevel(uid).Values;
|
|
public BinaryObject GetLatestBinaryObject(Guid uid)
|
|
{
|
|
BTree<int, BinaryObject> secondLevel = GetSecondLevel(uid);
|
|
return secondLevel.LastValue();
|
|
}
|
|
public BinaryObject GetBinaryObject(Guid uid, int version) => GetSecondLevel(uid)[version];
|
|
|
|
public bool TryGetBinaryObjects(Guid uid,out IEnumerable<BinaryObject> binaryObjects)
|
|
{
|
|
if (!TryGetSecondLevel(uid,out BTree<int,BinaryObject> secondLevel))
|
|
{
|
|
binaryObjects = null;
|
|
return false;
|
|
}
|
|
|
|
binaryObjects = secondLevel.Values;
|
|
return true;
|
|
}
|
|
public bool TryGetBinaryObject(Guid uid, int version, out BinaryObject binaryObject)
|
|
{
|
|
if (TryGetSecondLevel(uid, out BTree<int, BinaryObject> secondLevel))
|
|
{
|
|
if (version == -1)
|
|
{
|
|
if (secondLevel.TryGetLastValue(out binaryObject))
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (secondLevel.TryGet(version, out binaryObject))
|
|
return true;
|
|
}
|
|
}
|
|
binaryObject = null;
|
|
return false;
|
|
}
|
|
|
|
public bool TryGetLatestBinaryObject(Guid uid, out BinaryObject binaryObject) => GetSecondLevel(uid).TryGetLastValue(out binaryObject);
|
|
public bool TryGetFirstBinaryObject(Guid uid, out BinaryObject binaryObject) => GetSecondLevel(uid).TryGetFirstValue(out binaryObject);
|
|
|
|
}
|
|
}
|