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

mendersoftware / mender / 1022567176

02 Oct 2023 07:50AM UTC coverage: 80.127% (+2.5%) from 77.645%
1022567176

push

gitlab-ci

kacf
chore: Centralize selection of `std::filesystem` library.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

6447 of 8046 relevant lines covered (80.13%)

9912.21 hits per line

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

94.29
/common/key_value_database/in_memory/in_memory.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.hpp>
16
#include <common/key_value_database_in_memory.hpp>
17

18
#include <string>
19

20
namespace mender {
21
namespace common {
22
namespace key_value_database {
23

24
using namespace std;
25

26
namespace expected = mender::common::expected;
27

28
class InMemoryTransaction : public Transaction {
29
public:
30
        InMemoryTransaction(KeyValueDatabaseInMemory &db, bool read_only);
31

32
        ExpectedBytes Read(const string &key) override;
33
        Error Write(const string &key, const vector<uint8_t> &value) override;
34
        Error Remove(const string &key) override;
35

36
private:
37
        KeyValueDatabaseInMemory &db_;
38
        bool read_only_;
39
};
40

41
InMemoryTransaction::InMemoryTransaction(KeyValueDatabaseInMemory &db, bool read_only) :
18✔
42
        db_ {db},
43
        read_only_ {read_only} {
18✔
44
}
×
45

46
ExpectedBytes InMemoryTransaction::Read(const string &key) {
15✔
47
        auto value = db_.map_.find(key);
15✔
48
        if (value != db_.map_.end()) {
15✔
49
                return ExpectedBytes(value->second);
9✔
50
        } else {
51
                return expected::unexpected(
6✔
52
                        MakeError(KeyError, "Key " + key + " not found in memory database"));
18✔
53
        }
54
}
55

56
Error InMemoryTransaction::Write(const string &key, const vector<uint8_t> &value) {
9✔
57
        AssertOrReturnError(!read_only_);
9✔
58
        db_.map_[key] = value;
9✔
59
        return error::NoError;
9✔
60
}
61

62
Error InMemoryTransaction::Remove(const string &key) {
1✔
63
        AssertOrReturnError(!read_only_);
1✔
64
        db_.map_.erase(key);
1✔
65
        return error::NoError;
1✔
66
}
67

68
ExpectedBytes KeyValueDatabaseInMemory::Read(const string &key) {
7✔
69
        ExpectedBytes ret {vector<uint8_t>()};
7✔
70
        auto err = ReadTransaction([&key, &ret](Transaction &txn) -> Error {
7✔
71
                ret = txn.Read(key);
7✔
72
                return error::NoError;
7✔
73
        });
7✔
74
        if (error::NoError != err) {
7✔
75
                return expected::unexpected(err);
×
76
        } else {
77
                return ret;
78
        }
79
}
80

81
Error KeyValueDatabaseInMemory::Write(const string &key, const vector<uint8_t> &value) {
5✔
82
        return WriteTransaction(
83
                [&key, &value](Transaction &txn) -> Error { return txn.Write(key, value); });
15✔
84
}
85

86
Error KeyValueDatabaseInMemory::Remove(const string &key) {
1✔
87
        return WriteTransaction([&key](Transaction &txn) -> Error { return txn.Remove(key); });
3✔
88
}
89

90
Error KeyValueDatabaseInMemory::WriteTransaction(function<Error(Transaction &)> txnFunc) {
9✔
91
        InMemoryTransaction txn(*this, false);
92
        // Simple, but inefficient rollback support.
93
        auto backup_map = this->map_;
94
        auto error = txnFunc(txn);
9✔
95
        if (error::NoError != error) {
9✔
96
                this->map_ = backup_map;
97
        }
98
        return error;
9✔
99
}
100

101
Error KeyValueDatabaseInMemory::ReadTransaction(function<Error(Transaction &)> txnFunc) {
9✔
102
        InMemoryTransaction txn(*this, true);
103
        return txnFunc(txn);
9✔
104
}
105

106
} // namespace key_value_database
107
} // namespace common
108
} // 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

© 2025 Coveralls, Inc