public void Load(BinaryReader reader, Action <int, string> values, int count) { if (reader.ReadByte() != VERSION) { throw new Exception("Invalid StringIndexerPersist version."); } PersistMode mode = (PersistMode)reader.ReadByte(); switch (mode) { case PersistMode.Raw: { new Raw().Load(reader, values, count); } break; case PersistMode.Dictionary: { string[] map = new string[reader.ReadInt32()]; for (int i = 0; i < map.Length; i++) { map[i] = reader.ReadString(); } new Int32IndexerPersist().Load(reader, (idx, value) => { values(idx, value == NULL_ID ? null : map[value]); }, count); } break; default: throw new NotSupportedException(mode.ToString()); } }
bool IDataPersistence.Persist(IPersistence persistence) { persistence.UpsertField(Constants.IDataPesistence_InitializeMode, InitializeMode.ToString()); persistence.UpsertField(Constants.IDataPesistence_PersistMode, PersistMode.ToString()); persistence.UpsertFieldArray(Constants.IDataPesistence_DataConnections, DataConnections.ToArray()); return(true); }
public void Store(BinaryWriter writer, Func <int, string> values, int count) { writer.Write(VERSION); int MAP_CAPACITY = (int)((PERCENT / 100) * count); Dictionary <string, int> map = new Dictionary <string, int>(/*MAP_CAPACITY*/); //optimistic variant int ID = 0; int[] indexes = new int[count]; PersistMode mode = PersistMode.Dictionary; for (int i = 0; i < count; i++) { var value = values(i); if (value == null) { indexes[i] = NULL_ID; continue; } int id; if (map.TryGetValue(value, out id)) { indexes[i] = id; continue; } if (map.Count == MAP_CAPACITY) { mode = PersistMode.Raw; break; } map.Add(value, ID); indexes[i] = ID; ID++; } writer.Write((byte)mode); switch (mode) { case PersistMode.Raw: { new Raw().Store(writer, values, count); } break; case PersistMode.Dictionary: { writer.Write(map.Count); foreach (var kv in map.OrderBy(x => x.Value)) { writer.Write(kv.Key); } new Int32IndexerPersist().Store(writer, (idx) => { return(indexes[idx]); }, count); } break; default: throw new NotSupportedException(mode.ToString()); } }