diff options
Diffstat (limited to 'components/wisp_relay/storage_engine.h')
| -rw-r--r-- | components/wisp_relay/storage_engine.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/components/wisp_relay/storage_engine.h b/components/wisp_relay/storage_engine.h new file mode 100644 index 0000000..4e17113 --- /dev/null +++ b/components/wisp_relay/storage_engine.h | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | #ifndef STORAGE_ENGINE_H | ||
| 2 | #define STORAGE_ENGINE_H | ||
| 3 | |||
| 4 | #include <stdbool.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "esp_err.h" | ||
| 7 | #include "freertos/FreeRTOS.h" | ||
| 8 | #include "freertos/semphr.h" | ||
| 9 | #include "freertos/task.h" | ||
| 10 | |||
| 11 | #define STORAGE_MAX_EVENTS 5000 | ||
| 12 | #define STORAGE_MAX_EVENT_SIZE 8192 | ||
| 13 | #define STORAGE_INDEX_ENTRIES 5000 | ||
| 14 | #define STORAGE_PARTITION_LABEL "relay_store" | ||
| 15 | |||
| 16 | typedef enum { | ||
| 17 | STORAGE_OK = 0, | ||
| 18 | STORAGE_ERR_NOT_INITIALIZED, | ||
| 19 | STORAGE_ERR_FULL, | ||
| 20 | STORAGE_ERR_DUPLICATE, | ||
| 21 | STORAGE_ERR_NOT_FOUND, | ||
| 22 | STORAGE_ERR_IO, | ||
| 23 | STORAGE_ERR_NO_MEM, | ||
| 24 | STORAGE_ERR_SERIALIZE | ||
| 25 | } storage_error_t; | ||
| 26 | |||
| 27 | #define STORAGE_FLAG_DELETED 0x01 | ||
| 28 | |||
| 29 | typedef struct __attribute__((packed)) { | ||
| 30 | uint8_t event_id[32]; | ||
| 31 | uint32_t created_at; | ||
| 32 | uint32_t expires_at; | ||
| 33 | uint32_t file_index; | ||
| 34 | uint16_t kind; | ||
| 35 | uint8_t pubkey_prefix[4]; | ||
| 36 | uint8_t flags; | ||
| 37 | uint8_t reserved; | ||
| 38 | } storage_index_entry_t; | ||
| 39 | |||
| 40 | typedef struct { | ||
| 41 | uint32_t total_events; | ||
| 42 | uint32_t total_bytes; | ||
| 43 | uint32_t free_bytes; | ||
| 44 | uint32_t oldest_event_ts; | ||
| 45 | uint32_t newest_event_ts; | ||
| 46 | } storage_stats_t; | ||
| 47 | |||
| 48 | typedef struct storage_engine { | ||
| 49 | storage_index_entry_t *index; | ||
| 50 | uint16_t index_count; | ||
| 51 | uint16_t max_index_entries; | ||
| 52 | uint32_t next_file_index; | ||
| 53 | SemaphoreHandle_t lock; | ||
| 54 | TaskHandle_t cleanup_task; | ||
| 55 | bool initialized; | ||
| 56 | bool cleanup_stop; | ||
| 57 | char mount_point[16]; | ||
| 58 | uint32_t default_ttl_sec; | ||
| 59 | } storage_engine_t; | ||
| 60 | |||
| 61 | esp_err_t storage_init(storage_engine_t *engine, uint32_t default_ttl_sec); | ||
| 62 | void storage_destroy(storage_engine_t *engine); | ||
| 63 | |||
| 64 | storage_error_t storage_save_event_json(storage_engine_t *engine, | ||
| 65 | const char *event_json, | ||
| 66 | size_t event_json_len); | ||
| 67 | |||
| 68 | storage_error_t storage_query_events_json(storage_engine_t *engine, | ||
| 69 | int kind, | ||
| 70 | const char *author_hex, | ||
| 71 | int limit, | ||
| 72 | char ***results, | ||
| 73 | uint16_t *count); | ||
| 74 | |||
| 75 | void storage_free_query_results(char **results, uint16_t count); | ||
| 76 | |||
| 77 | bool storage_event_exists(storage_engine_t *engine, const uint8_t event_id[32]); | ||
| 78 | |||
| 79 | storage_error_t storage_delete_event(storage_engine_t *engine, const uint8_t event_id[32]); | ||
| 80 | |||
| 81 | int storage_purge_expired(storage_engine_t *engine); | ||
| 82 | int storage_compact_index(storage_engine_t *engine); | ||
| 83 | |||
| 84 | void storage_get_stats(storage_engine_t *engine, storage_stats_t *stats); | ||
| 85 | |||
| 86 | esp_err_t storage_start_cleanup_task(storage_engine_t *engine); | ||
| 87 | |||
| 88 | #endif | ||