Skip to content

Commit 1a89531

Browse files
committed
feat: dictionary
1 parent 4dbe06e commit 1a89531

10 files changed

Lines changed: 184 additions & 84 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"antd": "^5.9.0",
1818
"cheerio": "1.0.0-rc.12",
1919
"localforage": "^1.10.0",
20+
"lodash": "^4.17.21",
2021
"match-sorter": "^6.3.1",
2122
"react": "^18.2.0",
2223
"react-dom": "^18.2.0",
@@ -29,6 +30,7 @@
2930
"devDependencies": {
3031
"@tailwindcss/typography": "^0.5.9",
3132
"@tauri-apps/cli": "^1.4.0",
33+
"@types/lodash": "^4.14.201",
3234
"@types/node": "^20.3.3",
3335
"@types/react": "^18.2.15",
3436
"@types/react-dom": "^18.2.7",

pnpm-lock.yaml

Lines changed: 23 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/src/commands/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ pub fn greet(name: &str) -> String {
99

1010
#[tauri::command]
1111
pub fn put_db(key: &str, value: &str) {
12-
db::put(key,value);
12+
db::put(key, value);
1313
}
1414

1515
#[tauri::command]
1616
pub fn get_db(key: &str) -> String {
1717
db::get(key)
18-
}
18+
}
19+
20+
#[tauri::command]
21+
pub fn get_all_db_keys() -> Vec<String> {
22+
db::get_all()
23+
}
24+
25+
#[tauri::command]
26+
pub fn remove_key(key: &str) {
27+
db::remove_key(key)
28+
}

src-tauri/src/db/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extern crate rocksdb;
2-
use rocksdb::{DBCommon, DBIteratorWithThreadMode, DBWithThreadMode, Options, SingleThreaded, DB};
2+
use rocksdb::{IteratorMode, DB};
33

44
fn make_db_path() -> String {
55
let home_dir = tauri::api::path::home_dir().unwrap();
@@ -12,11 +12,17 @@ fn make_db_path() -> String {
1212
path.to_str().unwrap().to_string()
1313
}
1414

15+
fn open_db() -> DB {
16+
let path = make_db_path();
17+
let db = DB::open_default(path).unwrap();
18+
return db;
19+
}
20+
1521
pub fn put(k: &str, v: &str) {
1622
let path = make_db_path();
1723
let db = DB::open_default(path).unwrap();
1824
db.put(k, v).unwrap();
19-
println!("SET key: {}, value: {}", k, v);
25+
// println!("SET key: {}, value: {}", k, v);
2026
}
2127

2228
pub fn get(k: &str) -> String {
@@ -26,12 +32,34 @@ pub fn get(k: &str) -> String {
2632
match value {
2733
Some(v) => {
2834
let s = String::from_utf8(v).unwrap();
29-
println!("GET SOME key: {}, value: {}", k, s);
35+
// println!("GET SOME key: {}, value: {}", k, s);
3036
return s;
3137
}
3238
None => {
33-
println!("GET NONE key: {}, value: None", k);
39+
// println!("GET NONE key: {}, value: None", k);
3440
return String::from("None");
3541
}
3642
}
3743
}
44+
45+
// get all keys
46+
pub fn get_all() -> Vec<String> {
47+
let db = open_db();
48+
let mut keys: Vec<String> = Vec::new();
49+
let iterator = db.iterator(IteratorMode::Start);
50+
for result in iterator {
51+
let (key, _) = result.unwrap();
52+
let key_str = String::from_utf8_lossy(&key);
53+
// println!("Key: {}", key_str);
54+
keys.push(key_str.to_string());
55+
}
56+
return keys;
57+
}
58+
59+
// remove by key
60+
pub fn remove_key(k: &str) {
61+
let path = make_db_path();
62+
let db = DB::open_default(path).unwrap();
63+
db.delete(k).unwrap();
64+
// println!("REMOVE key: {}", k);
65+
}

src-tauri/src/events/mod.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +0,0 @@
1-
use std::thread::sleep;
2-
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
3-
4-
use tauri::{App, Manager};
5-
6-
use crate::Payload;
7-
pub fn handle_time_event(app: &mut App) {
8-
let interval = Duration::from_secs(1);
9-
10-
let mut start_time = Instant::now();
11-
12-
loop {
13-
// Your function to run every 1 second goes here
14-
// Get the current time as a `SystemTime` object
15-
let current_time = SystemTime::now();
16-
17-
// Calculate the duration since the Unix epoch (January 1, 1970)
18-
let duration_since_epoch = current_time
19-
.duration_since(UNIX_EPOCH)
20-
.expect("Time went backwards");
21-
22-
// Get the timestamp as a u64 representing the number of seconds since the epoch
23-
let timestamp = duration_since_epoch.as_secs();
24-
25-
app.emit_all(
26-
"click",
27-
Payload {
28-
message: timestamp.to_string().into(),
29-
},
30-
)
31-
.unwrap();
32-
println!("Current timestamp: {}", timestamp);
33-
34-
// Sleep for 1 second
35-
let elapsed_time = start_time.elapsed();
36-
if elapsed_time < interval {
37-
let sleep_duration = interval - elapsed_time;
38-
sleep(sleep_duration);
39-
}
40-
41-
// Reset the start time for the next iteration
42-
start_time = Instant::now();
43-
}
44-
}

src-tauri/src/main.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
3-
4-
5-
6-
use std::{thread, time::Duration};
7-
83
use tauri::Manager;
94

105
mod commands;
116
pub mod db;
12-
mod menus;
137
mod events;
8+
mod menus;
149
// the payload type must implement `Serialize` and `Clone`.
1510
#[derive(Clone, serde::Serialize)]
1611
struct Payload {
@@ -23,7 +18,9 @@ fn main() {
2318
.invoke_handler(tauri::generate_handler![
2419
commands::greet,
2520
commands::put_db,
26-
commands::get_db
21+
commands::get_db,
22+
commands::get_all_db_keys,
23+
commands::remove_key
2724
])
2825
.setup(|app| {
2926
// listen to the `event-name` (emitted on any window)
@@ -42,11 +39,11 @@ fn main() {
4239
// unlisten to the event using the `id` returned on the `listen_global` function
4340
// a `once_global` API is also exposed on the `App` struct
4441
// app.unlisten(id);
45-
//
42+
//
4643

4744
// let handle = thread::spawn(|| {
48-
// events::handle_time_event(app);
49-
// });
45+
// events::handle_time_event(app);
46+
// });
5047
Ok(())
5148
})
5249
.run(tauri::generate_context!())

src-tauri/src/menus/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
1+
use tauri::{CustomMenuItem, Menu, Submenu};
22

33
pub fn make_menu() -> Menu {
44
let quit = CustomMenuItem::new("quit".to_string(), "Quit");

0 commit comments

Comments
 (0)