Skip to content

Commit

Permalink
populate local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 19, 2024
1 parent a79a7e5 commit 1e05f37
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 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 gamercade_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gamercade_interface = { path = "../gamercade_interface" }

rfd = "0.11.3"
radix_fmt = "1.0.0"
bytemuck = "1.15.0"

tokio = { version = "1.28.1", features = ["full"] }
tokio-stream = "0.1.14"
Expand Down
7 changes: 4 additions & 3 deletions gamercade_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ impl App {
TaskNotification::DownloadRomComplete(complete) => {
println!("TODO: Release download complete")
}
TaskNotification::FrontPageResponse(front_page_response) => {
println!("TODO: Got Response: {front_page_response:?}");
}
TaskNotification::FrontPageResponse(mut front_page_response) => front_page_response
.games
.drain(..)
.for_each(|game| self.directory.update_game(game)),
}
}
}
Expand Down
30 changes: 27 additions & 3 deletions gamercade_app/src/local_directory/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,45 @@ pub(super) fn upsert_games_table(db: &Connection) {
}

impl LocalDirectory {
pub fn update_game(&self, game: GameInfoBasic) {
pub fn update_game(&mut self, game: GameInfoBasic) {
let tag_bytes = game
.tags
.into_iter()
.map(|tag| u8::try_from(tag).unwrap())
.collect::<Vec<_>>();

self.db.execute("INSERT OR REPLACE INTO games (id, title, short_description, file_checksum, rating, tags)
VALUES (?, ?, ?, ?, ?, ?, ?)",
VALUES (?, ?, ?, ?, ?, ?);",
(game.game_id, game.title, game.short_description, game.checksum, game.average_rating, tag_bytes)).unwrap();

self.cache_dirty = true;
}

pub fn iter_games(&self) -> GameIter<'_> {
GameIter::new(&self.cached_games)
}

pub fn sync_games_cache(&mut self) {
let mut query = self.db.prepare("SELECT * FROM games;").unwrap();

self.cached_games = query
.query_map((), |row| {
let tag_bytes: Vec<u8> = row.get(4)?;
Ok(Game {
id: row.get(0)?,
title: row.get(1)?,
short_description: row.get(2)?,
long_description: row.get(3)?,
tags: bytemuck::cast_vec(tag_bytes),
rating: row.get(5)?,
})
})
.unwrap()
.flatten()
.collect();

self.cache_dirty = false;
}
}

pub struct GameIter<'a> {
Expand Down
6 changes: 5 additions & 1 deletion gamercade_app/src/local_directory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PermissionLevelDictionary = Dictionary<PermissionLevelId, PermissionLevel>;
pub struct LocalDirectory {
db: Connection,
pub cached_games: Vec<Game>,
cache_dirty: bool,
pub tags: TagDictionary,
pub users: UserDictionary,
pub permission_levels: PermissionLevelDictionary,
Expand Down Expand Up @@ -154,16 +155,19 @@ impl Default for LocalDirectory {
fn default() -> Self {
let db = Connection::open(LOCAL_DB_PATH).unwrap();

let output = Self {
let mut output = Self {
tags: TagDictionary::new(&db),
users: UserDictionary::new(&db),
permission_levels: PermissionLevelDictionary::new(&db),
db,
cached_games: Vec::new(),
cache_dirty: true,
};

upsert_games_table(&output.db);

output.sync_games_cache();

output
}
}

0 comments on commit 1e05f37

Please sign in to comment.