/// <summary> /// Serialize a single index /// </summary> /// <typeparam name="TKey">The type of the key</typeparam> /// <typeparam name="TIndex">The type of the index</typeparam> /// <param name="type">The type of the parent table</param> /// <param name="indexName">The name of the index</param> /// <param name="indexMap">The index map</param> public override async Task SerializeIndexAsync <TKey, TIndex>(Type type, string indexName, Dictionary <TKey, TIndex> indexMap) { var indexPath = _pathProvider.GetIndexPath(_basePath, DatabaseInstanceName, type, this, indexName); var pathLock = PathLock.GetLock(type.FullName); using (await pathLock.LockAsync().ConfigureAwait(false)) { using (var indexFile = _fileHelper.GetWriter(indexPath)) { indexFile.Write(indexMap.Count); foreach (var index in indexMap) { DatabaseSerializer.Serialize(index.Key, indexFile); DatabaseSerializer.Serialize(index.Value, indexFile); } } } }
/// <summary> /// Deserialize the keys /// </summary> /// <param name="type">Type of the parent table</param> /// <param name="keyType">Type of the key</param> /// <param name="dictionary">Empty dictionary</param> /// <returns>The key list</returns> public override async Task <IDictionary> DeserializeKeysAsync(Type type, Type keyType, IDictionary dictionary) { var keyPath = _pathProvider.GetKeysPath(_basePath, DatabaseInstanceName, type, this); if (_fileHelper.FileExists(keyPath)) { var pathLock = PathLock.GetLock(type.FullName); using (await pathLock.LockAsync().ConfigureAwait(false)) { using (var keyFile = _fileHelper.GetReader(keyPath)) { var count = keyFile.ReadInt32(); for (var x = 0; x < count; x++) { dictionary.Add(DatabaseSerializer.Deserialize(keyType, keyFile), keyFile.ReadInt32()); } } } } return(dictionary); }