Skip to content

Commit

Permalink
Store search index fields in array
Browse files Browse the repository at this point in the history
  • Loading branch information
agersant committed Sep 24, 2024
1 parent ee3f9fd commit 971b46b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ base64 = "0.22.1"
bitcode = { version = "0.6.3", features = ["serde"] }
branca = "0.10.1"
chumsky = "0.9.3"
enum-map = { version = "2.7.3", features = ["serde"] }
getopts = "0.2.21"
headers = "0.4"
http = "1.1.0"
Expand Down
5 changes: 3 additions & 2 deletions src/app/index/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use chumsky::{
text::{int, keyword, whitespace, TextParser},
Parser,
};
use enum_map::Enum;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, Enum, Eq, Hash, PartialEq, Serialize)]
pub enum TextField {
Album,
AlbumArtist,
Expand All @@ -27,7 +28,7 @@ pub enum TextOp {
Like,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, Enum, Eq, Hash, PartialEq, Serialize)]
pub enum NumberField {
DiscNumber,
TrackNumber,
Expand Down
79 changes: 20 additions & 59 deletions src/app/index/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chumsky::Parser;
use enum_map::EnumMap;
use lasso2::{RodeoReader, Spur};
use nohash_hasher::{IntMap, IntSet};
use serde::{Deserialize, Serialize};
Expand All @@ -24,8 +25,8 @@ use super::{

#[derive(Serialize, Deserialize)]
pub struct Search {
text_fields: HashMap<TextField, TextFieldIndex>,
number_fields: HashMap<NumberField, NumberFieldIndex>,
text_fields: EnumMap<TextField, TextFieldIndex>,
number_fields: EnumMap<NumberField, NumberFieldIndex>,
}

impl Default for Search {
Expand Down Expand Up @@ -149,13 +150,9 @@ impl Search {
operator: TextOp,
value: &str,
) -> IntSet<SongKey> {
let Some(field_index) = self.text_fields.get(&field) else {
return IntSet::default();
};

match operator {
TextOp::Eq => field_index.find_exact(canon, value),
TextOp::Like => field_index.find_like(strings, value),
TextOp::Eq => self.text_fields[field].find_exact(canon, value),
TextOp::Like => self.text_fields[field].find_like(strings, value),
}
}

Expand All @@ -165,10 +162,7 @@ impl Search {
operator: NumberOp,
value: i32,
) -> IntSet<SongKey> {
let Some(field_index) = self.number_fields.get(&field) else {
return IntSet::default();
};
field_index.find(value as i64, operator)
self.number_fields[field].find(value as i64, operator)
}
}

Expand Down Expand Up @@ -267,8 +261,8 @@ impl NumberFieldIndex {

#[derive(Default)]
pub struct Builder {
text_fields: HashMap<TextField, TextFieldIndex>,
number_fields: HashMap<NumberField, NumberFieldIndex>,
text_fields: EnumMap<TextField, TextFieldIndex>,
number_fields: EnumMap<NumberField, NumberFieldIndex>,
}

impl Builder {
Expand All @@ -278,98 +272,65 @@ impl Builder {
};

if let (Some(str), Some(spur)) = (&scanner_song.album, storage_song.album) {
self.text_fields
.entry(TextField::Album)
.or_default()
.insert(str, spur, song_key);
self.text_fields[TextField::Album].insert(str, spur, song_key);
}

for (str, spur) in scanner_song
.album_artists
.iter()
.zip(storage_song.album_artists.iter())
{
self.text_fields
.entry(TextField::AlbumArtist)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::AlbumArtist].insert(str, *spur, song_key);
}

for (str, spur) in scanner_song.artists.iter().zip(storage_song.artists.iter()) {
self.text_fields
.entry(TextField::Artist)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::Artist].insert(str, *spur, song_key);
}

for (str, spur) in scanner_song
.composers
.iter()
.zip(storage_song.composers.iter())
{
self.text_fields
.entry(TextField::Composer)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::Composer].insert(str, *spur, song_key);
}

if let Some(disc_number) = &scanner_song.disc_number {
self.number_fields
.entry(NumberField::DiscNumber)
.or_default()
.insert(*disc_number, song_key);
self.number_fields[NumberField::DiscNumber].insert(*disc_number, song_key);
}

for (str, spur) in scanner_song.genres.iter().zip(storage_song.genres.iter()) {
self.text_fields
.entry(TextField::Genre)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::Genre].insert(str, *spur, song_key);
}

for (str, spur) in scanner_song.labels.iter().zip(storage_song.labels.iter()) {
self.text_fields
.entry(TextField::Label)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::Label].insert(str, *spur, song_key);
}

for (str, spur) in scanner_song
.lyricists
.iter()
.zip(storage_song.lyricists.iter())
{
self.text_fields
.entry(TextField::Lyricist)
.or_default()
.insert(str, *spur, song_key);
self.text_fields[TextField::Lyricist].insert(str, *spur, song_key);
}

self.text_fields.entry(TextField::Path).or_default().insert(
self.text_fields[TextField::Path].insert(
scanner_song.virtual_path.to_string_lossy().as_ref(),
storage_song.virtual_path.0,
song_key,
);

if let (Some(str), Some(spur)) = (&scanner_song.title, storage_song.title) {
self.text_fields
.entry(TextField::Title)
.or_default()
.insert(str, spur, song_key);
self.text_fields[TextField::Title].insert(str, spur, song_key);
}

if let Some(track_number) = &scanner_song.track_number {
self.number_fields
.entry(NumberField::TrackNumber)
.or_default()
.insert(*track_number, song_key);
self.number_fields[NumberField::TrackNumber].insert(*track_number, song_key);
}

if let Some(year) = &scanner_song.year {
self.number_fields
.entry(NumberField::Year)
.or_default()
.insert(*year, song_key);
self.number_fields[NumberField::Year].insert(*year, song_key);
}
}

Expand Down

0 comments on commit 971b46b

Please sign in to comment.