112 lines
3.2 KiB
C#
112 lines
3.2 KiB
C#
|
using System;
|
|||
|
using System.Reflection;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Xml;
|
|||
|
using oodb.mapping;
|
|||
|
using System.IO;
|
|||
|
using System.Reflection.Emit;
|
|||
|
using System.Linq;
|
|||
|
namespace oodb.index
|
|||
|
{
|
|||
|
public abstract class Index
|
|||
|
{
|
|||
|
public OODB OODB { get; }
|
|||
|
public FieldInfo FieldInfo { get; }
|
|||
|
|
|||
|
public String FileName { get; }
|
|||
|
|
|||
|
public FlatTypeMapping ValueMapping { get; }
|
|||
|
public FlatTypeMapping StorageMapping { get; protected set; }
|
|||
|
|
|||
|
public Index(OODB oodb, FieldInfo fieldInfo)
|
|||
|
{
|
|||
|
OODB = oodb;
|
|||
|
FieldInfo = fieldInfo;
|
|||
|
|
|||
|
ValueMapping = OODB.GetFlatTypeMapping(fieldInfo.FieldType);
|
|||
|
StorageMapping = ValueMapping;
|
|||
|
|
|||
|
FileName = Path.Combine(
|
|||
|
OODB.StorageAdapter.GetTypeStorage(FieldInfo.DeclaringType).Directory.FullName,
|
|||
|
String.Format("index_{0}_{1}_{2}.xml", GetType().Name, FieldInfo.DeclaringType.Name, FieldInfo.Name)
|
|||
|
);
|
|||
|
|
|||
|
OODB.StorageAdapter.GetTypeStorage(fieldInfo.DeclaringType).AddIndex(this);
|
|||
|
|
|||
|
Prepare();
|
|||
|
|
|||
|
Load();
|
|||
|
}
|
|||
|
|
|||
|
protected abstract void Prepare();
|
|||
|
|
|||
|
public abstract IEnumerable<KeyValuePair<Guid, object>> RetrieveIndexValues();
|
|||
|
public abstract void RestoreIndexValues(IEnumerable<KeyValuePair<Guid, object>> indexValues);
|
|||
|
|
|||
|
public abstract void Add(Persistent persistent);
|
|||
|
public abstract void Remove(Guid persistenceID);
|
|||
|
|
|||
|
public void Remove(Persistent persistent)
|
|||
|
{
|
|||
|
Remove(persistent.PersistenceID);
|
|||
|
}
|
|||
|
|
|||
|
public abstract IEnumerable<Guid> Query();
|
|||
|
|
|||
|
|
|||
|
public void Load()
|
|||
|
{
|
|||
|
if (File.Exists(FileName))
|
|||
|
{
|
|||
|
XmlDocument xmlDocument = new XmlDocument();
|
|||
|
xmlDocument.Load(FileName);
|
|||
|
|
|||
|
RestoreIndexValues(
|
|||
|
xmlDocument.SelectNodes("/Index/Entry").Cast<XmlElement>().Select((entry) => new KeyValuePair<Guid, object>(Guid.Parse(entry.GetAttribute("PersistenceID")), StorageMapping.FromXml(entry.SelectSingleNode("Value") as XmlElement)))
|
|||
|
);
|
|||
|
}
|
|||
|
}
|
|||
|
public void Save()
|
|||
|
{
|
|||
|
XmlDocument xmlDocument = new XmlDocument();
|
|||
|
XmlElement indexElement = xmlDocument.CreateElement("Index");
|
|||
|
indexElement.SetAttribute("Type", GetType().FullName);
|
|||
|
|
|||
|
xmlDocument.AppendChild(indexElement);
|
|||
|
|
|||
|
|
|||
|
foreach (KeyValuePair<Guid,object> entry in RetrieveIndexValues())
|
|||
|
{
|
|||
|
XmlElement xmlEntry = xmlDocument.CreateElement("Entry");
|
|||
|
xmlEntry.SetAttribute("PersistenceID", entry.Key.ToString());
|
|||
|
xmlEntry.AppendChild(StorageMapping.ToXml(xmlDocument, entry.Value));
|
|||
|
indexElement.AppendChild(xmlEntry);
|
|||
|
}
|
|||
|
|
|||
|
xmlDocument.Save(FileName);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public override int GetHashCode()
|
|||
|
{
|
|||
|
return FileName.GetHashCode();
|
|||
|
}
|
|||
|
|
|||
|
public override bool Equals(object obj)
|
|||
|
{
|
|||
|
if (obj is Index)
|
|||
|
{
|
|||
|
Index other = obj as Index;
|
|||
|
return FileName.Equals(other.FileName);
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|