class="hide-preCode-box">

- 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
4.1.3 SetValueToFile\DeleteValueFromFile存入\删除键值
函数SetValueToFile同于把键值存入文件,函数DeleteValueFromFile则用于删除键值。⑴处根据键名获取存放值的文件路径keyPath,⑵处打开文件,然后写入键名对应的值。在函数DeleteValueFromFile中,⑶处先组装路径,然后删除文件。
static int SetValueToFile(const char* dataPath, const char* key, const char* value)
{
char* keyPath = (char *)malloc(PATH_MAX + 1);
if (keyPath == NULL) {
return EC_FAILURE;
}
⑴ if (GetResolvedPath(dataPath, key, keyPath, PATH_MAX + 1) != EC_SUCCESS) {
free(keyPath);
return EC_FAILURE;
}
⑵ int fd = open(keyPath, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
free(keyPath);
keyPath = NULL;
if (fd < 0) {
return EC_FAILURE;
}
int ret = write(fd, value, strlen(value));
close(fd);
fd = -1;
return (ret < 0) ? EC_FAILURE : EC_SUCCESS;
}
static int DeleteValueFromFile(const char* dataPath, const char* key)
{
char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
if (keyPath == NULL) {
return EC_FAILURE;
}
⑶ if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s/%s/%s", dataPath, KVSTORE_PATH, key) < 0) {
free(keyPath);
return EC_FAILURE;
}
int ret = unlink(keyPath);
free(keyPath);
return ret;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 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
4.1.4 InitKv创建kvstore目录
函数InitKv确保保存键值时,kvstore目录被创建,用于存放键值文件。⑴处组装kvstore目录,⑵处使用F_OK参数判断目录是否存在,如果存在返回EC_SUCCESS。否则执行⑶创建kvstore目录。
static int InitKv(const char* dataPath)
{
if (dataPath == NULL) {
return EC_FAILURE;
}
char* kvPath = (char *)malloc(MAX_KEY_PATH + 1);
if (kvPath == NULL) {
return EC_FAILURE;
}
⑴ if (sprintf_s(kvPath, MAX_KEY_PATH + 1, "%s/%s", dataPath, KVSTORE_PATH) < 0) {
free(kvPath);
return EC_FAILURE;
}
⑵ if (access(kvPath, F_OK) == F_OK) {
free(kvPath);
return EC_SUCCESS;
}
⑶ if (mkdir(kvPath, S_IRUSR | S_IWUSR | S_IXUSR) != F_OK) {
free(kvPath);
return EC_FAILURE;
}
free(kvPath);
return EC_SUCCESS;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
4.1.5 GetCurrentItem获取当前的键值对数目
函数GetCurrentItem用于获取当前的键值对数目。首先,组装目录路径"XXX/kvstore",然后执行⑴打开目录,然后读取目录项。⑵循环每一个目录项,判断键值对的数量。⑶处组装kvstore目录下每一个键的文件路径,然后获取每个文件的状态信息。⑷如果文件是常规普通文件,则键值对数量加1。然后读取kvstore目录下的下一个目录项,依次循环。
static int GetCurrentItem(const char* dataPath)
{
char kvPath[MAX_KEY_PATH + 1] = {0};
if (sprintf_s(kvPath, MAX_KEY_PATH + 1, "%s/%s", dataPath, KVSTORE_PATH) < 0) {
return EC_FAILURE;
}
⑴ DIR* fileDir = opendir(kvPath);
if (fileDir == NULL) {
return EC_FAILURE;
}
struct dirent* dir = readdir(fileDir);
int sum = 0;
⑵ while (dir != NULL) {
char fullPath[MAX_KEY_PATH + 1] = {0};
struct stat info = {0};
⑶ if (sprintf_s(fullPath, MAX_KEY_PATH + 1, "%s/%s", kvPath, dir->d_name) < 0) {
closedir(fileDir);
return EC_FAILURE;
}
if (stat(fullPath, &info) != 0) {
closedir(fileDir);
return EC_FAILURE;
}
⑷ if (S_ISREG(info.st_mode)) {
sum++;
}
dir = readdir(fileDir);
}
closedir(fileDir);
return sum;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 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
4.1.6 NewItem判断是否新键值对
函数NewItem可以用于判断是否新的键值对。⑴处获取键名对应的文件路径,⑵处判断文件是否存在,存在则返回FALSE;不存在键值对则返回TRUE。
static boolean NewItem(const char* dataPath, const char* key)
{
char* keyPath = (char *)malloc(MAX_KEY_PATH + 1);
if (keyPath == NULL) {
return FALSE;
}
⑴ if (sprintf_s(keyPath, MAX_KEY_PATH + 1, "%s/%s/%s", dataPath, KVSTORE_PATH, key) < 0) {
free(keyPath);
return FALSE;
}
⑵ if (access(keyPath, F_OK) == F_OK) {
free(keyPath);
return FALSE;
}
free(keyPath);
return TRUE;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
4.2 读取键值UtilsGetValue
函数UtilsSetValue用于读取键名对应的值,第一个参数为输入参数键名,第二个参数为输出参数键名对应的值,第三个参数为值的字符串长度。⑴处获取键值对所在的路径,注意互斥锁的使用。如果支持键值缓存,则执行⑵尝试从缓存中读取。缓存中不能读取时,继续执行⑶从文件中读取。如果读取成功,则执行⑷,加入缓存中,注意第三个参数为FALSE。读取时,会把读取到的键值对,放到缓存的键值对链表的头部,但不删除之前的键值对数据。
int UtilsGetValue(const char* key, char* value, unsigned int len)
{
if (!IsValidKey(key) || (value == NULL) || (len > MAX_GET_VALUE_LEN)) {
return EC_INVALID;
}
pthread_mutex_lock(&g_kvGlobalMutex);
⑴ const char* dataPath = g_dataPath;
if (dataPath == NULL) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
#ifdef FEATURE_KV_CACHE
⑵ if (GetValueByCache(key, value, len) == EC_SUCCESS) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_SUCCESS;
}
#endif
⑶ int ret = GetValueByFile(dataPath, key, value, len);
if (ret < 0) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
#ifdef FEATURE_KV_CACHE
⑷ AddKVCache(key, value, FALSE);
#endif
pthread_mutex_unlock(&g_kvGlobalMutex);
return ret;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 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
4.3 设置键值UtilsGetValue
函数UtilsSetValue用于保存一对键值,⑴处确保kvstore目录存在,不存在则创建。⑵处用于获取kvstore目录下键值对的数目。g_getKvSum默认为FALSE,只需要获取一次即可,键值对数目保存在全局变量g_kvSum。⑶处判断是否新的键值对,如果键值对数目超过缓存允许的最大数,并且需要设置的是新的缓存则返回EC_FAILURE。⑷处把键值对保存到文件中,如果支持缓存,还需要存入缓存中。注意AddKVCache存入缓存的第三方参数为TRUE,会先删除之前同一个键名对应的键值对。⑸处如果是新的键值对,键值对数目需要加1。
int UtilsSetValue(const char* key, const char* value)
{
if (!IsValidKey(key) || !IsValidValue(value, MAX_VALUE_LEN)) {
return EC_INVALID;
}
pthread_mutex_lock(&g_kvGlobalMutex);
const char* dataPath = g_dataPath;
⑴ int ret = InitKv(dataPath);
if (ret != EC_SUCCESS) {
g_getKvSum = FALSE;
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
⑵ if (!g_getKvSum) {
g_kvSum = GetCurrentItem(dataPath);
if (g_kvSum < 0) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
g_getKvSum = TRUE;
}
⑶ boolean newItem = NewItem(dataPath, key);
if ((g_kvSum >= MAX_KV_SUM) && newItem) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
⑷ ret = SetValueToFile(dataPath, key, value);
if (ret == EC_SUCCESS) {
#ifdef FEATURE_KV_CACHE
AddKVCache(key, value, TRUE);
#endif
if (newItem) {
⑸ g_kvSum++;
}
}
pthread_mutex_unlock(&g_kvGlobalMutex);
return ret;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 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
4.4 删除键值UtilsDeleteValue
函数UtilsDeleteValue用于删除一对键值。⑴处如果支持键值缓存,则首先尝试从缓存中删除键值对。⑵处从文件中删除键值,如果删除超过,键值对数目减1。
int UtilsDeleteValue(const char* key)
{
if (!IsValidKey(key)) {
return EC_INVALID;
}
pthread_mutex_lock(&g_kvGlobalMutex);
const char* dataPath = g_dataPath;
if (dataPath == NULL) {
pthread_mutex_unlock(&g_kvGlobalMutex);
return EC_FAILURE;
}
#ifdef FEATURE_KV_CACHE
⑴ DeleteKVCache(key);
#endif
⑵ int ret = DeleteValueFromFile(dataPath, key);
if (ret == EC_SUCCESS) {
g_kvSum--;
}
pthread_mutex_unlock(&g_kvGlobalMutex);
return ret;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4.5 清除键值缓存ClearKVCache和设置缓存路径UtilsSetEnv
函数ClearKVCache用于清除缓存,直接调用接口ClearKVCacheInner完成。函数UtilsSetEnv用于设置键值对的保存路径,维护在全局变量g_dataPath里。
#ifdef FEATURE_KV_CACHE
int ClearKVCache(void)
{
pthread_mutex_lock(&g_kvGlobalMutex);
int ret = ClearKVCacheInner();
pthread_mutex_unlock(&g_kvGlobalMutex);
return ret;
}
#endif
int UtilsSetEnv(const char* path)
{
if (path == NULL) {
return EC_FAILURE;
}
pthread_mutex_lock(&g_kvGlobalMutex);
int ret = strcpy_s(g_dataPath, MAX_KEY_PATH + 1, path);
pthread_mutex_unlock(&g_kvGlobalMutex);
return (ret != EOK) ? EC_FAILURE : EC_SUCCESS;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
如果大家想更加深入的学习 OpenHarmony 开发的内容,不妨可以参考以下相关学习文档进行学习,助你快速提升自己:

- 搭建开发环境
- Windows 开发环境的搭建
- Ubuntu 开发环境搭建
- Linux 与 Windows 之间的文件共享
- ……

- 构建子系统
- 启动流程
- 子系统
- 分布式任务调度子系统
- 分布式通信子系统
- 驱动子系统
- ……



写在最后
- 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
- 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
- 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
- 想要获取更多完整鸿蒙最新学习资源,请移步前往小编:
https://qr21.cn/FV7h05

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/maniuT/article/details/139780272","extend1":"pc","ab":"new"}">>
id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type2" id="blog_extension">
class="extension_official" data-report-click="{"spm":"1001.2101.3001.6471"}" data-report-view="{"spm":"1001.2101.3001.6471"}">
class="blog_extension_card_left">
class="blog_extension_card_cont">
鸿蒙开发学习资料领取!!!
class="blog_extension_card_cont_r">
微信名片
评论记录:
回复评论: