Apollo 10.0
自动驾驶开放平台
apollo::common::KVDB类 参考

Lightweight key-value database to store system-wide parameters. 更多...

#include <kv_db.h>

apollo::common::KVDB 的协作图:

静态 Public 成员函数

static bool Put (std::string_view key, std::string_view value)
 Store {key, value} to DB.
 
static bool Delete (std::string_view key)
 Delete a key.
 
static std::optional< std::string > Get (std::string_view key)
 Get value of a key.
 
static std::vector< std::pair< std::string, std::string > > GetWithStart (std::string_view start)
 Get the tuple whose key starts with start.
 

详细描述

Lightweight key-value database to store system-wide parameters.

We prefer keys like "apollo:data:commit_id".

在文件 kv_db.h39 行定义.

成员函数说明

◆ Delete()

bool apollo::common::KVDB::Delete ( std::string_view  key)
static

Delete a key.

返回
Success or not.

在文件 kv_db.cc101 行定义.

101 {
102 SqliteWraper sqlite;
103 return sqlite.SQL(
104 absl::StrCat("DELETE FROM key_value WHERE key='", key, "';"));
105}

◆ Get()

std::optional< std::string > apollo::common::KVDB::Get ( std::string_view  key)
static

Get value of a key.

返回
An optional value. Use has_value() to check if there is non-empty value. Use value() to get real value. Use value_or("") to get existing value or fallback to default.

在文件 kv_db.cc107 行定义.

107 {
108 SqliteWraper sqlite;
109 std::string value;
110 const bool ret = sqlite.SQL(
111 absl::StrCat("SELECT value FROM key_value WHERE key='", key, "';"),
112 &value);
113 if (ret && !value.empty()) {
114 return value;
115 }
116 return {};
117}

◆ GetWithStart()

std::vector< std::pair< std::string, std::string > > apollo::common::KVDB::GetWithStart ( std::string_view  start)
static

Get the tuple whose key starts with start.

返回
An optional value. Use has_value() to check if there is non-empty value. Use value() to get real value. Use value_or("") to get existing value or fallback to default.

在文件 kv_db.cc119 行定义.

120 {
121 SqliteWraper sqlite;
122 std::vector<std::pair<std::string, std::string>> results;
123
124 // Use the SQL LIKE operator to match any key that starts with the 'start'
125 // parameter.
126 std::string sql = absl::StrCat(
127 "SELECT key, value FROM key_value WHERE key LIKE '", start, "%';");
128
129 // Modify the callback function to handle multiple rows.
130 auto callback = [](void *data, int argc, char **argv,
131 char **col_name) -> int {
132 auto *vec =
133 static_cast<std::vector<std::pair<std::string, std::string>> *>(data);
134 if (argc == 2 && argv[0] && argv[1]) {
135 vec->emplace_back(argv[0], argv[1]);
136 }
137 return 0;
138 };
139
140 char *error = nullptr;
141 if (sqlite3_exec(sqlite.GetDB(), sql.c_str(), callback, &results, &error) !=
142 SQLITE_OK) {
143 AERROR << "Failed to execute SQL: " << error;
144 sqlite3_free(error);
145 }
146
147 return results;
148}
#define AERROR
Definition log.h:44

◆ Put()

bool apollo::common::KVDB::Put ( std::string_view  key,
std::string_view  value 
)
static

Store {key, value} to DB.

返回
Success or not.

在文件 kv_db.cc94 行定义.

94 {
95 SqliteWraper sqlite;
96 return sqlite.SQL(
97 absl::StrCat("INSERT OR REPLACE INTO key_value (key, value) VALUES ('",
98 key, "', '", value, "');"));
99}

该类的文档由以下文件生成: