CatalogueConfigStorage.vala 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * CatalogueConfigStorage - Low-level storage for catalogue configuration
  3. *
  4. * Handles the 'catcfg:' prefix for storing catalogue configuration.
  5. *
  6. * Key format: catcfg:<path>
  7. * Value: Serialized (string type_label, string expression)
  8. *
  9. * @version 0.1
  10. * @since 0.1
  11. */
  12. namespace Implexus.Storage.LowLevel {
  13. /**
  14. * Configuration data for a Catalogue entity.
  15. */
  16. public class CatalogueConfig : Object {
  17. /**
  18. * The type label for documents to catalogue.
  19. */
  20. public string type_label { get; set; }
  21. /**
  22. * The expression used to extract the grouping key.
  23. */
  24. public string expression { get; set; }
  25. /**
  26. * Creates a new CatalogueConfig.
  27. *
  28. * @param type_label The type label for documents
  29. * @param expression The expression to extract the grouping key
  30. */
  31. public CatalogueConfig(string type_label, string expression) {
  32. this.type_label = type_label;
  33. this.expression = expression;
  34. }
  35. }
  36. /**
  37. * Low-level storage for catalogue configuration.
  38. *
  39. * This class provides type-safe operations for storing and retrieving
  40. * catalogue configuration using the 'catcfg:' key prefix.
  41. */
  42. public class CatalogueConfigStorage : Object {
  43. /**
  44. * Key prefix for catalogue config entries.
  45. */
  46. private const string PREFIX = "catcfg:";
  47. /**
  48. * The underlying Dbm storage.
  49. */
  50. private Dbm _dbm;
  51. /**
  52. * Creates a new CatalogueConfigStorage with the given Dbm backend.
  53. *
  54. * @param dbm The Dbm backend to use for storage
  55. */
  56. public CatalogueConfigStorage(Dbm dbm) {
  57. _dbm = dbm;
  58. }
  59. /**
  60. * Stores catalogue configuration.
  61. *
  62. * @param path The catalogue path
  63. * @param type_label The type label for documents to catalogue
  64. * @param expression The expression to extract the grouping key
  65. * @throws StorageError if the operation fails
  66. */
  67. public void store(Core.EntityPath path, string type_label, string expression) throws StorageError {
  68. string key = PREFIX + path.to_string();
  69. var writer = new ElementWriter();
  70. writer.write_element(new Invercargill.NativeElement<string>(type_label));
  71. writer.write_element(new Invercargill.NativeElement<string>(expression));
  72. _dbm.set(key, writer.to_binary_data());
  73. }
  74. /**
  75. * Loads catalogue configuration.
  76. *
  77. * @param path The catalogue path
  78. * @return The configuration, or null if not found
  79. * @throws StorageError if the data is corrupt
  80. */
  81. public CatalogueConfig? load(Core.EntityPath path) throws StorageError {
  82. string key = PREFIX + path.to_string();
  83. var data = _dbm.get(key);
  84. if (data == null) {
  85. return null;
  86. }
  87. var reader = new ElementReader((!) data);
  88. try {
  89. var label_element = reader.read_element();
  90. var expr_element = reader.read_element();
  91. if (label_element.is_null() || expr_element.is_null()) {
  92. return null;
  93. }
  94. string type_label = label_element.as<string>();
  95. string expression = expr_element.as<string>();
  96. return new CatalogueConfig(type_label, expression);
  97. } catch (Invercargill.ElementError e) {
  98. throw new StorageError.CORRUPT_DATA("Failed to read catalogue config: %s".printf(e.message));
  99. }
  100. }
  101. /**
  102. * Deletes catalogue configuration.
  103. *
  104. * @param path The catalogue path
  105. * @throws StorageError if the operation fails
  106. */
  107. public void delete(Core.EntityPath path) throws StorageError {
  108. string key = PREFIX + path.to_string();
  109. try {
  110. _dbm.delete(key);
  111. } catch (StorageError e) {
  112. // Key doesn't exist, that's fine
  113. }
  114. }
  115. }
  116. } // namespace Implexus.Storage.LowLevel