Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1355 for branches


Ignore:
Timestamp:
03/18/09 14:51:38 (16 years ago)
Author:
epitzer
Message:

Implement a type cache. (#506)

Location:
branches/New Persistence Exploration/Persistence
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs

    r1349 r1355  
    6363    private readonly Stack<IAccessibleObject> compositeStack;
    6464    private readonly PersistenceConfiguration persistenceConfiguration;
     65    private readonly Dictionary<int, Type> typeIds;
    6566
    6667    delegate void Thunk();
    6768    private List<Thunk> finalFixes;
    6869
    69     public DeSerializer() : this(PersistenceConfiguration.Instance) {}
     70    public DeSerializer(IEnumerable<KeyValuePair<string, int>> typeCache) :
     71      this(typeCache, PersistenceConfiguration.Instance) {}
    7072 
    71     public DeSerializer(PersistenceConfiguration persistenceConfiguration) {
     73    public DeSerializer(
     74      IEnumerable<KeyValuePair<string, int>> typeCache,
     75      PersistenceConfiguration persistenceConfiguration) {
    7276      this.persistenceConfiguration = persistenceConfiguration;
    7377      id2obj = new Dictionary<int, object>();
     
    8084                     {typeof (Null), NullHandler}
    8185                   };     
     86      typeIds = new Dictionary<int, Type>();
     87      foreach ( var pair in typeCache ) {
     88        Type type = Type.GetType(pair.Key);
     89        typeIds.Add(pair.Value, type);
     90      }
    8291    }
    8392
     
    96105      CompositeStart start = (CompositeStart)token;
    97106      object instance;
    98       if (persistenceConfiguration.GetDecomposer(start.Type) != null) {
     107      Type type = typeIds[int.Parse(start.TypeId)];
     108      if (persistenceConfiguration.GetDecomposer(type) != null) {
    99109        instance = new ParentReference();
    100110        compositeStack.Push(new CustomObject(instance));       
    101111      } else {       
    102         instance = Activator.CreateInstance(start.Type, true);
     112        instance = Activator.CreateInstance(type, true);
    103113        Dictionary<string, DataMemberAccessor> accessorDict =
    104114          StorableAttribute.GetAutostorableAccessors(instance);
     
    109119    }
    110120    private void CompositeEndHandler(IParseToken token) {
    111       CompositeEnd end = (CompositeEnd)token;     
    112       IDecomposer decomposer = persistenceConfiguration.GetDecomposer(end.Type);
     121      CompositeEnd end = (CompositeEnd)token;
     122      Type type = typeIds[int.Parse(end.TypeId)];
     123      IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type);
    113124      if (decomposer != null) {
    114125        CustomObject customObject = (CustomObject)compositeStack.Pop();
    115126        object deserializedObject =
    116           decomposer.DeSerialize(customObject.customValues, end.Type);
     127          decomposer.DeSerialize(customObject.customValues, type);
    117128        if ( end.Id != null )
    118129          id2obj[(int)end.Id] = deserializedObject;       
     
    126137    private void PrimitiveHandler(IParseToken token) {
    127138      Primitive primitive = (Primitive)token;
     139      Type type = typeIds[int.Parse(primitive.TypeId)];
    128140      object value = persistenceConfiguration
    129         .GetFormatter(XmlFormat.Instance, primitive.Type)
     141        .GetFormatter(XmlFormat.Instance, type)
    130142        .DeSerialize(primitive.SerializedValue);
    131143      if ( ! value.GetType().IsValueType )
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1349 r1355  
    22using System.Collections;
    33using System;
     4using System.Linq;
    45
    56namespace Persistence {
     
    1213    private readonly Dictionary<Type, int> typeCache;
    1314    private readonly PersistenceConfiguration persistenceConfiguration;
     15
     16    public Dictionary<string, int> TypeCache {
     17      get {       
     18        Dictionary<string, int> result = new Dictionary<string, int>();
     19        foreach ( var pair in typeCache )
     20          result.Add(pair.Key.AssemblyQualifiedName, pair.Value);
     21        return result;                                     
     22      }
     23    }
    1424
    1525    public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {}
     
    3545      IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor);
    3646      while (iterator.MoveNext())
    37         yield return iterator.Current;
    38       Console.WriteLine("TypeCache:");
    39       foreach (var pair in typeCache) {               
    40         Console.WriteLine("<TYPE id=\"{0}\" AssemblyQualifiedName=\"{1}\">",
    41           pair.Value,
    42           pair.Key.AssemblyQualifiedName);
    43       }
     47        yield return iterator.Current;     
    4448    }
    4549
     
    5761        yield break;
    5862      }
    59 
     63     
    6064      if ( ! typeCache.ContainsKey(value.GetType()))
    6165        typeCache.Add(value.GetType(), typeCache.Count);
     66      int typeId = typeCache[value.GetType()];
    6267
    6368      int? id = null;
     
    7075      if (formatter != null) {
    7176        yield return new PrimitiveToken(
    72           accessor,
     77          accessor.Name,
     78          typeId,
    7379          formatter.Serialize(value),         
    7480          id);
     
    7682      }
    7783     
    78       yield return new BeginToken(accessor, id);
     84      yield return new BeginToken(accessor.Name, typeId, id);
    7985      IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType());     
    8086
     
    8591            yield return iterator.Current;
    8692        }
    87         yield return new EndToken(accessor, id);
     93        yield return new EndToken(accessor.Name, typeId, id);
    8894        yield break;
    8995      }
     
    103109                                         value.GetType().FullName));
    104110      }
    105       yield return new EndToken(accessor, id);
     111      yield return new EndToken(accessor.Name, typeId, id);
    106112    }
    107113   
  • branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs

    r1339 r1355  
    66  public interface ISerializationToken {
    77  }
    8   public class BeginToken : ISerializationToken {
    9     public readonly DataMemberAccessor Accessor;
     8  public class BeginToken : ISerializationToken {   
     9    public readonly string Name;
     10    public readonly int? TypeId;
    1011    public readonly int? Id;
    11     public BeginToken(DataMemberAccessor accessor, int? id) {
    12       Accessor = accessor;
     12    public BeginToken(string name, int? typeId, int? id) {
     13      Name = name;
     14      TypeId = typeId;
    1315      Id = id;
    1416    }
    1517  }
    1618  public class EndToken : ISerializationToken {
    17     public readonly DataMemberAccessor Accessor;
     19    public readonly string Name;
     20    public readonly int? TypeId;
    1821    public readonly int? Id;
    19     public EndToken(DataMemberAccessor accessor, int? id) {
    20       Accessor = accessor;
     22    public EndToken(string name, int? typeId, int? id) {
     23      Name = name;
     24      TypeId = typeId;
    2125      Id = id;
    2226    }
    2327  }
    2428  public class PrimitiveToken : ISerializationToken {
    25     public readonly DataMemberAccessor Accessor;
    26     public readonly object Data;
     29    public readonly string Name;
     30    public readonly int? TypeId;
    2731    public readonly int? Id;
    28     public PrimitiveToken(DataMemberAccessor accessor, object data, int? id) {
    29       Accessor = accessor;
    30       Data = data;
     32    public readonly object SerialData;
     33    public PrimitiveToken(string name, int? typeId, object serialData, int? id) {
     34      Name = name;
     35      TypeId = typeId;
     36      SerialData = serialData;     
    3137      Id = id;
    3238    }
     
    5460  public class CompositeStart : IParseToken {
    5561    public readonly string Name;
    56     public readonly Type Type;
     62    public readonly string TypeId;
    5763    public readonly int? Id;
    58     public CompositeStart(string name, Type type, int? id) {
     64    public CompositeStart(string name, string typeId, int? id) {
    5965      Name = name;
    60       Type = type;
     66      TypeId = typeId;
    6167      Id = id;
    6268    }
     
    6470  public class CompositeEnd : IParseToken {
    6571    public readonly string Name;
    66     public readonly Type Type;
     72    public readonly string TypeId;
    6773    public readonly int? Id;
    68     public CompositeEnd(string name, Type type, int? id) {
     74    public CompositeEnd(string name, string typeId, int? id) {
    6975      Name = name;
    70       Type = type;
     76      TypeId = typeId;
    7177      Id = id;
    7278    }
     
    7480  public class Primitive : IParseToken {
    7581    public readonly string Name;
    76     public readonly Type Type;
     82    public readonly string TypeId;
    7783    public readonly string SerializedValue;
    7884    public readonly int? Id;
    79     public Primitive(string name, Type type, string serilaizedValue, int? id) {
     85    public Primitive(string name, string typeId, string serilaizedValue, int? id) {
    8086      Name = name;
    81       Type = type;
     87      TypeId = typeId;
    8288      SerializedValue = serilaizedValue;
    8389      Id = id;
  • branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs

    r1339 r1355  
    5252    private string FormatBegin(ISerializationToken token) {     
    5353      BeginToken beginToken = (BeginToken)token;
    54       var attributes = new Dictionary<string, string> {
    55         {"name", beginToken.Accessor.Name},
    56         {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName } };
     54      var attributes = new Dictionary<string, string> {{"name", beginToken.Name}};
     55      if ( beginToken.TypeId != null )
     56        attributes.Add("typeId", beginToken.TypeId.ToString());
    5757      if ( beginToken.Id != null )
    5858        attributes.Add("id", beginToken.Id.ToString());                                           
     
    7272      Dictionary<string, string> attributes =
    7373        new Dictionary<string, string> {
    74             {"type", dataToken.Accessor.Get().GetType().AssemblyQualifiedName}};
    75       if ( !string.IsNullOrEmpty(dataToken.Accessor.Name) )
    76         attributes.Add("name", dataToken.Accessor.Name);
     74            {"typeId", dataToken.TypeId.ToString()}};
     75      if ( !string.IsNullOrEmpty(dataToken.Name) )
     76        attributes.Add("name", dataToken.Name);
    7777      if ( dataToken.Id != null )
    7878        attributes.Add("id", dataToken.Id.ToString());
    7979      return Prefix +
    8080        FormatNode("PRIMITIVE", attributes, NodeType.Start) +
    81         dataToken.Data + "</PRIMITIVE>\n";     
     81        dataToken.SerialData + "</PRIMITIVE>\n";     
    8282    }
    8383
     
    9898      return Prefix + FormatNode("NULL", attributes, NodeType.Inline) + "\n";
    9999    }
     100
     101    public IEnumerable<string> Format(Dictionary<string, int> typeCache) {
     102      yield return "<TYPECACHE>";
     103      foreach ( var pair in typeCache ) {
     104        yield return String.Format("  <TYPE id=\"{0}\" name=\"{1}\"/>",
     105                                   pair.Value, pair.Key);       
     106      }
     107      yield return "</TYPECACHE>";
     108    }
    100109  }
    101110}
  • branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs

    r1339 r1355  
    66
    77namespace Persistence {
     8
     9
    810  public class XmlParser : IEnumerable<IParseToken> {
    911
     
    5254      yield return new Primitive(
    5355        reader.GetAttribute("name"),
    54         Type.GetType(reader.GetAttribute("type")),
     56        reader.GetAttribute("typeId"),       
    5557        reader.ReadString(),
    5658        id);
     
    5860    private IEnumerator<IParseToken> ParseComposite() {
    5961      string name = reader.GetAttribute("name");     
    60       Type type = Type.GetType(reader.GetAttribute("type"));
     62      string typeId = reader.GetAttribute("typeId");
    6163      string idString = reader.GetAttribute("id");
    6264      int? id = null;
    6365      if (idString != null)
    6466        id = int.Parse(idString);     
    65       yield return new CompositeStart(name, type, id);
     67      yield return new CompositeStart(name, typeId, id);
    6668      IEnumerator<IParseToken> iterator = GetEnumerator();
    6769      while (iterator.MoveNext())
    6870        yield return iterator.Current;
    69       yield return new CompositeEnd(name, type, id);
     71      yield return new CompositeEnd(name, typeId, id);
    7072    }
    7173    private IEnumerator<IParseToken> ParseReference() {
     
    8082      return GetEnumerator();
    8183    }
     84    public static Dictionary<string, int> ParseTypeCache(TextReader reader) {
     85      Dictionary<string, int> typeCache = new Dictionary<string, int>();
     86      XmlReader xmlReader = XmlReader.Create(reader);
     87      while ( xmlReader.Read() ) {
     88        if (xmlReader.Name == "TYPE") {         
     89          typeCache.Add(xmlReader.GetAttribute("name")
     90          , int.Parse(xmlReader.GetAttribute("id")));
     91        }
     92      }
     93      return typeCache;
     94    }
    8295  } 
    8396}
  • branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs

    r1354 r1355  
    2323      }
    2424      writer.Close();
     25      writer = new StreamWriter("test-types.xml");
     26      foreach (string line in xmlFormatter.Format(s.TypeCache)) {
     27        writer.WriteLine(line);
     28        Console.Out.WriteLine(line);
     29      }
     30      writer.Close();
     31     
    2532      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
    26       DeSerializer deSerializer = new DeSerializer();       
     33      DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));       
    2734      object o = deSerializer.DeSerialize(parser);
    2835      Root t = CloningFactory.DefaultClone(r);
     
    4249      writer.Close();
    4350      XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
    44       DeSerializer deSerializer = new DeSerializer();
     51      DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));             
    4552      object o = deSerializer.DeSerialize(parser);
    4653      Manager n = CloningFactory.DefaultClone(m);
     
    120127
    121128    public static void Main() {     
    122       //Test1();     
     129      Test1();     
    123130      //Test2();
    124131      //SpeedTest();
Note: See TracChangeset for help on using the changeset viewer.