溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

sled怎么使用

發(fā)布時間:2022-01-17 10:32:20 來源:億速云 閱讀:127 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹了sled怎么使用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇sled怎么使用文章都會有所收獲,下面我們一起來看看吧。

簡介

Sled  是基于Bw樹構(gòu)建的嵌入式KV數(shù)據(jù)庫,其API接近于一個線程安全的BTreeMap<[u8], [u8]>。而其Bw樹的數(shù)據(jù)結(jié)構(gòu)加上包括crossbeam-epoch的“GC”等技術(shù),使得Sled成為一個lock-free的數(shù)據(jù)庫而在并發(fā)環(huán)境中傲視群雄。忘記那些慢吞吞的鎖吧~ 而官方宣稱在一臺16核的機(jī)器上,在一個小數(shù)據(jù)集上可以達(dá)到每分鐘10億次操作(95%讀核5%寫)
要使用sled,只需要在Cargo.toml中加入
  sled = "0.32"

例子

基礎(chǔ)用法  
打開數(shù)據(jù)庫let tree = sled::open("/tmp/welcome-to-sled").expect("open");
// 插入KV,讀取Key對應(yīng)的值tree.insert("KEY1", "VAL1");assert_eq!(tree.get(&"KEY1"), Ok(Some(sled::IVec::from("VAL1"))));
// 范圍查詢for kv in tree.range("KEY1".."KEY9") {    ...}
// 刪除tree.remove(&"KEY1");
// atomic compare and swap,可以用在并發(fā)編程中tree.compare_and_swap("KEY1", Some("VAL1"), Some("VAL2"));
// 阻塞直到所有修改都寫入硬盤tree.flush();
處理結(jié)構(gòu)體
use {    byteorder::{BigEndian, LittleEndian},    zerocopy::{        byteorder::U64, AsBytes, FromBytes, LayoutVerified, Unaligned,    },};
//  鍵結(jié)構(gòu)體//  zerocopy::byteorder::U64保證了數(shù)據(jù)對齊問題#[derive(FromBytes, AsBytes, Unaligned)]#[repr(C)]struct Key {    a: U64<BigEndian>,    b: U64<BigEndian>,}
// 值結(jié)構(gòu)體#[derive(FromBytes, AsBytes, Unaligned)]#[repr(C)]struct Value {    count: U64<LittleEndian>,    whatever: [u8; 16],}
let key = Key { a: U64::new(21), b: U64::new(890) };
// 取得鍵所對應(yīng)的值,并對其施加給定函數(shù)燦做db.update_and_fetch(key.as_bytes(), |value_opt| {    if let Some(existing) = value_opt {        let mut backing_bytes = sled::IVec::from(existing);
       // 驗(yàn)證數(shù)據(jù)對齊(這里其實(shí)不是必須的,因?yàn)槲覀兪褂昧薝64)        let layout: LayoutVerified<&mut [u8], Value> =            LayoutVerified::new_unaligned(&mut *backing_bytes)                .expect("bytes do not fit schema");
       // 得到底層數(shù)據(jù)的可變引用        let value: &mut Value = layout.into_mut();
       let new_count = value.count.get() + 1;
       println!("incrementing count to {}", new_count);
       value.count.set(new_count);
       Some(backing_bytes)    } else {        println!("setting count to 0");
       //  初始化一個Value        Some(sled::IVec::from(            Value { count: U64::new(0), whatever: [0; 16] }.as_bytes(),        ))    }})?;

關(guān)于“sled怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“sled怎么使用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI