public T Get(long Position) { using (var accessor = MemoryMappedTableFile.CreateViewAccessor(Position, TableRowSize, MemoryMappedFileAccess.Read)) { byte[] Buffer = new byte[TableRowSize]; accessor.ReadArray(0, Buffer, 0, TableRowSize); return(ION.DeserializeObject <T>(Buffer)); } }
public TableColumnMap(string StoragePath, string TableName, string ColumnName, StringLengthAttribute StringLength, MaxLengthAttribute MaxLength) { int stringLength = StringLength != null ? StringLength.MaximumLength : 0; int stringArrayLength = MaxLength != null ? MaxLength.Length : 0; if (stringLength > 0) { MapRowSizeNoPosition = stringLength; if (stringArrayLength > 0) { MapRowSizeNoPosition *= stringArrayLength; } MapRowSizeNoPosition += 4; } else { MapRowSizeNoPosition = Marshal.SizeOf(typeof(T)); } MapRowSize = MapRowSizeNoPosition + 8; DeleteBuffer = Common.CreateDefaultArray(DeleteChar, MapRowSize); ColumnMap = $"{TableName}_{ColumnName}"; ColumnMapFile = $"{StoragePath}{ColumnMap}.map"; if (File.Exists(ColumnMapFile)) { FileInfo fi = new FileInfo(ColumnMapFile); CurrentColumnMapperSize = fi.Length; } MemoryMappedColumnMapFile = MemoryMappedFile.CreateFromFile(ColumnMapFile, FileMode.OpenOrCreate, ColumnMap, CurrentColumnMapperSize); MemoryMappedColumnMapStream = MemoryMappedColumnMapFile.CreateViewStream(0, CurrentColumnMapperSize); MemoryMappedColumnMapCountAccessor = MemoryMappedColumnMapFile.CreateViewAccessor(0, 4, MemoryMappedFileAccess.Write); byte[] buffer = new byte[4]; MemoryMappedColumnMapStream.Read(buffer, 0, 4); int ExpectedMappings = BitConverter.ToInt32(buffer, 0); if (ExpectedMappings > 0) { for (long position = 0; position < MemoryMappedColumnMapStream.Length; position += MapRowSize) { buffer = new byte[MapRowSizeNoPosition]; MemoryMappedColumnMapStream.Read(buffer, 0, MapRowSizeNoPosition); if (!buffer.StartsWith(DeleteChar, MapRowSizeNoPosition)) { T key = ION.DeserializeObject <T>(buffer); buffer = new byte[8]; MemoryMappedColumnMapStream.Read(buffer, 0, 8); long filePosition = BitConverter.ToInt64(buffer, 0); ColumnMapper.Add(key, new MapperFilePostion(filePosition, position)); ExpectedMappings--; if (ExpectedMappings == 0) { break; } } else { //Skip Position MemoryMappedColumnMapStream.Position += MapRowSize; } } if (ExpectedMappings > 0) { throw new Exception("Mapping File Corrupt!"); } } }
static void Main(string[] args) { //PREP Model m = new Model(); PopulateTestModel(ref m); //ION - SERIALIZE Stopwatch sw = Stopwatch.StartNew(); byte[] ion = ION.SerializeObject(m); sw.Stop(); long ionSerializeTime = sw.ElapsedTicks; //JSON - SERIALIZE sw = Stopwatch.StartNew(); string jsonStr = JsonConvert.SerializeObject(m); sw.Stop(); long jsonSerializeTime = sw.ElapsedTicks; //ION - DESERIALIZE sw = Stopwatch.StartNew(); Model ionDeserialized = ION.DeserializeObject <Model>(ion); sw.Stop(); long ionDeserializeTime = sw.ElapsedTicks; string parity1 = ION.Parity(m); string parity2 = ION.Parity(ionDeserialized); if (parity1 != parity2) { throw new Exception(":-("); } //JSON - DESERIALIZE sw = Stopwatch.StartNew(); Model jsonDeserialized = JsonConvert.DeserializeObject <Model>(jsonStr); sw.Stop(); long jsonDeserializeTime = sw.ElapsedTicks; //SIZE Console.WriteLine("--SIZE--"); Console.WriteLine("ION: " + ion.Length); Console.WriteLine("JSON: " + jsonStr.Length); long max = Math.Max(ion.Length, jsonStr.Length); long min = Math.Min(ion.Length, jsonStr.Length); float perc = ((float)max / (float)min); Console.WriteLine("DIFF: " + (ion.Length < jsonStr.Length ? "-" : "+") + (perc * 100f).ToString() + "% (" + perc.ToString() + "x)"); //SPEED - SERIALIZE Console.WriteLine(Environment.NewLine + "--SERIALIZE SPEED--"); Console.WriteLine("ION: " + ionSerializeTime); Console.WriteLine("JSON: " + jsonSerializeTime); max = Math.Max(ionSerializeTime, jsonSerializeTime); min = Math.Min(ionSerializeTime, jsonSerializeTime); perc = ((float)max / (float)min); Console.WriteLine("DIFF: " + (ionSerializeTime < jsonSerializeTime ? "-" : "+") + (perc * 100f).ToString() + "% (" + perc.ToString() + "x)"); //SPEED - DESERIALIZE Console.WriteLine(Environment.NewLine + "--DESERIALIZE SPEED--"); Console.WriteLine("ION: " + ionDeserializeTime); Console.WriteLine("JSON: " + jsonDeserializeTime); max = Math.Max(ionDeserializeTime, jsonDeserializeTime); min = Math.Min(ionDeserializeTime, jsonDeserializeTime); perc = ((float)max / (float)min); Console.WriteLine("DIFF: " + (ionDeserializeTime < jsonDeserializeTime ? "-" : "+") + (perc * 100f).ToString() + "% (" + perc.ToString() + "x)"); Console.Read(); }