Make process_scrobbles vars more readable, and change hashing
This commit is contained in:
parent
1e4a271b8d
commit
a8a4ed27c4
1 changed files with 56 additions and 51 deletions
|
|
@ -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
|
// Probably want to include artist name here, but not sure how to yet
|
||||||
|
|
||||||
const track_artist_count = item.getT(.array, "artists_track").?.count();
|
const track_artists = item.getT(.array, "artists_track").?.items();
|
||||||
const album_artist_count = item.getT(.array, "artists_album").?.count();
|
const album_artists = item.getT(.array, "artists_album").?.items();
|
||||||
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_name_buffer = try allocator.alloc([]const u8, track_artists.len);
|
||||||
var track_artist_id_buffer = try allocator.alloc(i32, track_artist_count);
|
var album_artist_name_buffer = try allocator.alloc([]const u8, album_artists.len);
|
||||||
var album_artist_id_buffer = try allocator.alloc(i32, album_artist_count);
|
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 = .{
|
const scrobble: Data.Scrobble = .{
|
||||||
.track = item.getT(.string, "track").?,
|
.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").?)),
|
.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);
|
const artist_name = try artist.coerce([]const u8);
|
||||||
track_artist_name_buffer[i] = artist_name;
|
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);
|
const artist_name = try artist.coerce([]const u8);
|
||||||
album_artist_name_buffer[i] = artist_name;
|
album_artist_name_buffer[i] = artist_name;
|
||||||
album_artist_id_buffer[i] = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(artist_name)));
|
album_artist_id_buffer[i] = @as(i64, @bitCast(std.hash.Fnv1a_32.hash(artist_name)));
|
||||||
try id_prehash.appendSlice(artist_name);
|
try album_hash_string.appendSlice(artist_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
try id_prehash.appendSlice(scrobble.album);
|
try album_hash_string.appendSlice(scrobble.album);
|
||||||
const album_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(id_prehash.items)));
|
try track_hash_string.appendSlice(scrobble.album);
|
||||||
try id_prehash.appendSlice(scrobble.track);
|
const album_hash = @as(i64, @bitCast(std.hash.Fnv1a_32.hash(album_hash_string.items)));
|
||||||
const song_id = @as(i32, @bitCast(std.hash.Fnv1a_32.hash(id_prehash.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)
|
var albumsong_id = try jetzig.database.Query(.Albumsong)
|
||||||
.findBy(.{ .album_id = album_id, .song_id = song_id })
|
.find(album_hash ^ track_hash)
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
var ins_album = try jetzig.database.Query(.Album)
|
var album_id = try jetzig.database.Query(.Album)
|
||||||
.find(album_id)
|
.find(album_hash)
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
for (track_artist_name_buffer, track_artist_id_buffer) |artist_name, artist_id| {
|
for (track_artist_name_buffer, track_artist_id_buffer) |scrobble_track_artist, track_artist_hash| {
|
||||||
var ins_artist = try jetzig.database.Query(.Artist)
|
var artist_id = try jetzig.database.Query(.Artist)
|
||||||
.find(artist_id)
|
.find(track_artist_hash)
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (ins_artist == null)
|
if (artist_id == null)
|
||||||
ins_artist = try jetzig.database.Query(.Artist)
|
artist_id = try jetzig.database.Query(.Artist)
|
||||||
.insert(.{ .id = artist_id, .name = artist_name, .disambiguation = null })
|
.insert(.{ .id = track_artist_hash, .name = scrobble_track_artist, .disambiguation = null })
|
||||||
.returning(.{.id}).execute(env.repo);
|
.returning(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (albumsong == null) {
|
if (albumsong_id == null) {
|
||||||
var ins_song = try jetzig.database.Query(.Song)
|
var track_id = try jetzig.database.Query(.Song)
|
||||||
.find(song_id)
|
.find(track_hash)
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (ins_song == null)
|
if (track_id == null)
|
||||||
ins_song = try jetzig.database.Query(.Song)
|
track_id = try jetzig.database.Query(.Song)
|
||||||
.insert(.{ .id = song_id, .name = scrobble.track, .length = null, .hidden = false })
|
.insert(.{ .id = track_hash, .name = scrobble.track, .length = null, .hidden = false })
|
||||||
.returning(.{.id}).execute(env.repo);
|
.returning(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (ins_album == null)
|
if (album_id == null)
|
||||||
ins_album = try jetzig.database.Query(.Album)
|
album_id = try jetzig.database.Query(.Album)
|
||||||
.insert(.{ .id = album_id, .name = scrobble.album, .length = null })
|
.insert(.{ .id = album_hash, .name = scrobble.album, .length = null })
|
||||||
.returning(.{.id}).execute(env.repo);
|
.returning(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
albumsong = try jetzig.database.Query(.Albumsong)
|
albumsong_id = try jetzig.database.Query(.Albumsong)
|
||||||
.insert(.{ .song_id = ins_song.?.id, .album_id = ins_album.?.id })
|
.insert(.{ .song_id = track_id.?.id, .album_id = album_id.?.id })
|
||||||
.returning(.{.id}).execute(env.repo);
|
.returning(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
try jetzig.database.Query(.Albumsongsartist)
|
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 {
|
} else {
|
||||||
const ins_albumsongartist = try jetzig.database.Query(.Albumsongsartist)
|
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);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (ins_albumsongartist == null)
|
if (ins_albumsongartist == null)
|
||||||
try jetzig.database.Query(.Albumsongsartist)
|
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)
|
for (album_artist_name_buffer, album_artist_id_buffer) |scrobble_album_artist, album_artist_hash| {
|
||||||
.findBy(.{ .album_id = ins_album.?.id, .artist_id = artist_id })
|
const artistalbum_id = try jetzig.database.Query(.Artistalbum)
|
||||||
|
.findBy(.{ .album_id = album_id.?.id, .artist_id = album_artist_hash })
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
|
|
||||||
if (ins_artistalbum == null) {
|
if (artistalbum_id == null) {
|
||||||
var ins_artist = try jetzig.database.Query(.Artist)
|
var artist_id = try jetzig.database.Query(.Artist)
|
||||||
.find(artist_id)
|
.find(album_artist_hash)
|
||||||
.select(.{.id}).execute(env.repo);
|
.select(.{.id}).execute(env.repo);
|
||||||
if (ins_artist == null)
|
if (artist_id == null)
|
||||||
ins_artist = try jetzig.database.Query(.Artist)
|
artist_id = try jetzig.database.Query(.Artist)
|
||||||
.insert(.{ .id = artist_id, .name = artist_name, .disambiguation = null })
|
.insert(.{ .id = artist_id, .name = scrobble_album_artist, .disambiguation = null })
|
||||||
.returning(.{.id}).execute(env.repo);
|
.returning(.{.id}).execute(env.repo);
|
||||||
try jetzig.database.Query(.Artistalbum)
|
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)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue