ln.types/odb/ODBFileStorage.cs

160 lines
5.3 KiB
C#

// /**
// * File: ODB.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.IO;
using System.Collections.Generic;
using Castle.DynamicProxy;
using ln.types.serialize;
using Castle.Components.DictionaryAdapter;
using System.Linq;
using System.Reflection;
namespace ln.types.odb
{
//public class ODBFileStorage : Storage
//{
// public String BasePath { get; set; }
// public int StorageVersion { get; } = 0x01;
// public ODBFileStorage(string basePath)
// {
// BasePath = Path.GetFullPath(basePath);
// if (!Directory.Exists(BasePath))
// {
// Directory.CreateDirectory(BasePath);
// }
// }
// private string GetPersistentPath(Guid persistenceID)
// {
// byte[] idBytes = persistenceID.ToByteArray();
// return Path.Combine(BasePath, BitConverter.ToString(idBytes, 0, 4));
// }
// public override bool Contains(Guid persistenceID)
// {
// string fn = Path.Combine(GetPersistentPath(persistenceID), String.Format("{0}.0", persistenceID));
// return File.Exists(fn);
// }
// public override bool Store(PreparedObject preparedObject)
// {
// string targetPath = GetPersistentPath(preparedObject.PersistenceID);
// String fnbase = Path.Combine(targetPath, preparedObject.PersistenceID.ToString());
// if (!Directory.Exists(targetPath))
// Directory.CreateDirectory(targetPath);
// String fnn = String.Format("{0}.new", fnbase);
// if (File.Exists(fnn))
// File.Delete(fnn);
// using (FileStream fs = new FileStream(fnn, FileMode.CreateNew))
// {
// ToStream(preparedObject, fs);
// fs.Close();
// }
// for (int n = 5; n > 0; n--)
// {
// string fn1 = String.Format("{0}.{1}", fnbase, n - 1);
// string fn2 = String.Format("{0}.{1}", fnbase, n);
// if (File.Exists(fn1))
// {
// if (File.Exists(fn2))
// File.Delete(fn2);
// File.Move(fn1, fn2);
// }
// }
// string fn = String.Format("{0}.{1}", fnbase, 0);
// File.Move(fnn, fn);
// return true;
// }
// public override bool Load(PreparedObject preparedObject)
// {
// string targetPath = GetPersistentPath(preparedObject.PersistenceID);
// String fnbase = Path.Combine(targetPath, preparedObject.PersistenceID.ToString());
// string fn = String.Format("{0}.0", fnbase);
// using (FileStream fs = new FileStream(fn, FileMode.Open))
// {
// FromStream(fs, preparedObject);
// fs.Close();
// }
// return true;
// }
// private void ToStream(PreparedObject preparedObject,Stream stream)
// {
// using (BinaryWriter writer = new BinaryWriter(stream))
// {
// writer.Write(StorageVersion);
// writer.Write(DateTimeOffset.Now.ToUnixTimeMilliseconds());
// writer.Write(preparedObject.PreparedType.Assembly.GetName().Name);
// writer.Write(preparedObject.PreparedType.FullName);
// string[] fieldNames = preparedObject.StoredFieldNames;
// writer.Write(fieldNames.Length);
// foreach (String fieldName in fieldNames)
// {
// byte[] fieldBytes = preparedObject.GetFieldBytes(fieldName);
// writer.Write(fieldName);
// writer.Write(fieldBytes.Length);
// writer.Write(fieldBytes);
// }
// writer.Close();
// }
// }
// private void FromStream(Stream stream,PreparedObject preparedObject)
// {
// using (BinaryReader reader = new BinaryReader(stream))
// {
// if (reader.ReadInt32() != StorageVersion)
// throw new FormatException("Unsupported Storage Version");
// DateTime timestamp = DateTimeOffset.FromUnixTimeMilliseconds(reader.ReadInt64()).DateTime;
// string assemblyName = reader.ReadString();
// string typeName = reader.ReadString();
// Assembly assembly = Assembly.Load(assemblyName);
// Type targetType = assembly.GetType(typeName);
// preparedObject.ReConfigure(targetType,timestamp);
// int nStoredFields = reader.ReadInt32();
// for (int n = 0; n < nStoredFields; n++)
// {
// string fieldName = reader.ReadString();
// int nBytes = reader.ReadInt32();
// byte[] fieldBytes = reader.ReadBytes(nBytes);
// preparedObject.SetFieldBytes(fieldName, fieldBytes);
// }
// reader.Close();
// }
// }
//}
}