forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretainedmessagesdb.h
66 lines (54 loc) · 1.48 KB
/
retainedmessagesdb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef RETAINEDMESSAGESDB_H
#define RETAINEDMESSAGESDB_H
#include "persistencefile.h"
#include "retainedmessage.h"
#define MAGIC_STRING_V1 "FlashMQRetainedDBv1"
#define MAGIC_STRING_V2 "FlashMQRetainedDBv2"
#define MAGIC_STRING_V3 "FlashMQRetainedDBv3"
#define MAGIC_STRING_V4 "FlashMQRetainedDBv4"
#define RESERVED_SPACE_RETAINED_DB_V2 64
/**
* @brief The RetainedMessagesDB class saves and loads the retained messages.
*
* The DB looks like, from the top:
*
* MAGIC_STRING_LENGH bytes file header
* HASH_SIZE SHA512
* [MESSAGES]
*
* Each message has a row header, which is 8 bytes. See writeRowHeader().
*
*/
class RetainedMessagesDB : private PersistenceFile
{
enum class ReadVersion
{
unknown,
v1,
v2,
v3,
v4
};
struct RowHeader
{
uint32_t topicLen = 0;
uint32_t payloadLen = 0;
};
ReadVersion readVersion = ReadVersion::unknown;
std::list<RetainedMessage> readDataV3V4();
public:
RetainedMessagesDB(const std::string &filePath);
void openWrite();
void openRead();
void closeFile();
void saveData(const std::vector<RetainedMessage> &messages);
std::list<RetainedMessage> readData();
};
#endif // RETAINEDMESSAGESDB_H