Metadata.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.IO;
  3. using Best.HTTP.Shared.Databases.Utils;
  4. namespace Best.HTTP.Shared.Databases
  5. {
  6. public abstract class Metadata
  7. {
  8. public int Index;
  9. public int FilePosition;
  10. public int Length;
  11. public bool IsDeleted => this.FilePosition == -1 && this.Length == -1;
  12. public void MarkForDelete()
  13. {
  14. this.FilePosition = -1;
  15. this.Length = -1;
  16. }
  17. public virtual void SaveTo(Stream stream)
  18. {
  19. if (this.IsDeleted)
  20. throw new Exception($"Trying to save a deleted metadata({this.ToString()})!");
  21. stream.EncodeUnsignedVariableByteInteger((uint)this.FilePosition);
  22. stream.EncodeUnsignedVariableByteInteger((uint)this.Length);
  23. }
  24. public virtual void LoadFrom(Stream stream)
  25. {
  26. this.FilePosition = (int)stream.DecodeUnsignedVariableByteInteger();
  27. this.Length = (int)stream.DecodeUnsignedVariableByteInteger();
  28. }
  29. public override string ToString()
  30. {
  31. return $"[Metadata Idx: {Index}, Pos: {FilePosition}, Length: {Length}, IsDeleted: {IsDeleted}]";
  32. }
  33. }
  34. }