sharp-oodb/index/Unique.cs

74 lines
2.0 KiB
C#

using System;
using System.Reflection;
using System.Collections.Generic;
namespace oodb.index
{
public class Unique : Index
{
Dictionary<Guid, object> uniqueForward = new Dictionary<Guid, object>();
Dictionary<object, Guid> uniqueReverse = new Dictionary<object, Guid>();
public Unique(OODB oodb,FieldInfo fieldInfo)
:base(oodb,fieldInfo)
{
}
protected override void Prepare()
{
}
public override void Add(Persistent persistent)
{
object value = FieldInfo.GetValue(persistent);
Guid persistenceId = persistent.PersistenceID;
if (uniqueReverse.ContainsKey(value))
{
if (uniqueReverse[value].Equals(persistenceId))
return;
throw new ArgumentException();
}
if (uniqueForward.ContainsKey(persistenceId))
Remove(persistenceId);
uniqueForward.Add(persistenceId, value);
uniqueReverse.Add(value, persistenceId);
}
public override IEnumerable<Guid> Query()
{
throw new NotImplementedException();
}
public override void Remove(Guid persistenceID)
{
if (uniqueForward.ContainsKey(persistenceID))
{
uniqueReverse.Remove(uniqueForward[persistenceID]);
uniqueForward.Remove(persistenceID);
}
}
public override void RestoreIndexValues(IEnumerable<KeyValuePair<Guid, object>> indexValues)
{
uniqueForward.Clear();
uniqueReverse.Clear();
foreach (KeyValuePair<Guid,object> entry in indexValues)
{
uniqueForward.Add(entry.Key, entry.Value);
uniqueReverse.Add(entry.Value, entry.Key);
}
}
public override IEnumerable<KeyValuePair<Guid, object>> RetrieveIndexValues()
{
return uniqueForward;
}
}
}