FileDictionary
FileDictionary32 represents files as dictionary. Each file name is 32-bit hexadesimal, e.g. "00000000".
// Create files based map
IDictionary<uint, byte[]> files = new FileDictionary32(FileSystem.Application, directory: "");
// Write file
files[0x100] = new byte[256];
// Read file
byte[] data = files[0x100];
// List files
foreach (KeyValuePair<uint, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
FileDictionary64 represents files as dictionary. Each file name is 64-bit hexadesimal, e.g. "0000000000000000".
// Create files based map
IDictionary<ulong, byte[]> files = new FileDictionary64(FileSystem.Application, directory: "");
// Write file
files[0x100] = new byte[256];
// Read file
byte[] data = files[0x100];
// List files
foreach (KeyValuePair<ulong, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
FileDictionary128 represents files as dictionary. Each file name is 128-bit hexadesimal, e.g. "00000000000000000000000000000000".
// Create files based map
IDictionary<UInt128, byte[]> files = new FileDictionary128(FileSystem.Application, directory: "");
// Write file
files[0x200] = new byte[256];
// Read file
byte[] data = files[0x200];
// List files
foreach (KeyValuePair<UInt128, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
FileDictionary can be combined with serializer+deserializer using DictionaryAdapter.
// Files based map
IDictionary<ulong, byte[]> files = new FileDictionary64(FileSystem.Application, directory: "");
// Files based car serializer
IDictionary<string, Car> cars = new DictionaryAdapter<ulong, byte[], string, Car>(
files, null, id => new FNVHash64().HashIn(id), Car.Deserialize, c => c.Serialize(), null, c => c.Id
);
// Write car
cars["XYZ-123"] = new Car { Id = "XYZ-123", Name = "My Car" };
// Read car
Car car = cars["XYZ-123"];
// List cars
foreach (Car _car in cars.Values)
Console.WriteLine(_car);
public record Car
{
static UTF8Encoding encoding = new UTF8Encoding(false);
public string Id { get; set; } = "id";
public string Name { get; set; } = "";
public static Car Deserialize(byte[] data)
{
// Read id
int len = data[0];
string id = encoding.GetString(data, 1, len);
// Read string
string name = encoding.GetString(data, len + 1, data.Length - len - 1);
//
return new Car { Id = id, Name = name };
}
public byte[] Serialize()
{
MemoryStream ms = new MemoryStream();
// Write Id
byte[] idBytes = encoding.GetBytes(Id);
ms.WriteByte((byte)idBytes.Length);
for (int i = 0; i < idBytes.Length; i++) ms.WriteByte(idBytes[i]);
// Write name
byte[] nameBytes = encoding.GetBytes(Name);
for (int i = 0; i < nameBytes.Length; i++) ms.WriteByte(nameBytes[i]);
ms.Flush();
return ms.ToArray();
}
}
Full Example
Full example
using System.Text;
using Avalanche.FileSystem;
using Avalanche.FileSystem.Utilities;
using Avalanche.Utilities;
public class filedictionary
{
public static void Run()
{
{
// <01>
// Create files based map
IDictionary<uint, byte[]> files = new FileDictionary32(FileSystem.Application, directory: "");
// Write file
files[0x100] = new byte[256];
// Read file
byte[] data = files[0x100];
// List files
foreach (KeyValuePair<uint, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
// </01>
}
{
// <02>
// Create files based map
IDictionary<ulong, byte[]> files = new FileDictionary64(FileSystem.Application, directory: "");
// Write file
files[0x100] = new byte[256];
// Read file
byte[] data = files[0x100];
// List files
foreach (KeyValuePair<ulong, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
// </02>
}
{
// <03>
// Create files based map
IDictionary<UInt128, byte[]> files = new FileDictionary128(FileSystem.Application, directory: "");
// Write file
files[0x200] = new byte[256];
// Read file
byte[] data = files[0x200];
// List files
foreach (KeyValuePair<UInt128, byte[]> file in files)
Console.WriteLine($"{file.Key}: Length={file.Value.Length}");
// </03>
}
{
// <04>
// Files based map
IDictionary<ulong, byte[]> files = new FileDictionary64(FileSystem.Application, directory: "");
// Files based car serializer
IDictionary<string, Car> cars = new DictionaryAdapter<ulong, byte[], string, Car>(
files, null, id => new FNVHash64().HashIn(id), Car.Deserialize, c => c.Serialize(), null, c => c.Id
);
// Write car
cars["XYZ-123"] = new Car { Id = "XYZ-123", Name = "My Car" };
// Read car
Car car = cars["XYZ-123"];
// List cars
foreach (Car _car in cars.Values)
Console.WriteLine(_car);
// </04>
}
}
// <05>
public record Car
{
static UTF8Encoding encoding = new UTF8Encoding(false);
public string Id { get; set; } = "id";
public string Name { get; set; } = "";
public static Car Deserialize(byte[] data)
{
// Read id
int len = data[0];
string id = encoding.GetString(data, 1, len);
// Read string
string name = encoding.GetString(data, len + 1, data.Length - len - 1);
//
return new Car { Id = id, Name = name };
}
public byte[] Serialize()
{
MemoryStream ms = new MemoryStream();
// Write Id
byte[] idBytes = encoding.GetBytes(Id);
ms.WriteByte((byte)idBytes.Length);
for (int i = 0; i < idBytes.Length; i++) ms.WriteByte(idBytes[i]);
// Write name
byte[] nameBytes = encoding.GetBytes(Name);
for (int i = 0; i < nameBytes.Length; i++) ms.WriteByte(nameBytes[i]);
ms.Flush();
return ms.ToArray();
}
}
// </05>
}