public Result ReadDatabaseFromBuffer(ReadOnlySpan <byte> 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 <byte> keyBytes, out ReadOnlySpan <byte> valueBytes); if (rc.IsFailure()) { return(rc); } Debug.Assert(keyBytes.Length == Unsafe.SizeOf <TKey>()); var key = new TKey(); keyBytes.CopyTo(SpanHelpers.AsByteSpan(ref key)); byte[] value = valueBytes.ToArray(); KvDict.Add(key, value); } 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 WriteDatabaseToBuffer(Span <byte> output) { var writer = new ImkvdbWriter(output); writer.WriteHeader(KvDict.Count); foreach (KeyValuePair <TKey, byte[]> entry in KvDict.OrderBy(x => x.Key)) { TKey key = entry.Key; writer.WriteEntry(SpanHelpers.AsByteSpan(ref key), entry.Value); } return(Result.Success); }
public Dictionary <TKey, byte[]> .Enumerator GetEnumerator() { return(KvDict.GetEnumerator()); }
public Result Delete(ref TKey key) { bool deleted = KvDict.Remove(key); return(deleted ? Result.Success : ResultKvdb.KeyNotFound.Log()); }
public List <(TKey key, byte[] value)> ToList() { return(KvDict.OrderBy(x => x.Key).Select(entry => (entry.Key, entry.Value)).ToList()); }