<commit_before>#include "Album.h"

/* constructors */
Album::Album(Locutus *locutus) {
	this->locutus = locutus;
	artist = new Artist(locutus);
	mbid = "";
	title = "";
	released = "";
	type = "";
}

/* destructors */
Album::~Album() {
	for (vector<Track *>::size_type t = 0; t < tracks.size(); ++t)
		delete tracks[t];
	delete artist;
}

/* methods */
bool Album::loadFromCache(const string &mbid) {
	/* fetch album from cache */
	if (locutus == NULL)
		return false;
	if (mbid.size() != 36 || mbid[8] != '-' || mbid[13] != '-' || mbid[18] != '-' || mbid[23] != '-') {
		string msg = "Unable to load album from cache. Illegal MusicBrainz ID: ";
		msg.append(mbid);
		locutus->debug(DEBUG_NOTICE, msg);
		return false;
	}
	ostringstream query;
	query << "SELECT * FROM v_album_lookup WHERE album_mbid = '" << locutus->database->escapeString(mbid) << "' AND last_updated + INTERVAL '" << locutus->album_cache_lifetime << " months' > now()";
	if (!locutus->database->query(query.str()) || locutus->database->getRows() <= 0) {
		/* album not in cache */
		string msg = "Unable to load album from cache. MusicBrainz ID not found or cache is too old: ";
		msg.append(mbid);
		locutus->debug(DEBUG_NOTICE, msg);
		return false;
	}
	/* artist data */
	artist->mbid = locutus->database->getString(0, 0);
	artist->name = locutus->database->getString(0, 1);
	artist->sortname = locutus->database->getString(0, 2);
	/* album data */
	this->mbid = locutus->database->getString(0, 3);
	type = locutus->database->getString(0, 4);
	title = locutus->database->getString(0, 5);
	released = locutus->database->getString(0, 6);
	/* track data */
	int trackcount = locutus->database->getRows();
	tracks.resize(trackcount, NULL);
	for (int t = 0; t < trackcount; ++t) {
		/* we could reduce memory usage here.
		 * we'll get the same artist stored <trackcount> times.
		 * since each artist entry usually eats about 64 bytes mem,
		 * it's hardly necessary to improve this, though */
		Artist *track_artist = new Artist(locutus);
		Track *track = new Track(locutus, this, track_artist);
		int trackindex = track->tracknumber - 1;
		if (trackindex >= (int) tracks.size()) {
			/* this really shouldn't happen.
			 * seemingly we're missing entries in the track table */
			string msg = "Tracknumber exceed album track count. Did you erase data in the track table? MusicBrainz Album ID: ";
			msg.append(mbid);
			locutus->debug(DEBUG_WARNING, msg);
			for (vector<Track *>::size_type t = 0; t < tracks.size(); ++t)
				delete tracks[t];
			return false;
		}
		/* track data */
		track->mbid = locutus->database->getString(t, 7);
		track->title = locutus->database->getString(t, 8);
		track->duration = locutus->database->getInt(t, 9);
		track->tracknumber = locutus->database->getInt(t, 10);
		/* track artist data */
		track_artist->mbid = locutus->database->getString(t, 11);
		track_artist->name = locutus->database->getString(t, 12);
		track_artist->sortname = locutus->database->getString(t, 13);
		tracks[trackindex] = track;
	}
	return true;
}

bool Album::retrieveFromWebService(const string &mbid) {
	/* album data */
	if (locutus == NULL)
		return false;
	XMLNode *root = locutus->webservice->lookupAlbum(mbid);
	if (root == NULL)
		return false;
	if (root->children["metadata"].size() <= 0)
		return false;
	XMLNode *album = root->children["metadata"][0]->children["release"][0];
	this->mbid = album->children["id"][0]->value;
	type = album->children["type"][0]->value;
	title = album->children["title"][0]->value;
	if (album->children["release-event-list"].size() > 0) {
		released = album->children["release-event-list"][0]->children["event"][0]->children["date"][0]->value;
		if (released.size() == 10 && released[4] == '-' && released[7] == '-') {
			/* ok as it is, probably a valid date */
		} else if (released.size() == 4) {
			/* no month & day, add 1st of january */
			released.append("-01-01");<commit_msg>another bugfix. tad tired, didn't notice that i used an unset value for trackindex (which then caused it to always be -1). also added a check that trackindex is not less than 0 (even though the database got a check for this already)<commit_after>#include "Album.h"

/* constructors */
Album::Album(Locutus *locutus) {
	this->locutus = locutus;
	artist = new Artist(locutus);
	mbid = "";
	title = "";
	released = "";
	type = "";
}

/* destructors */
Album::~Album() {
	for (vector<Track *>::size_type t = 0; t < tracks.size(); ++t)
		delete tracks[t];
	delete artist;
}

/* methods */
bool Album::loadFromCache(const string &mbid) {
	/* fetch album from cache */
	if (locutus == NULL)
		return false;
	if (mbid.size() != 36 || mbid[8] != '-' || mbid[13] != '-' || mbid[18] != '-' || mbid[23] != '-') {
		string msg = "Unable to load album from cache. Illegal MusicBrainz ID: ";
		msg.append(mbid);
		locutus->debug(DEBUG_NOTICE, msg);
		return false;
	}
	ostringstream query;
	query << "SELECT * FROM v_album_lookup WHERE album_mbid = '" << locutus->database->escapeString(mbid) << "' AND last_updated + INTERVAL '" << locutus->album_cache_lifetime << " months' > now()";
	if (!locutus->database->query(query.str()) || locutus->database->getRows() <= 0) {
		/* album not in cache */
		string msg = "Unable to load album from cache. MusicBrainz ID not found or cache is too old: ";
		msg.append(mbid);
		locutus->debug(DEBUG_NOTICE, msg);
		return false;
	}
	/* artist data */
	artist->mbid = locutus->database->getString(0, 0);
	artist->name = locutus->database->getString(0, 1);
	artist->sortname = locutus->database->getString(0, 2);
	/* album data */
	this->mbid = locutus->database->getString(0, 3);
	type = locutus->database->getString(0, 4);
	title = locutus->database->getString(0, 5);
	released = locutus->database->getString(0, 6);
	/* track data */
	int trackcount = locutus->database->getRows();
	tracks.resize(trackcount, NULL);
	for (int t = 0; t < trackcount; ++t) {
		/* we could reduce memory usage here.
		 * we'll get the same artist stored <trackcount> times.
		 * since each artist entry usually eats about 64 bytes mem,
		 * it's hardly necessary to improve this, though */
		Artist *track_artist = new Artist(locutus);
		Track *track = new Track(locutus, this, track_artist);
		int trackindex = locutus->database->getInt(t, 10) - 1;
		if (trackindex < 0 || trackindex >= (int) tracks.capacity()) {
			/* this really shouldn't happen.
			 * seemingly we're missing entries in the track table */
			string msg = "Tracknumber either exceed album track count or is less than 1. Did you erase data in the track table? MusicBrainz Album ID: ";
			msg.append(mbid);
			locutus->debug(DEBUG_WARNING, msg);
			/* need to clean up the tracks we've created */
			for (vector<Track *>::size_type t = 0; t < tracks.size(); ++t)
				delete tracks[t];
			delete track;
			return false;
		}
		/* track data */
		track->mbid = locutus->database->getString(t, 7);
		track->title = locutus->database->getString(t, 8);
		track->duration = locutus->database->getInt(t, 9);
		track->tracknumber = trackindex + 1;
		/* track artist data */
		track_artist->mbid = locutus->database->getString(t, 11);
		track_artist->name = locutus->database->getString(t, 12);
		track_artist->sortname = locutus->database->getString(t, 13);
		tracks[trackindex] = track;
	}
	return true;
}

bool Album::retrieveFromWebService(const string &mbid) {
	/* album data */
	if (locutus == NULL)
		return false;
	XMLNode *root = locutus->webservice->lookupAlbum(mbid);
	if (root == NULL)
		return false;
	if (root->children["metadata"].size() <= 0)
		return false;
	XMLNode *album = root->children["metadata"][0]->children["release"][0];
	this->mbid = album->children["id"][0]->value;
	type = album->children["type"][0]->value;
	title = album->children["title"][0]->value;
	if (album->children["release-event-list"].size() > 0) {
		released = album->children["release-event-list"][0]->children["event"][0]->children["date"][0]->value;
		if (released.size() == 10 && released[4] == '-' && released[7] == '-') {
			/* ok as it is, probably a valid date */
		} else if (released.size() == 4) {
			/* no month & day, add 1st of january */
			released.append("-01-01");
{'fraction_non_alphanumeric': 0.1786591355599214, 'fraction_numerical': 0.015594302554027505, 'mean_word_length': 4.049597024178549, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 154, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3256387', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2745, 'n_words': 732}