實(shí)驗(yàn)內(nèi)容
本例程演示如何在小凌派-RK2206開(kāi)發(fā)板上使用OpenHarmony輕量級(jí)操作系統(tǒng)進(jìn)行KvStore(即分布式數(shù)據(jù)管理)數(shù)據(jù)讀寫(xiě)。
例程:
(1)創(chuàng)建兩個(gè)線程,一個(gè)負(fù)責(zé)寫(xiě)入KvStore存儲(chǔ),一個(gè)負(fù)責(zé)讀取KvStore存儲(chǔ);
(2)每1秒進(jìn)行1次讀寫(xiě)操作;
程序設(shè)計(jì)
在本章節(jié)中,我們將了解OpenHarmony KvStore存儲(chǔ)接口,如文件如何獲取數(shù)據(jù)、設(shè)置數(shù)據(jù)、刪除數(shù)據(jù)和清除緩存。
API分析
頭文件
//utils/native/lite/include/kv_store.h
UtilsGetValue()
intUtilsGetValue(constchar*key,char*value,unsignedintlen);
描述:
從文件系統(tǒng)或緩存中獲取與指定鍵匹配的值。
參數(shù):
名字 | 描述 |
---|---|
key | 鍵值 |
value | 獲取數(shù)據(jù) |
len | 數(shù)據(jù)長(zhǎng)度 |
返回值:
返回值 | 描述 |
---|---|
0 | 成功 |
其它 | 見(jiàn)utils/native/lite/include/ohos_errno.h |
UtilsSetValue()
intUtilsSetValue(constchar*key,constchar*value);
描述:
添加或更新與文件系統(tǒng)或緩存中的指定鍵匹配的值。
參數(shù):
名字 | 描述 |
---|---|
key | 鍵值 |
value | 寫(xiě)入數(shù)據(jù) |
返回值:
返回值 | 描述 |
---|---|
0 | 成功 |
其它 | 見(jiàn)utils/native/lite/include/ohos_errno.h |
UtilsDeleteValue()
intUtilsDeleteValue(constchar*key);
描述:
從文件系統(tǒng)或緩存中刪除與指定鍵匹配的值。
參數(shù):
名字 | 描述 |
---|---|
key | 鍵值 |
返回值:
返回值 | 描述 |
---|---|
0 | 成功 |
其它 | 見(jiàn)utils/native/lite/include/ohos_errno.h |
ClearKVCache()
int ClearKVCache(void);
描述:
從緩存中清除所有鍵值對(duì)。
返回值:
返回值 | 描述 |
---|---|
0 | 成功 |
其它 | 見(jiàn)utils/native/lite/include/ohos_errno.h |
軟件設(shè)計(jì)
主要代碼分析
在kv_store_example函數(shù)中通過(guò)LOS_TaskCreate函數(shù)創(chuàng)建兩個(gè)線程:kv_store_write_thread、kv_store_read_thread。
void kv_store_example(){ unsigned int thread_id1; unsigned int thread_id2; TSK_INIT_PARAM_S task1 = {0}; TSK_INIT_PARAM_S task2 = {0}; unsigned int ret = LOS_OK;
task1.pfnTaskEntry = (TSK_ENTRY_FUNC)kv_store_write_thread; task1.uwStackSize = 1024 * 10; task1.pcName = "kv_store_write_thread"; task1.usTaskPrio = 25; ret = LOS_TaskCreate(&thread_id1, &task1); if (ret != LOS_OK) { printf("Falied to create kv_store_write_thread ret:0x%x\n", ret); return; }
task2.pfnTaskEntry = (TSK_ENTRY_FUNC)kv_store_read_thread; task2.uwStackSize = 1024 * 10; task2.pcName = "kv_store_read_thread"; task2.usTaskPrio = 25; ret = LOS_TaskCreate(&thread_id2, &task2); if (ret != LOS_OK) { printf("Falied to create kv_store_read_thread ret:0x%x\n", ret); return; }}
APP_FEATURE_INIT(kv_store_example);
kv_store_write_thread線程負(fù)責(zé)創(chuàng)建/更新KV存儲(chǔ),每1秒寫(xiě)入一段內(nèi)容,重復(fù)以上流程。
void kv_store_write_thread(){ int ret = 0; char defValue[50] = {0}; int current = 0;
while (1) { snprintf(defValue, sizeof(defValue), "test value %d.", current); int ret = UtilsSetValue(key, defValue); if (ret < 0) { printf("[error] %d\r\n", ret); } else { printf("[write] write success\r\n"); }
current++; LOS_Msleep(1000); }}
kv_store_read_thread線程負(fù)責(zé)讀取KV存儲(chǔ),每1秒讀取一段內(nèi)容,重復(fù)以上流程。
void kv_store_read_thread(){ int ret = 0; char value1[50] = {0};
while (1) { ret = UtilsGetValue(key, value1, sizeof(value1)); if (ret < 0) { printf("[error] %d\r\n", ret); } else { printf("[read] key: %s value:%s\r\n", key, value1); }
LOS_Msleep(1000); }}
編譯調(diào)試
修改 BUILD.gn 文件
修改 vendor/lockzhiner/rk2206/sample 路徑下 BUILD.gn 文件,指定 a10_kv_store 參與編譯。
"./a10_kv_store:kv_store_example",
修改 device/rockchip/rk2206/sdk_liteos路徑下 Makefile 文件,添加 `-lkv_store_example` 參與編譯。
app_LIBS = -lkv_store_example
運(yùn)行結(jié)果
示例代碼編譯燒錄代碼后,按下開(kāi)發(fā)板的RESET按鍵,通過(guò)串口助手查看日志。
HalFileInit: Flash Init Successful![write] write success[read] key: key_sample value:test value 0.[write] write success[read] key: key_sample value:test value 1.[write] write success[read] key: key_sample value:test value 2.[write] write success[read]key:key_samplevalue:testvalue3.
-
操作系統(tǒng)
+關(guān)注
關(guān)注
37文章
7025瀏覽量
124700 -
開(kāi)發(fā)板
+關(guān)注
關(guān)注
25文章
5389瀏覽量
100876 -
分布式數(shù)據(jù)
+關(guān)注
關(guān)注
0文章
9瀏覽量
9015 -
OpenHarmony
+關(guān)注
關(guān)注
26文章
3804瀏覽量
17849
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
HarmonyOS開(kāi)發(fā)實(shí)例:【分布式數(shù)據(jù)管理】

基于Openharmony輕量級(jí)操作系統(tǒng)的分布式數(shù)據(jù)管理開(kāi)發(fā)案例
HarmonyOS鴻蒙操作系統(tǒng)之什么是“基于微內(nèi)核的全場(chǎng)景分布式操作系統(tǒng)”?
OpenHarmony 3.0 LTS 新增特性功能
分布式操作系統(tǒng)的相關(guān)資料分享
OpenHarmony標(biāo)準(zhǔn)設(shè)備應(yīng)用開(kāi)發(fā)(三)——分布式數(shù)據(jù)管理
OpenHarmony3.1分布式技術(shù)資料合集
【學(xué)習(xí)打卡】OpenHarmony的分布式數(shù)據(jù)管理介紹
【學(xué)習(xí)打卡】OpenHarmony的分布式任務(wù)調(diào)度
【開(kāi)發(fā)樣例】OpenHarmony分布式購(gòu)物車
好書(shū)推薦|《OpenHarmony嵌入式操作原理與應(yīng)用》
鴻蒙操作系統(tǒng)的前世今生
如何通過(guò)分布式數(shù)據(jù)管理實(shí)現(xiàn)多臺(tái)設(shè)備間的數(shù)據(jù)同步更新

鴻蒙開(kāi)發(fā)接口數(shù)據(jù)管理:【@ohos.data.distributedData (分布式數(shù)據(jù)管理)】

評(píng)論