• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 2460319284

17 Apr 2026 11:02AM UTC coverage: 81.435% (-0.3%) from 81.695%
2460319284

push

gitlab-ci

web-flow
Merge pull request #1938 from michalkopczan/MEN-9075-mender-marks-download-as-imcomplete-when-update-module-returns-too-fast

chore: Document stream tree flow synchronization

9181 of 11274 relevant lines covered (81.44%)

20188.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.19
/src/common/key_value_database/platform/lmdb/lmdb.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <common/key_value_database_lmdb.hpp>
16

17
#include <filesystem>
18

19
#include <lmdbxx/lmdb++.h>
20

21
#include <common/common.hpp>
22
#include <common/log.hpp>
23

24
namespace mender {
25
namespace common {
26
namespace key_value_database {
27

28
namespace fs = std::filesystem;
29

30
namespace log = mender::common::log;
31

32
const string kBrokenSuffix {"-broken"};
33

34
class LmdbTransaction : public Transaction {
3,268✔
35
public:
36
        LmdbTransaction(lmdb::txn &txn, lmdb::dbi &dbi);
37

38
        expected::ExpectedBytes Read(const string &key) override;
39
        error::Error Write(const string &key, const vector<uint8_t> &value) override;
40
        error::Error Remove(const string &key) override;
41

42
private:
43
        lmdb::txn &txn_;
44
        lmdb::dbi &dbi_;
45
};
46

47
LmdbTransaction::LmdbTransaction(lmdb::txn &txn, lmdb::dbi &dbi) :
3,268✔
48
        txn_ {txn},
3,268✔
49
        dbi_ {dbi} {
3,268✔
50
}
×
51

52
expected::ExpectedBytes LmdbTransaction::Read(const string &key) {
1,977✔
53
        try {
54
                std::string_view value;
1,977✔
55
                bool exists = dbi_.get(txn_, key, value);
1,977✔
56
                if (!exists) {
1,977✔
57
                        return expected::unexpected(
1,166✔
58
                                MakeError(KeyError, "Key " + key + " not found in database"));
3,498✔
59
                }
60

61
                return common::ByteVectorFromString(value);
1,622✔
62
        } catch (std::runtime_error &e) {
×
63
                return expected::unexpected(MakeError(LmdbError, e.what()));
×
64
        }
×
65
}
66

67
error::Error LmdbTransaction::Write(const string &key, const vector<uint8_t> &value) {
1,486✔
68
        try {
69
                string_view data(reinterpret_cast<const char *>(value.data()), value.size());
70
                bool exists = dbi_.put(txn_, key, data);
1,486✔
71
                if (!exists) {
1,486✔
72
                        return MakeError(AlreadyExistsError, "Key " + key + " already exists");
×
73
                }
74

75
                return error::NoError;
1,486✔
76
        } catch (std::runtime_error &e) {
×
77
                return MakeError(LmdbError, e.what());
×
78
        }
×
79
}
80

81
error::Error LmdbTransaction::Remove(const string &key) {
1,941✔
82
        try {
83
                // We don't treat !exists as an error, just ignore return code.
84
                dbi_.del(txn_, key);
1,941✔
85

86
                return error::NoError;
1,941✔
87
        } catch (std::runtime_error &e) {
×
88
                return MakeError(LmdbError, e.what());
×
89
        }
×
90
}
91

92
KeyValueDatabaseLmdb::KeyValueDatabaseLmdb() {
1,092✔
93
}
1,092✔
94

95
KeyValueDatabaseLmdb::~KeyValueDatabaseLmdb() {
910✔
96
        Close();
455✔
97
}
1,365✔
98

99
error::Error KeyValueDatabaseLmdb::Open(const string &path) {
500✔
100
        return OpenInternal(path, true);
500✔
101
}
102

103
error::Error KeyValueDatabaseLmdb::OpenInternal(const string &path, bool try_recovery) {
501✔
104
        Close();
501✔
105

106
        env_ = make_unique<lmdb::env>(lmdb::env::create());
1,503✔
107

108
        try {
109
                env_->open(path.c_str(), MDB_NOSUBDIR, 0600);
501✔
110
        } catch (std::runtime_error &e) {
4✔
111
                auto err {MakeError(LmdbError, e.what()).WithContext("Opening LMDB database failed")};
8✔
112

113
                if (not try_recovery) {
4✔
114
                        env_.reset();
115
                        return err;
×
116
                }
117

118
                try {
119
                        if (not fs::exists(path)) {
4✔
120
                                env_.reset();
121
                                return err;
2✔
122
                        }
123

124
                        log::Warning(
2✔
125
                                "Failure to open database. Attempting to recover by resetting. The original database can be found at `"
126
                                + path + kBrokenSuffix + "`");
2✔
127

128
                        fs::rename(path, path + kBrokenSuffix);
3✔
129

130
                        auto err2 = OpenInternal(path, false);
1✔
131
                        if (err2 != error::NoError) {
1✔
132
                                return err.FollowedBy(err2);
×
133
                        }
134
                        return error::NoError;
1✔
135
                } catch (fs::filesystem_error &e) {
1✔
136
                        env_.reset();
137
                        return err.FollowedBy(error::Error(e.code().default_error_condition(), e.what())
2✔
138
                                                                          .WithContext("Opening LMDB database failed"));
2✔
139
                } catch (std::runtime_error &e) {
1✔
140
                        env_.reset();
141
                        return err.FollowedBy(error::MakeError(error::GenericError, e.what())
×
142
                                                                          .WithContext("Opening LMDB database failed"));
×
143
                }
×
144
        }
4✔
145

146
        return error::NoError;
497✔
147
}
148

149
void KeyValueDatabaseLmdb::Close() {
959✔
150
        env_.reset();
151
}
959✔
152

153
error::Error KeyValueDatabaseLmdb::WriteTransaction(function<error::Error(Transaction &)> txnFunc) {
2,588✔
154
        AssertOrReturnError(env_);
2,588✔
155

156
        try {
157
                lmdb::txn lmdb_txn = lmdb::txn::begin(*env_, nullptr, 0);
2,588✔
158
                lmdb::dbi lmdb_dbi = lmdb::dbi::open(lmdb_txn, nullptr, 0);
2,588✔
159
                LmdbTransaction txn(lmdb_txn, lmdb_dbi);
160
                auto error = txnFunc(txn);
2,588✔
161
                if (error::NoError != error) {
2,588✔
162
                        lmdb_txn.abort();
58✔
163
                } else {
164
                        lmdb_txn.commit();
2,530✔
165
                }
166
                return error;
2,588✔
167
        } catch (std::runtime_error &e) {
2,588✔
168
                return MakeError(LmdbError, e.what());
×
169
        }
×
170
}
171

172
error::Error KeyValueDatabaseLmdb::ReadTransaction(function<error::Error(Transaction &)> txnFunc) {
681✔
173
        AssertOrReturnError(env_);
682✔
174

175
        try {
176
                lmdb::txn lmdb_txn = lmdb::txn::begin(*env_, nullptr, MDB_RDONLY);
680✔
177
                lmdb::dbi lmdb_dbi = lmdb::dbi::open(lmdb_txn, nullptr, 0);
680✔
178
                LmdbTransaction txn(lmdb_txn, lmdb_dbi);
179
                return txnFunc(txn);
680✔
180
        } catch (std::runtime_error &e) {
680✔
181
                return MakeError(LmdbError, e.what());
×
182
        }
×
183
}
184

185
} // namespace key_value_database
186
} // namespace common
187
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc