diff --git a/src/app/jobs/process_scrobbles.zig b/src/app/jobs/process_scrobbles.zig index dcf7f1f..ea743b9 100644 --- a/src/app/jobs/process_scrobbles.zig +++ b/src/app/jobs/process_scrobbles.zig @@ -20,12 +20,13 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig // Probably want to include artist name here, but not sure how to yet - const track_artist_count = item.getT(.array, "artists_track").?.count(); - const album_artist_count = item.getT(.array, "artists_album").?.count(); - var track_artist_name_buffer = try allocator.alloc([]const u8, track_artist_count); - var album_artist_name_buffer = try allocator.alloc([]const u8, album_artist_count); - var track_artist_id_buffer = try allocator.alloc(i32, track_artist_count); - var album_artist_id_buffer = try allocator.alloc(i32, album_artist_count); + const track_artists = item.getT(.array, "artists_track").?.items(); + const album_artists = item.getT(.array, "artists_album").?.items(); + + var track_artist_name_buffer = try allocator.alloc([]const u8, track_artists.len); + var album_artist_name_buffer = try allocator.alloc([]const u8, album_artists.len); + var track_artist_id_buffer = try allocator.alloc(i64, track_artists.len); + var album_artist_id_buffer = try allocator.alloc(i64, album_artists.len); const scrobble: Data.Scrobble = .{ .track = item.getT(.string, "track").?, @@ -35,95 +36,99 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig .date = @as(i64, @truncate(item.getT(.integer, "date").?)), }; - var id_prehash = std.ArrayList(u8).init(allocator); + var album_hash_string = std.ArrayList(u8).init(allocator); + var track_hash_string = std.ArrayList(u8).init(allocator); - for (item.getT(.array, "artists_track").?.items(), 0..track_artist_count) |artist, i| { + // I theoretically don't need this for loop + for (track_artists, 0..track_artists.len) |artist, i| { const artist_name = try artist.coerce([]const u8); track_artist_name_buffer[i] = artist_name; - track_artist_id_buffer[i] = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(artist_name))); + track_artist_id_buffer[i] = @as(i64, @bitCast(std.hash.Fnv1a_64.hash(artist_name))); } - for (item.getT(.array, "artists_album").?.items(), 0..album_artist_count) |artist, i| { + for (album_artists, 0..album_artists.len) |artist, i| { const artist_name = try artist.coerce([]const u8); album_artist_name_buffer[i] = artist_name; - album_artist_id_buffer[i] = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(artist_name))); - try id_prehash.appendSlice(artist_name); + album_artist_id_buffer[i] = @as(i64, @bitCast(std.hash.Fnv1a_32.hash(artist_name))); + try album_hash_string.appendSlice(artist_name); } - try id_prehash.appendSlice(scrobble.album); - const album_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(id_prehash.items))); - try id_prehash.appendSlice(scrobble.track); - const song_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(id_prehash.items))); + try album_hash_string.appendSlice(scrobble.album); + try track_hash_string.appendSlice(scrobble.album); + const album_hash = @as(i64, @bitCast(std.hash.Fnv1a_32.hash(album_hash_string.items))); + try track_hash_string.appendSlice(scrobble.track); + const track_hash = @as(i64, @bitCast(std.hash.Fnv1a_32.hash(track_hash_string.items))); - var albumsong = try jetzig.database.Query(.Albumsong) - .findBy(.{ .album_id = album_id, .song_id = song_id }) + var albumsong_id = try jetzig.database.Query(.Albumsong) + .find(album_hash ^ track_hash) .select(.{.id}).execute(env.repo); - var ins_album = try jetzig.database.Query(.Album) - .find(album_id) + var album_id = try jetzig.database.Query(.Album) + .find(album_hash) .select(.{.id}).execute(env.repo); - for (track_artist_name_buffer, track_artist_id_buffer) |artist_name, artist_id| { - var ins_artist = try jetzig.database.Query(.Artist) - .find(artist_id) + for (track_artist_name_buffer, track_artist_id_buffer) |scrobble_track_artist, track_artist_hash| { + var artist_id = try jetzig.database.Query(.Artist) + .find(track_artist_hash) .select(.{.id}).execute(env.repo); - if (ins_artist == null) - ins_artist = try jetzig.database.Query(.Artist) - .insert(.{ .id = artist_id, .name = artist_name, .disambiguation = null }) + if (artist_id == null) + artist_id = try jetzig.database.Query(.Artist) + .insert(.{ .id = track_artist_hash, .name = scrobble_track_artist, .disambiguation = null }) .returning(.{.id}).execute(env.repo); - if (albumsong == null) { - var ins_song = try jetzig.database.Query(.Song) - .find(song_id) + if (albumsong_id == null) { + var track_id = try jetzig.database.Query(.Song) + .find(track_hash) .select(.{.id}).execute(env.repo); - if (ins_song == null) - ins_song = try jetzig.database.Query(.Song) - .insert(.{ .id = song_id, .name = scrobble.track, .length = null, .hidden = false }) + if (track_id == null) + track_id = try jetzig.database.Query(.Song) + .insert(.{ .id = track_hash, .name = scrobble.track, .length = null, .hidden = false }) .returning(.{.id}).execute(env.repo); - if (ins_album == null) - ins_album = try jetzig.database.Query(.Album) - .insert(.{ .id = album_id, .name = scrobble.album, .length = null }) + if (album_id == null) + album_id = try jetzig.database.Query(.Album) + .insert(.{ .id = album_hash, .name = scrobble.album, .length = null }) .returning(.{.id}).execute(env.repo); - albumsong = try jetzig.database.Query(.Albumsong) - .insert(.{ .song_id = ins_song.?.id, .album_id = ins_album.?.id }) + albumsong_id = try jetzig.database.Query(.Albumsong) + .insert(.{ .song_id = track_id.?.id, .album_id = album_id.?.id }) .returning(.{.id}).execute(env.repo); try jetzig.database.Query(.Albumsongsartist) - .insert(.{ .albumsong_id = albumsong.?.id, .artist_id = ins_artist.?.id }).execute(env.repo); + .insert(.{ .albumsong_id = albumsong_id.?.id, .artist_id = artist_id.?.id }).execute(env.repo); } else { const ins_albumsongartist = try jetzig.database.Query(.Albumsongsartist) - .findBy(.{ .albumsong_id = albumsong.?.id, .artist_id = ins_artist.?.id }) + .findBy(.{ .albumsong_id = albumsong_id.?.id, .artist_id = artist_id.?.id }) .select(.{.id}).execute(env.repo); if (ins_albumsongartist == null) try jetzig.database.Query(.Albumsongsartist) - .insert(.{ .albumsong_id = albumsong.?.id, .artist_id = ins_artist.?.id }).execute(env.repo); + .insert(.{ .albumsong_id = albumsong_id.?.id, .artist_id = artist_id.?.id }).execute(env.repo); } } - for (album_artist_name_buffer, album_artist_id_buffer) |artist_name, artist_id| { - const ins_artistalbum = try jetzig.database.Query(.Artistalbum) - .findBy(.{ .album_id = ins_album.?.id, .artist_id = artist_id }) + + for (album_artist_name_buffer, album_artist_id_buffer) |scrobble_album_artist, album_artist_hash| { + const artistalbum_id = try jetzig.database.Query(.Artistalbum) + .findBy(.{ .album_id = album_id.?.id, .artist_id = album_artist_hash }) .select(.{.id}).execute(env.repo); - if (ins_artistalbum == null) { - var ins_artist = try jetzig.database.Query(.Artist) - .find(artist_id) + if (artistalbum_id == null) { + var artist_id = try jetzig.database.Query(.Artist) + .find(album_artist_hash) .select(.{.id}).execute(env.repo); - if (ins_artist == null) - ins_artist = try jetzig.database.Query(.Artist) - .insert(.{ .id = artist_id, .name = artist_name, .disambiguation = null }) + if (artist_id == null) + artist_id = try jetzig.database.Query(.Artist) + .insert(.{ .id = artist_id, .name = scrobble_album_artist, .disambiguation = null }) .returning(.{.id}).execute(env.repo); try jetzig.database.Query(.Artistalbum) - .insert(.{ .album_id = ins_album.?.id, .artist_id = ins_artist.?.id }).execute(env.repo); + .insert(.{ .album_id = album_id.?.id, .artist_id = artist_id.?.id }).execute(env.repo); } } try jetzig.database.Query(.Scrobble) - .insert(.{ .albumsong = albumsong.?.id, .datetime = scrobble.date }).execute(env.repo); + .insert(.{ .albumsong = albumsong_id.?.id, .datetime = scrobble.date }).execute(env.repo); } } }