From d575415b0edd83dc51d5adf5aa0fc948b8718b18 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 3 Aug 2020 18:39:14 -0700 Subject: [PATCH] Remove the old KeyValueDatabase --- src/LibHac/Kvdb/IExportable.cs | 16 --- src/LibHac/Kvdb/ImkvdbHeader.cs | 24 ---- src/LibHac/Kvdb/ImkvdbReader.cs | 79 ----------- src/LibHac/Kvdb/ImkvdbWriter.cs | 61 --------- src/LibHac/Kvdb/KeyValueDatabase.cs | 199 ---------------------------- src/LibHac/Ncm/ContentMetaKey.cs | 3 +- 6 files changed, 1 insertion(+), 381 deletions(-) delete mode 100644 src/LibHac/Kvdb/IExportable.cs delete mode 100644 src/LibHac/Kvdb/ImkvdbHeader.cs delete mode 100644 src/LibHac/Kvdb/ImkvdbReader.cs delete mode 100644 src/LibHac/Kvdb/ImkvdbWriter.cs delete mode 100644 src/LibHac/Kvdb/KeyValueDatabase.cs diff --git a/src/LibHac/Kvdb/IExportable.cs b/src/LibHac/Kvdb/IExportable.cs deleted file mode 100644 index f72e3c18..00000000 --- a/src/LibHac/Kvdb/IExportable.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace LibHac.Kvdb -{ - public interface IExportable - { - int ExportSize { get; } - void ToBytes(Span output); - void FromBytes(ReadOnlySpan input); - - /// - /// Prevent further modification of this object. - /// - void Freeze(); - } -} diff --git a/src/LibHac/Kvdb/ImkvdbHeader.cs b/src/LibHac/Kvdb/ImkvdbHeader.cs deleted file mode 100644 index 41f3c392..00000000 --- a/src/LibHac/Kvdb/ImkvdbHeader.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Runtime.InteropServices; - -namespace LibHac.Kvdb -{ - [StructLayout(LayoutKind.Sequential, Size = 0xC)] - internal struct ImkvdbHeader - { - public const uint ExpectedMagic = 0x564B4D49; // IMKV - - public uint Magic; - public int Reserved; - public int EntryCount; - } - - [StructLayout(LayoutKind.Sequential, Size = 0xC)] - internal struct ImkvdbEntryHeader - { - public const uint ExpectedMagic = 0x4E454D49; // IMEN - - public uint Magic; - public int KeySize; - public int ValueSize; - } -} diff --git a/src/LibHac/Kvdb/ImkvdbReader.cs b/src/LibHac/Kvdb/ImkvdbReader.cs deleted file mode 100644 index 1956affb..00000000 --- a/src/LibHac/Kvdb/ImkvdbReader.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Runtime.CompilerServices; - -namespace LibHac.Kvdb -{ - public ref struct ImkvdbReader - { - private readonly ReadOnlySpan _data; - private int _position; - - public ImkvdbReader(ReadOnlySpan data) - { - _data = data; - _position = 0; - } - - public Result ReadHeader(out int entryCount) - { - entryCount = default; - - if (_position + Unsafe.SizeOf() > _data.Length) - return ResultKvdb.InvalidKeyValue.Log(); - - ref ImkvdbHeader header = ref Unsafe.As(ref Unsafe.AsRef(_data[_position])); - - if (header.Magic != ImkvdbHeader.ExpectedMagic) - { - return ResultKvdb.InvalidKeyValue.Log(); - } - - entryCount = header.EntryCount; - _position += Unsafe.SizeOf(); - - return Result.Success; - } - - public Result GetEntrySize(out int keySize, out int valueSize) - { - keySize = default; - valueSize = default; - - if (_position + Unsafe.SizeOf() > _data.Length) - return ResultKvdb.InvalidKeyValue.Log(); - - ref ImkvdbEntryHeader header = ref Unsafe.As(ref Unsafe.AsRef(_data[_position])); - - if (header.Magic != ImkvdbEntryHeader.ExpectedMagic) - { - return ResultKvdb.InvalidKeyValue.Log(); - } - - keySize = header.KeySize; - valueSize = header.ValueSize; - - return Result.Success; - } - - public Result ReadEntry(out ReadOnlySpan key, out ReadOnlySpan value) - { - key = default; - value = default; - - Result rc = GetEntrySize(out int keySize, out int valueSize); - if (rc.IsFailure()) return rc; - - _position += Unsafe.SizeOf(); - - if (_position + keySize + valueSize > _data.Length) - return ResultKvdb.InvalidKeyValue.Log(); - - key = _data.Slice(_position, keySize); - value = _data.Slice(_position + keySize, valueSize); - - _position += keySize + valueSize; - - return Result.Success; - } - } -} diff --git a/src/LibHac/Kvdb/ImkvdbWriter.cs b/src/LibHac/Kvdb/ImkvdbWriter.cs deleted file mode 100644 index 5e5815fb..00000000 --- a/src/LibHac/Kvdb/ImkvdbWriter.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Runtime.CompilerServices; - -namespace LibHac.Kvdb -{ - public ref struct ImkvdbWriter - { - private readonly Span _data; - private int _position; - - public ImkvdbWriter(Span data) - { - _data = data; - _position = 0; - } - - public void WriteHeader(int entryCount) - { - if (_position + Unsafe.SizeOf() > _data.Length) throw new InvalidOperationException(); - - ref ImkvdbHeader header = ref Unsafe.As(ref _data[_position]); - - header.Magic = ImkvdbHeader.ExpectedMagic; - header.Reserved = 0; - header.EntryCount = entryCount; - - _position += Unsafe.SizeOf(); - } - - public void WriteEntry(ReadOnlySpan key, ReadOnlySpan value) - { - WriteEntryHeader(key.Length, value.Length); - Write(key); - Write(value); - } - - private void WriteEntryHeader(int keySize, int valueSize) - { - if (_position + Unsafe.SizeOf() > _data.Length) throw new InvalidOperationException(); - - ref ImkvdbEntryHeader header = ref Unsafe.As(ref _data[_position]); - - header.Magic = ImkvdbEntryHeader.ExpectedMagic; - header.KeySize = keySize; - header.ValueSize = valueSize; - - _position += Unsafe.SizeOf(); - } - - private void Write(ReadOnlySpan value) - { - int valueSize = value.Length; - if (_position + valueSize > _data.Length) throw new InvalidOperationException(); - - Span dest = _data.Slice(_position, valueSize); - value.CopyTo(dest); - - _position += valueSize; - } - } -} diff --git a/src/LibHac/Kvdb/KeyValueDatabase.cs b/src/LibHac/Kvdb/KeyValueDatabase.cs deleted file mode 100644 index 1ea5680d..00000000 --- a/src/LibHac/Kvdb/KeyValueDatabase.cs +++ /dev/null @@ -1,199 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Runtime.CompilerServices; -using LibHac.Common; -using LibHac.Fs; - -namespace LibHac.Kvdb -{ - public class KeyValueDatabase where TKey : unmanaged, IComparable, IEquatable - { - private Dictionary KvDict { get; } = new Dictionary(); - - private FileSystemClient FsClient { get; } - private U8String FileName { get; } - - public int Count => KvDict.Count; - - public KeyValueDatabase() { } - - public KeyValueDatabase(FileSystemClient fsClient, U8Span fileName) - { - FsClient = fsClient; - FileName = fileName.ToU8String(); - } - - public Result Get(ref TKey key, Span valueBuffer) - { - Result rc = GetValue(ref key, out byte[] value); - if (rc.IsFailure()) return rc; - - int size = Math.Min(valueBuffer.Length, value.Length); - - value.AsSpan(0, size).CopyTo(valueBuffer); - return Result.Success; - } - - public Result GetValue(ref TKey key, out byte[] value) - { - if (!KvDict.TryGetValue(key, out value)) - { - return ResultKvdb.KeyNotFound.Log(); - } - - return Result.Success; - } - - public Result Set(ref TKey key, ReadOnlySpan value) - { - KvDict[key] = value.ToArray(); - - return Result.Success; - } - - public Result Delete(ref TKey key) - { - bool deleted = KvDict.Remove(key); - - return deleted ? Result.Success : ResultKvdb.KeyNotFound.Log(); - } - - public Dictionary.Enumerator GetEnumerator() - { - return KvDict.GetEnumerator(); - } - - public Result ReadDatabaseFromBuffer(ReadOnlySpan data) - { - KvDict.Clear(); - - var reader = new ImkvdbReader(data); - - Result rc = reader.ReadHeader(out int entryCount); - if (rc.IsFailure()) return rc; - - for (int i = 0; i < entryCount; i++) - { - rc = reader.ReadEntry(out ReadOnlySpan keyBytes, out ReadOnlySpan valueBytes); - if (rc.IsFailure()) return rc; - - Debug.Assert(keyBytes.Length == Unsafe.SizeOf()); - - var key = new TKey(); - keyBytes.CopyTo(SpanHelpers.AsByteSpan(ref key)); - - byte[] value = valueBytes.ToArray(); - - KvDict.Add(key, value); - } - - return Result.Success; - } - - public Result WriteDatabaseToBuffer(Span output) - { - var writer = new ImkvdbWriter(output); - - writer.WriteHeader(KvDict.Count); - - foreach (KeyValuePair entry in KvDict.OrderBy(x => x.Key)) - { - TKey key = entry.Key; - writer.WriteEntry(SpanHelpers.AsByteSpan(ref key), entry.Value); - } - - return Result.Success; - } - - public Result ReadDatabaseFromFile() - { - if (FsClient == null || FileName.IsNull()) - return ResultFs.PreconditionViolation.Log(); - - Result rc = ReadFile(out byte[] data); - - if (rc.IsFailure()) - { - return ResultFs.PathNotFound.Includes(rc) ? Result.Success : rc; - } - - return ReadDatabaseFromBuffer(data); - } - - public Result WriteDatabaseToFile() - { - if (FsClient == null || FileName.IsNull()) - return ResultFs.PreconditionViolation.Log(); - - var buffer = new byte[GetExportedSize()]; - - Result rc = WriteDatabaseToBuffer(buffer); - if (rc.IsFailure()) return rc; - - return WriteFile(buffer); - } - - public int GetExportedSize() - { - int size = Unsafe.SizeOf(); - - foreach (byte[] value in KvDict.Values) - { - size += Unsafe.SizeOf(); - size += Unsafe.SizeOf(); - size += value.Length; - } - - return size; - } - - public List<(TKey key, byte[] value)> ToList() - { - return KvDict.OrderBy(x => x.Key).Select(entry => (entry.Key, entry.Value)).ToList(); - } - - private Result ReadFile(out byte[] data) - { - Debug.Assert(FsClient != null); - Debug.Assert(!FileName.IsEmpty()); - - data = default; - - Result rc = FsClient.OpenFile(out FileHandle handle, FileName, OpenMode.Read); - if (rc.IsFailure()) return rc; - - rc = FsClient.GetFileSize(out long fileSize, handle); - - if (rc.IsSuccess()) - { - data = new byte[fileSize]; - - rc = FsClient.ReadFile(handle, 0, data); - } - - FsClient.CloseFile(handle); - return rc; - } - - private Result WriteFile(ReadOnlySpan data) - { - Debug.Assert(FsClient != null); - Debug.Assert(!FileName.IsEmpty()); - - FsClient.DeleteFile(FileName); - - Result rc = FsClient.CreateFile(FileName, data.Length); - if (rc.IsFailure()) return rc; - - rc = FsClient.OpenFile(out FileHandle handle, FileName, OpenMode.Write); - if (rc.IsFailure()) return rc; - - rc = FsClient.WriteFile(handle, 0, data, WriteOption.Flush); - FsClient.CloseFile(handle); - - return rc; - } - } -} diff --git a/src/LibHac/Ncm/ContentMetaKey.cs b/src/LibHac/Ncm/ContentMetaKey.cs index 4bf9e125..bcd562bf 100644 --- a/src/LibHac/Ncm/ContentMetaKey.cs +++ b/src/LibHac/Ncm/ContentMetaKey.cs @@ -1,10 +1,9 @@ using System; using System.Buffers.Binary; -using LibHac.Kvdb; namespace LibHac.Ncm { - public class ContentMetaKey : IComparable, IComparable, IEquatable, IExportable + public class ContentMetaKey : IComparable, IComparable, IEquatable { public ulong TitleId { get; private set; } public uint Version { get; private set; }