diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 069edec..68352f3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -58,14 +58,6 @@ jobs: run: pnpm install - name: Build web run: pnpm build - - name: Check code format - run: | - cd src-tauri - cargo fmt -- --check - - name: Lint rust sources - run: | - cd src-tauri - cargo clippy --all-targets --all-features --tests --benches -- -D warnings - name: Build tauri client run: | pnpm tauri build diff --git a/README.md b/README.md index fdb21cb..bec97a4 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ ## FIXME - [x] Need to consider loading time, pause local player when loading. (Preload?) - Renew the progress after track loaded. -- [x] Reduce the frequency of rebuilding the tantivy index. - [ ] Refactor client player (init status, error handle, etc.) ## Tech Stack diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 1296a3b..bf2016c 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -172,12 +172,6 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" -[[package]] -name = "arc-swap" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" - [[package]] name = "arrayvec" version = "0.7.4" @@ -523,15 +517,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bitpacking" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8c7d2ac73c167c06af4a5f37e6e59d84148d57ccbe4480b76f0273eefea82d7" -dependencies = [ - "crunchy", -] - [[package]] name = "bitvec" version = "1.0.1" @@ -754,25 +739,9 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ - "jobserver", "libc", ] -[[package]] -name = "cedarwood" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d910bedd62c24733263d0bed247460853c9d22e8956bd4cd964302095e04e90" -dependencies = [ - "smallvec", -] - -[[package]] -name = "census" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fafee10a5dd1cffcb5cc560e0d0df8803d7355a2b12272e3557dee57314cb6e" - [[package]] name = "cesu8" version = "1.1.0" @@ -1105,30 +1074,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "crossbeam-deque" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" -dependencies = [ - "cfg-if", - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" -dependencies = [ - "autocfg", - "cfg-if", - "crossbeam-utils", - "memoffset", - "scopeguard", -] - [[package]] name = "crossbeam-queue" version = "0.3.8" @@ -1148,12 +1093,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crunchy" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - [[package]] name = "crypto-common" version = "0.1.6" @@ -1269,8 +1208,6 @@ dependencies = [ "futures-util", "migration", "sea-orm", - "tantivy", - "tantivy-jieba", "thiserror", "tokio", "tokio-stream", @@ -1378,12 +1315,6 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" -[[package]] -name = "downcast-rs" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" - [[package]] name = "dtoa" version = "1.0.9" @@ -1502,12 +1433,6 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" -[[package]] -name = "fastdivide" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25c7df09945d65ea8d70b3321547ed414bbc540aad5bac6883d021b970f35b04" - [[package]] name = "fastrand" version = "1.9.0" @@ -1645,16 +1570,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "fs4" -version = "0.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eeb4ed9e12f43b7fa0baae3f9cdda28352770132ef2e09a23760c29cae8bd47" -dependencies = [ - "rustix 0.38.11", - "windows-sys", -] - [[package]] name = "funty" version = "2.0.0" @@ -2261,12 +2176,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "htmlescape" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163" - [[package]] name = "http" version = "0.2.9" @@ -2521,9 +2430,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", ] [[package]] @@ -2587,21 +2493,6 @@ dependencies = [ "system-deps 5.0.0", ] -[[package]] -name = "jieba-rs" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f0c1347cd3ac8d7c6e3a2dc33ac496d365cf09fc0831aa61111e1a6738983e" -dependencies = [ - "cedarwood", - "fxhash", - "hashbrown 0.14.0", - "lazy_static", - "phf 0.11.2", - "phf_codegen 0.11.2", - "regex", -] - [[package]] name = "jni" version = "0.20.0" @@ -2622,15 +2513,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" -[[package]] -name = "jobserver" -version = "0.1.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" -dependencies = [ - "libc", -] - [[package]] name = "js-sys" version = "0.3.64" @@ -2696,12 +2578,6 @@ dependencies = [ "spin 0.5.2", ] -[[package]] -name = "levenshtein_automata" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2cdeb66e45e9f36bfad5bbdb4d2384e70936afbee843c6f6543f0c551ebb25" - [[package]] name = "libc" version = "0.2.147" @@ -2800,7 +2676,6 @@ checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5" dependencies = [ "cfg-if", "generator", - "pin-utils", "scoped-tls", "serde", "serde_json", @@ -2808,21 +2683,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "lru" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eedb2bdbad7e0634f83989bf596f497b070130daaa398ab22d84c39e266deec5" -dependencies = [ - "hashbrown 0.14.0", -] - -[[package]] -name = "lz4_flex" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea9b256699eda7b0387ffbc776dd625e28bde3918446381781245b7a50349d8" - [[package]] name = "mac" version = "0.1.1" @@ -2846,7 +2706,7 @@ checksum = "a24f40fb03852d1cdd84330cddcaf98e9ec08a7b7768e952fad3b4cf048ec8fd" dependencies = [ "log", "phf 0.8.0", - "phf_codegen 0.8.0", + "phf_codegen", "string_cache", "string_cache_codegen", "tendril", @@ -2888,31 +2748,12 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" -[[package]] -name = "measure_time" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56220900f1a0923789ecd6bf25fbae8af3b2f1ff3e9e297fc9b6b8674dd4d852" -dependencies = [ - "instant", - "log", -] - [[package]] name = "memchr" version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" -[[package]] -name = "memmap2" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f49388d20533534cd19360ad3d6a7dadc885944aa802ba3995040c5ec11288c6" -dependencies = [ - "libc", -] - [[package]] name = "memoffset" version = "0.9.0" @@ -2989,12 +2830,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" -[[package]] -name = "murmurhash32" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9380db4c04d219ac5c51d14996bbf2c2e9a15229771b53f8671eb6c83cf44df" - [[package]] name = "native-tls" version = "0.2.11" @@ -3262,15 +3097,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "oneshot" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc22d22931513428ea6cc089e942d38600e3d00976eef8c86de6b8a3aadec6eb" -dependencies = [ - "loom", -] - [[package]] name = "openssl" version = "0.10.57" @@ -3354,15 +3180,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" -[[package]] -name = "ownedbytes" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e8a72b918ae8198abb3a18c190288123e1d442b6b9a7d709305fd194688b4b7" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "pango" version = "0.15.10" @@ -3499,16 +3316,6 @@ dependencies = [ "phf_shared 0.8.0", ] -[[package]] -name = "phf_codegen" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" -dependencies = [ - "phf_generator 0.11.2", - "phf_shared 0.11.2", -] - [[package]] name = "phf_generator" version = "0.8.0" @@ -4009,28 +3816,6 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" -[[package]] -name = "rayon" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" -dependencies = [ - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-utils", - "num_cpus", -] - [[package]] name = "redox_syscall" version = "0.2.16" @@ -4260,16 +4045,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "rust-stemmers" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e46a2036019fdb888131db7a4c847a1063a7493f971ed94ea82c67eada63ca54" -dependencies = [ - "serde", - "serde_derive", -] - [[package]] name = "rust_decimal" version = "1.32.0" @@ -4292,12 +4067,6 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustc_version" version = "0.4.0" @@ -4623,7 +4392,7 @@ dependencies = [ "log", "matches", "phf 0.8.0", - "phf_codegen 0.8.0", + "phf_codegen", "precomputed-hash", "servo_arc", "smallvec", @@ -4889,15 +4658,6 @@ version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" -[[package]] -name = "sketches-ddsketch" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a406c1882ed7f29cd5e248c9848a80e7cb6ae0fea82346d2746f2f941c07e1" -dependencies = [ - "serde", -] - [[package]] name = "slab" version = "0.4.9" @@ -5344,157 +5104,6 @@ dependencies = [ "version-compare 0.1.1", ] -[[package]] -name = "tantivy" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1d4675fed6fe2218ce11445374e181e864a8ffd0f28e7e0591ccfc38cd000ae" -dependencies = [ - "aho-corasick", - "arc-swap", - "async-trait", - "base64 0.21.3", - "bitpacking", - "byteorder", - "census", - "crc32fast", - "crossbeam-channel", - "downcast-rs", - "fastdivide", - "fs4", - "htmlescape", - "itertools", - "levenshtein_automata", - "log", - "lru", - "lz4_flex", - "measure_time", - "memmap2", - "murmurhash32", - "num_cpus", - "once_cell", - "oneshot", - "rayon", - "regex", - "rust-stemmers", - "rustc-hash", - "serde", - "serde_json", - "sketches-ddsketch", - "smallvec", - "tantivy-bitpacker", - "tantivy-columnar", - "tantivy-common", - "tantivy-fst", - "tantivy-query-grammar", - "tantivy-stacker", - "tantivy-tokenizer-api", - "tempfile", - "thiserror", - "time 0.3.28", - "uuid", - "winapi", -] - -[[package]] -name = "tantivy-bitpacker" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cecb164321482301f514dd582264fa67f70da2d7eb01872ccd71e35e0d96655a" -dependencies = [ - "bitpacking", -] - -[[package]] -name = "tantivy-columnar" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d85f8019af9a78b3118c11298b36ffd21c2314bd76bbcd9d12e00124cbb7e70" -dependencies = [ - "fastdivide", - "fnv", - "itertools", - "serde", - "tantivy-bitpacker", - "tantivy-common", - "tantivy-sstable", - "tantivy-stacker", -] - -[[package]] -name = "tantivy-common" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af4a3a975e604a2aba6b1106a04505e1e7a025e6def477fab6e410b4126471e1" -dependencies = [ - "async-trait", - "byteorder", - "ownedbytes", - "serde", - "time 0.3.28", -] - -[[package]] -name = "tantivy-fst" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc3c506b1a8443a3a65352df6382a1fb6a7afe1a02e871cee0d25e2c3d5f3944" -dependencies = [ - "byteorder", - "regex-syntax 0.6.29", - "utf8-ranges", -] - -[[package]] -name = "tantivy-jieba" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44022293c12a8f878e03439b2f11806d3d394130fe33d4e7781cba91abbac0a4" -dependencies = [ - "jieba-rs", - "lazy_static", - "tantivy-tokenizer-api", -] - -[[package]] -name = "tantivy-query-grammar" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d39c5a03100ac10c96e0c8b07538e2ab8b17da56434ab348309b31f23fada77" -dependencies = [ - "nom", -] - -[[package]] -name = "tantivy-sstable" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc0c1bb43e5e8b8e05eb8009610344dbf285f06066c844032fbb3e546b3c71df" -dependencies = [ - "tantivy-common", - "tantivy-fst", - "zstd", -] - -[[package]] -name = "tantivy-stacker" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2c078595413f13f218cf6f97b23dcfd48936838f1d3d13a1016e05acd64ed6c" -dependencies = [ - "murmurhash32", - "tantivy-common", -] - -[[package]] -name = "tantivy-tokenizer-api" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "347b6fb212b26d3505d224f438e3c4b827ab8bd847fe9953ad5ac6b8f9443b66" -dependencies = [ - "serde", -] - [[package]] name = "tao" version = "0.16.2" @@ -6317,12 +5926,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" -[[package]] -name = "utf8-ranges" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcfc827f90e53a02eaef5e535ee14266c1d569214c6aa70133a624d8a3164ba" - [[package]] name = "utf8parse" version = "0.2.1" @@ -6978,33 +6581,3 @@ name = "zeroize" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" - -[[package]] -name = "zstd" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a27595e173641171fc74a1232b7b1c7a7cb6e18222c11e9dfb9888fa424c53c" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "6.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee98ffd0b48ee95e6c5168188e44a54550b1564d9d530ee21d5f0eaed1069581" -dependencies = [ - "libc", - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.8+zstd.1.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5556e6ee25d32df2586c098bbfa278803692a20d0ab9565e049480d52707ec8c" -dependencies = [ - "cc", - "libc", - "pkg-config", -] diff --git a/src-tauri/dbm/Cargo.toml b/src-tauri/dbm/Cargo.toml index 5795d3c..87fa777 100644 --- a/src-tauri/dbm/Cargo.toml +++ b/src-tauri/dbm/Cargo.toml @@ -23,8 +23,6 @@ chrono = { workspace = true } tonic = { workspace = true } tokio = { workspace = true, features = ["full"] } tokio-stream = { workspace = true } -tantivy = "0.21" -tantivy-jieba = "0.10.0" utils = { path = "../utils" } debounced = { git = "https://github.com/Discreater/debounced" } futures-channel = { workspace = true, features = ["sink"] } diff --git a/src-tauri/dbm/src/lib.rs b/src-tauri/dbm/src/lib.rs index b2eb47a..592e5a4 100644 --- a/src-tauri/dbm/src/lib.rs +++ b/src-tauri/dbm/src/lib.rs @@ -1,10 +1,8 @@ pub mod error; mod manager; -mod search; pub use error::MusyncError; use sea_orm::DatabaseConnection; -use search::SearchActorHandle; pub type PlaylistId = i32; pub type TrackId = i32; @@ -13,5 +11,4 @@ pub type UserId = i32; #[derive(Debug, Clone)] pub struct DbManager { db: DatabaseConnection, - search_actor_handle: SearchActorHandle, } diff --git a/src-tauri/dbm/src/manager.rs b/src-tauri/dbm/src/manager.rs index a4df0de..eb54775 100644 --- a/src-tauri/dbm/src/manager.rs +++ b/src-tauri/dbm/src/manager.rs @@ -14,12 +14,11 @@ use sea_orm::{ }; use tracing::{info, trace, warn}; -use crate::{search::SearchActorHandle, DbManager, MusyncError, PlaylistId, TrackId, UserId}; +use crate::{DbManager, MusyncError, PlaylistId, TrackId, UserId}; impl DbManager { - pub fn new(db: DatabaseConnection, data_folder: PathBuf) -> Self { + pub fn new(db: DatabaseConnection, _data_folder: PathBuf) -> Self { Self { - search_actor_handle: SearchActorHandle::new(db.clone(), data_folder), db, } } @@ -29,10 +28,6 @@ impl DbManager { Migrator::up(&db, None).await?; Ok(Self::new(db, data_folder)) } - - pub async fn re_index(&self) { - self.search_actor_handle.index().await; - } } /// Playlist impl DbManager { @@ -206,7 +201,6 @@ impl DbManager { .await?; } txn.commit().await?; - self.search_actor_handle.index().await; Ok(abi::Track::from_entity(inserted)) } @@ -258,7 +252,6 @@ impl DbManager { } LocalSrc::insert_many(local_srcs).exec(&txn).await?; txn.commit().await?; - self.search_actor_handle.index().await; Ok(inserted.last_insert_id) } @@ -284,7 +277,6 @@ impl DbManager { .exec(&txn) .await?; txn.commit().await?; - self.search_actor_handle.index().await; trace!("deleted tracks: {:?}", deleted); Ok(deleted.rows_affected) } @@ -345,7 +337,6 @@ impl DbManager { inserted.insert(&self.db).await?; } } - self.search_actor_handle.index().await; self.track(update.id).await } @@ -359,7 +350,6 @@ impl DbManager { .filter(entity::track::Column::Id.is_in(ids.to_owned())) .exec(&self.db) .await?; - self.search_actor_handle.index().await; Ok(deleted.rows_affected) } @@ -401,15 +391,6 @@ impl DbManager { .collect() } - pub async fn search_tracks(&self, query: &str) -> Result, MusyncError> { - let tracks = self.search_actor_handle.search(query.to_owned()).await?; - let tracks = Track::find() - .filter(entity::track::Column::Id.is_in(tracks)) - .all(&self.db) - .await?; - Ok(tracks.into_iter().map(abi::Track::from_entity).collect()) - } - pub async fn track(&self, id: TrackId) -> Result { let row = Track::find_by_id(id) .one(&self.db) diff --git a/src-tauri/dbm/src/search.rs b/src-tauri/dbm/src/search.rs deleted file mode 100644 index 64e39f5..0000000 --- a/src-tauri/dbm/src/search.rs +++ /dev/null @@ -1,205 +0,0 @@ -use std::{path::PathBuf, time::Duration}; - -use chrono::Utc; -use debounced::{debounced_if, Debounced}; -use futures_channel::mpsc; -use futures_util::SinkExt; -use sea_orm::{DatabaseConnection, EntityTrait}; -use tantivy::{ - directory::MmapDirectory, - doc, - query::QueryParser, - schema::{Schema, STORED, TEXT}, - Index, -}; -use tokio::sync::oneshot; -use tokio_stream::StreamExt; -use tracing::{error, trace}; - -use crate::{MusyncError, TrackId}; - -struct SearchActor { - receiver: Debounced>, - db: DatabaseConnection, - index: Index, -} - -enum ActorMessage { - Index, - Search { - query: String, - respond_to: oneshot::Sender, - }, -} - -impl SearchActor { - fn new( - receiver: Debounced>, - db: DatabaseConnection, - cache_data_folder: PathBuf, - ) -> Self { - Self { - receiver, - db, - index: Self::init(cache_data_folder), - } - } - - fn init(cache_data_folder: PathBuf) -> Index { - let mut schema_builder = Schema::builder(); - schema_builder.add_i64_field("id", STORED); - schema_builder.add_text_field("title", TEXT | STORED); - schema_builder.add_text_field("artist", TEXT | STORED); - schema_builder.add_text_field("album", TEXT | STORED); - let schema = schema_builder.build(); - if !cache_data_folder.exists() { - std::fs::create_dir_all(&cache_data_folder).expect("create cache data folder failed"); - } - let index = Index::open_or_create( - MmapDirectory::open(cache_data_folder).expect("open cache data folder failed"), - schema, - ) - .expect("create index failed"); - let tokenizer = tantivy_jieba::JiebaTokenizer {}; - index.tokenizers().register("jieba", tokenizer); - index - } - - async fn rebuild_index(&mut self) { - let start = Utc::now(); - // 100MB - let mut index_writer = self - .index - .writer(100_000_000) - .expect("create writer failed"); - index_writer - .delete_all_documents() - .expect("delete all documents failed"); - index_writer.commit().expect("commit failed"); - let tracks = match entity::track::Entity::find().all(&self.db).await { - Err(e) => { - error!("find all tracks failed: {:?}", e); - return; - } - Ok(track) => track, - }; - let id = self.index.schema().get_field("id").unwrap(); - let title = self.index.schema().get_field("title").unwrap(); - let artist = self.index.schema().get_field("artist").unwrap(); - let album = self.index.schema().get_field("album").unwrap(); - - for track in tracks { - let doc = doc!( - id => track.id as i64, - title => track.title, - artist => track.artist.unwrap_or_default(), - album => track.album.unwrap_or_default(), - ); - index_writer - .add_document(doc) - .expect("add doc should not fail"); - } - index_writer.commit().expect("commit failed"); - let end = Utc::now(); - trace!("index duration: {:?}", end - start); - } - - async fn handle_message(&mut self, msg: ActorMessage) { - match msg { - ActorMessage::Index => { - trace!("indexing..."); - self.rebuild_index().await; - } - ActorMessage::Search { query, respond_to } => { - trace!("searching: {query}"); - let start = Utc::now(); - let reader = self - .index - .reader_builder() - .reload_policy(tantivy::ReloadPolicy::OnCommit) - .try_into() - .unwrap(); - let searcher = reader.searcher(); - let id = self.index.schema().get_field("id").unwrap(); - let title = self.index.schema().get_field("title").unwrap(); - let artist = self.index.schema().get_field("artist").unwrap(); - let album = self.index.schema().get_field("album").unwrap(); - let query_parser = QueryParser::for_index(&self.index, vec![title, artist, album]); - let query = query_parser.parse_query(&query).unwrap(); - let top_docs = searcher - .search(&query, &tantivy::collector::TopDocs::with_limit(20)) - .unwrap(); - let track_ids = top_docs - .iter() - .map(|(_, doc)| { - let retrieved_doc = searcher.doc(*doc).unwrap(); - let id = retrieved_doc.get_first(id).unwrap().as_i64().unwrap(); - id as i32 - }) - .collect::>(); - for (score, doc_address) in top_docs { - let retrieved_doc = searcher.doc(doc_address).unwrap(); - trace!( - "score: {score}, doc: {}", - self.index.schema().to_json(&retrieved_doc) - ); - } - respond_to.send(track_ids).unwrap(); - let end = Utc::now(); - trace!("search duration: {:?}", end - start); - } - } - } -} - -async fn run_search_actor(mut actor: SearchActor) { - while let Some(msg) = actor.receiver.next().await { - actor.handle_message(msg).await; - } -} - -#[derive(Clone, Debug)] -pub struct SearchActorHandle { - sender: mpsc::Sender, -} - -pub type SearchResult = Vec; - -impl SearchActorHandle { - pub fn new(db: DatabaseConnection, data_folder: PathBuf) -> Self { - let (sender, receiver) = mpsc::channel(32); - let debounced = debounced_if(receiver, Duration::from_secs(20), |msg| { - !matches!(msg, ActorMessage::Index) - }); - let actor = SearchActor::new(debounced, db, data_folder.join("tantivy")); - tokio::spawn(run_search_actor(actor)); - Self { sender } - } - - pub async fn index(&self) { - let _msg = ActorMessage::Index; - // let mut sender = self.sender.clone(); - // sender.send(msg).await.with_err(|e| { - // error!("error sending message to search actor: {:?}", e); - // }); - // trace!("index message sent") - } - - pub async fn search(&self, query: String) -> Result { - let (send, recv) = oneshot::channel(); - let msg = ActorMessage::Search { - query, - respond_to: send, - }; - let mut sender: mpsc::Sender = self.sender.clone(); - sender.send(msg).await.map_err(|e| { - error!("error sending message to search actor: {:?}", e); - crate::error::MusyncError::SearchActor - })?; - trace!("search message sent"); - recv.await.map_err(|e| { - error!("error receiving search results: {:?}", e); - crate::error::MusyncError::SearchActor - }) - } -} diff --git a/src-tauri/server/src/grpc.rs b/src-tauri/server/src/grpc.rs index daa1048..f574b73 100644 --- a/src-tauri/server/src/grpc.rs +++ b/src-tauri/server/src/grpc.rs @@ -277,8 +277,7 @@ impl abi::musync_service_server::MusyncService for GrpcServer { &self, _req: Request, ) -> GrpcResult { - self.db.re_index().await; - Ok(Response::new(RebuildIndexResponse { success: true })) + Err(Status::unimplemented("rebuild index unimplemented")) } } diff --git a/src-tauri/server/src/lib.rs b/src-tauri/server/src/lib.rs index 0e1cd31..37b4ab4 100644 --- a/src-tauri/server/src/lib.rs +++ b/src-tauri/server/src/lib.rs @@ -116,27 +116,6 @@ mod tests { .init(); } - #[tokio::test] - async fn search_test() { - init_tracing(); - let memory_sqlite_url = "sqlite::memory:"; - let manager = - dbm::DbManager::from_url(memory_sqlite_url, PathBuf::from("../target/data/server/")) - .await - .unwrap(); - let folder = "D:\\media\\music"; - let tracks = crate::musync::track::get_tracks_in_folder(folder); - manager.re_index().await; - manager.create_tracks(tracks, folder).await.unwrap(); - manager.re_index().await; - manager.re_index().await; - - let search_res = manager.search_tracks("beyond 邓紫棋 one").await.unwrap(); - println!("search res: {}", search_res.len()); - - tokio::time::sleep(Duration::from_secs(40)).await; - } - #[tokio::test] async fn main() { init_tracing();