|
| 1 | +// |
| 2 | +// k2hash_rust |
| 3 | +// |
| 4 | +// Copyright 2025 LY Corporation. |
| 5 | +// |
| 6 | +// Rust driver for k2hash that is a NoSQL Key Value Store(KVS) library. |
| 7 | +// For k2hash, see https://github.com/yahoojapan/k2hash for the details. |
| 8 | +// |
| 9 | +// For the full copyright and license information, please view |
| 10 | +// the license file that was distributed with this source code. |
| 11 | +// |
| 12 | +// AUTHOR: Hirotaka Wakabayashi |
| 13 | +// CREATE: Fri, 17 Jul 2025 |
| 14 | +// REVISION: |
| 15 | +// |
| 16 | + |
| 17 | +use k2hash_rust::{K2hash, KeyQueue, KeyQueueBuilder}; |
| 18 | + |
| 19 | +fn main() { |
| 20 | + let db = K2hash::open_mem().expect("open_mem failed"); |
| 21 | + |
| 22 | + let q = KeyQueue::new(db.handle(), true, None, None, None).expect("KeyQueue creation failed"); |
| 23 | + let key = "hello".to_string(); |
| 24 | + let value = "world".to_string(); |
| 25 | + q.put(&key, &value).expect("Push failed"); |
| 26 | + if let Some(value) = q.get() { |
| 27 | + println!("Popped key: {}, value: {}", value.0, value.1); |
| 28 | + } else { |
| 29 | + println!("KeyQueue is empty"); |
| 30 | + } |
| 31 | + assert!(q.qsize() == 0); |
| 32 | + assert!(q.clear()); |
| 33 | + assert!(q.close()); |
| 34 | + // Example of using KeyQueueBuilder |
| 35 | + let db = K2hash::open_mem().expect("open_mem failed"); |
| 36 | + |
| 37 | + let fifo = true; // or false, depending on your needs |
| 38 | + let prefix = "test_prefix".to_string(); |
| 39 | + let password = Some("your_password".to_string()); |
| 40 | + let expire_duration = Some(60); // for 60 seconds expiration |
| 41 | + // Create the KeyQueue using KeyQueueBuilder |
| 42 | + let qb1 = KeyQueueBuilder::new(db.handle()) |
| 43 | + .fifo(fifo) |
| 44 | + .prefix(prefix) // Optional prefix |
| 45 | + .password(password.expect("Error")) // Optional password |
| 46 | + .expire_duration(expire_duration.expect("Error")) // Optional expiration duration |
| 47 | + .build() |
| 48 | + .expect("KeyQueue creation failed"); |
| 49 | + qb1.put(&key, &value).expect("Push failed"); |
| 50 | + if let Some(value) = qb1.get() { |
| 51 | + println!("Popped key: {}, value: {}", value.0, value.1); |
| 52 | + } else { |
| 53 | + println!("KeyQueue is empty"); |
| 54 | + } |
| 55 | + assert!(qb1.qsize() == 0); |
| 56 | + assert!(qb1.clear()); |
| 57 | + assert!(qb1.close()); |
| 58 | +} |
| 59 | + |
| 60 | +// |
| 61 | +// Local variables: |
| 62 | +// tab-width: 4 |
| 63 | +// c-basic-offset: 4 |
| 64 | +// End: |
| 65 | +// vim600: expandtab sw=4 ts=4 fdm=marker |
| 66 | +// vim<600: expandtab sw=4 ts=4 |
| 67 | +// |
0 commit comments