serialized_storage.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef SERIALIZED_STORAGE_H
  2. #define SERIALIZED_STORAGE_H
  3. #include "../file_storage/file_storage.h"
  4. namespace binom {
  5. enum class FileType {
  6. undefined_file = 0,
  7. dynamic_storage = 1,
  8. serialized_storage = 2,
  9. };
  10. FileType checkFileType(std::string_view file_name);
  11. class SerializedStorage {
  12. FileIO file;
  13. #pragma pack(push, 1)
  14. struct Header {
  15. enum class VersionDifference {
  16. identical = 0,
  17. file_type = 1,
  18. major = 2,
  19. minor = 3,
  20. };
  21. static constexpr DBVersion current{"BinOM.SS", 0, 1}; // Serialized_Storage
  22. DBVersion version = current;
  23. VersionDifference checkFileVersion() {
  24. if(memcmp(version.file_type, current.file_type, sizeof(current.file_type)))
  25. return VersionDifference::file_type;
  26. if(version.major != current.major)
  27. return VersionDifference::major;
  28. if(version.minor != current.minor)
  29. return VersionDifference::minor;
  30. return VersionDifference::identical;
  31. }
  32. };
  33. #pragma pack(pop)
  34. Header file_header;
  35. public:
  36. SerializedStorage(std::string_view file_name)
  37. : file(file_name) {
  38. if(isEmpty()) {
  39. file.write(0, file_header);
  40. } else {
  41. file.read(file_header, 0);
  42. switch (file_header.checkFileVersion()) {
  43. case Header::VersionDifference::identical: break;
  44. case Header::VersionDifference::file_type:
  45. throw Exception(ErrCode::binomdb_invalid_file, "Invalid file type");
  46. case Header::VersionDifference::major:
  47. break;
  48. case Header::VersionDifference::minor:
  49. break;
  50. }
  51. }
  52. }
  53. inline bool isExist() const {return file.isExist();}
  54. inline bool isEmpty() const {return file.isEmpty();}
  55. inline fs::path getPath() const {return file.getPath();}
  56. inline ui64 getSize() const {return file.getSize();}
  57. Variable read();
  58. SerializedStorage& write(Variable var);
  59. inline SerializedStorage& operator=(Variable var) {return write(std::move(var));}
  60. inline operator Variable() {return read();}
  61. };
  62. }
  63. #endif // SERIALIZED_STORAGE_H