Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Switched to using auto_increment integers for trackIDs / artistIDs, a…

…nd only supporting one code version per server
  • Loading branch information...
commit 7638150397bbf8348aa353421a2a6d17918fd2ff 1 parent b40d7f2
John Hurliman authored February 25, 2012
5  config.js
@@ -26,9 +26,8 @@ var settings = {
26 26
   // match valid
27 27
   code_threshold: 10,
28 28
   
29  
-  // Set this to false to disable code version checking and speed up queries
30  
-  // at the expense of safely supporting multiple codegen versions
31  
-  check_codever: true
  29
+  // Supported version of echoprint-codegen codes
  30
+  codever: '4.12'
32 31
 };
33 32
 
34 33
 // Override default settings with any local settings
5  config.local.js.orig
@@ -24,7 +24,6 @@ module.exports = {
24 24
   // match valid
25 25
   code_threshold: 10,
26 26
   
27  
-  // Set this to false to disable code version checking and speed up queries
28  
-  // at the expense of safely supporting multiple codegen versions
29  
-  check_codever: true
  27
+  // Supported version of echoprint-codegen codes
  28
+  codever: '4.12'
30 29
 };
7  controllers/api.js
@@ -14,7 +14,7 @@ exports.query = function(req, res) {
14 14
     return server.respond(req, res, 500, { error: 'Missing code' });
15 15
   
16 16
   var codeVer = url.query.version;
17  
-  if (!codeVer || codeVer.length !== 4)
  17
+  if (codeVer != config.codever)
18 18
     return server.respond(req, res, 500, { error: 'Missing or invalid version' });
19 19
   
20 20
   fingerprinter.decodeCodeString(code, function(err, fp) {
@@ -51,9 +51,12 @@ exports.ingest = function(req, res) {
51 51
   var length = req.body.length;
52 52
   var artist = req.body.artist;
53 53
   
54  
-  if (!code || !codeVer || codeVer.length !== 4 || isNaN(parseInt(length, 10)))
  54
+  if (!code || !codeVer || isNaN(parseInt(length, 10)))
55 55
     return server.respond(req, res, 500, { error: 'Missing or invalid required fields' });
56 56
   
  57
+  if (codeVer != config.codever)
  58
+    return server.respond(req, res, 500, { error: 'Invalid version' });
  59
+  
57 60
   fingerprinter.decodeCodeString(code, function(err, fp) {
58 61
     if (err || !fp.codes.length) {
59 62
       log.error('Failed to decode codes for ingest: ' + err);
41  controllers/fingerprinter.js
... ...
@@ -1,4 +1,3 @@
1  
-var spawn = require('child_process').spawn;
2 1
 var zlib = require('zlib');
3 2
 var log = require('winston');
4 3
 var Mutex = require('../mutex');
@@ -208,7 +207,6 @@ function getTrackMetadata(match, allMatches, status, callback) {
208 207
     if (!track)
209 208
       return callback('Track ' + match.track_id + ' went missing', null);
210 209
     
211  
-    match.codever = track.codever;
212 210
     match.track = track.name;
213 211
     match.artist = track.artist_name;
214 212
     match.artist_id = track.artist_id;
@@ -305,8 +303,7 @@ function ingest(fp, callback) {
305 303
   var MAX_DURATION = 60 * 10;
306 304
   
307 305
   log.info('Ingesting track "' + fp.track + '" by artist "' + fp.artist +
308  
-    '", ' + fp.length + ' seconds, ' + fp.codes.length + ' codes (v' +
309  
-    fp.codever + ')');
  306
+    '", ' + fp.length + ' seconds, ' + fp.codes.length + ' codes');
310 307
   
311 308
   if (!fp.codes.length || typeof fp.length !== 'number' || !fp.codever)
312 309
     return callback('Missing required track fields', null);
@@ -418,25 +415,22 @@ function ingest(fp, callback) {
418 415
       
419 416
       // Function for creating a new artist and new track
420 417
       function createArtistAndTrack() {
421  
-        var artistID = newArtistID();
422  
-        log.info('Creating artist ' + artistID + ' ("' + fp.artist + '")');
423  
-        
424  
-        database.addArtist(artistID, fp.artist, function(err) {
  418
+        database.addArtist(fp.artist, function(err, artistID) {
425 419
           if (err) { gMutex.release(); return callback(err, null); }
  420
+          
  421
+          // Success
  422
+          log.info('Created artist ' + artistID + ' ("' + fp.artist + '")');
426 423
           createTrack(artistID, fp.artist);
427 424
         });
428 425
       }
429 426
       
430 427
       // Function for creating a new track given an artistID
431 428
       function createTrack(artistID, artist) {
432  
-        var trackID = newTrackID();
433  
-        log.info('Creating track ' + trackID + ' ("' + fp.track + '")');
434  
-        
435  
-        database.addTrack(trackID, artistID, fp, function(err) {
  429
+        database.addTrack(artistID, fp, function(err, trackID) {
436 430
           if (err) { gMutex.release(); return callback(err, null); }
437 431
           
438 432
           // Success
439  
-          log.info('Track insert complete');
  433
+          log.info('Created track ' + trackID + ' ("' + fp.track + '")');
440 434
           gMutex.release();
441 435
           callback(null, { track_id: trackID, track: fp.track,
442 436
             artist_id: artistID, artist: artist });
@@ -445,24 +439,3 @@ function ingest(fp, callback) {
445 439
     });
446 440
   });
447 441
 }
448  
-
449  
-/**
450  
- * Generates a 16-character track identifier.
451  
- */
452  
-function newTrackID() {
453  
-  return 'TR' + newID();
454  
-}
455  
-
456  
-/**
457  
- * Generates a 16-character artist identifier.
458  
- */
459  
-function newArtistID() {
460  
-  return 'AR' + newID();
461  
-}
462  
-
463  
-function newID() {
464  
-  var id = '';
465  
-  for(var i = 0; i < 6; i++)
466  
-    id += CHARACTERS.charAt(Math.floor(Math.random() * CHARACTERS.length));
467  
-  return id + (gTimestamp++).toString(36).substr(-8).toUpperCase();
468  
-}
61  models/mysql.js
@@ -33,29 +33,14 @@ var client = mysql.createClient({
33 33
 function fpQuery(fp, rows, callback) {
34 34
   var fpCodesStr = fp.codes.join(',');
35 35
   
36  
-  var sql, args;
37  
-  if (config.check_codever) {
38  
-    sql = 'SELECT track_id,COUNT(track_id) AS score ' +
39  
-      'FROM codes,tracks ' +
40  
-      'WHERE code IN (' + fpCodesStr + ') ' +
41  
-      'AND id=track_id ' +
42  
-      'AND codever=?' +
43  
-      'GROUP BY track_id ' +
44  
-      'ORDER BY score DESC ' +
45  
-      'LIMIT ' + rows;
46  
-    args = [fp.codever];
47  
-  } else {
48  
-    sql = 'SELECT track_id,COUNT(track_id) AS score ' +
49  
-      'FROM codes ' +
50  
-      'WHERE code IN (' + fpCodesStr + ') ' +
51  
-      'GROUP BY track_id ' +
52  
-      'ORDER BY score DESC ' +
53  
-      'LIMIT ' + rows;
54  
-    args = [];
55  
-  }
56  
-  
57 36
   // Get the top N matching tracks sorted by score (number of matched codes)
58  
-  client.query(sql, args, function(err, matches) {
  37
+  var sql = 'SELECT track_id,COUNT(track_id) AS score ' +
  38
+    'FROM codes ' +
  39
+    'WHERE code IN (' + fpCodesStr + ') ' +
  40
+    'GROUP BY track_id ' +
  41
+    'ORDER BY score DESC ' +
  42
+    'LIMIT ' + rows;
  43
+  client.query(sql, [], function(err, matches) {
59 44
     if (err) return callback(err, null);
60 45
     if (!matches) return callback(null, []);
61 46
     
@@ -151,7 +136,7 @@ function getArtistByName(artistName, callback) {
151 136
   });
152 137
 }
153 138
 
154  
-function addTrack(trackID, artistID, fp, callback) {
  139
+function addTrack(artistID, fp, callback) {
155 140
   var length = fp.length;
156 141
   if (typeof length === 'string')
157 142
     length = parseInt(length, 10);
@@ -159,25 +144,26 @@ function addTrack(trackID, artistID, fp, callback) {
159 144
   // Sanity checks
160 145
   if (!trackID || trackID.length !== 16 ||
161 146
       !artistID || artistID.length !== 16 ||
162  
-      !fp.codever || fp.codever.length !== 4 ||
163 147
       isNaN(length))
164 148
   {
165  
-    return callback('Attempted to add track with missing fields');
  149
+    return callback('Attempted to add track with missing fields', null);
166 150
   }
167 151
   
168 152
   var sql = 'INSERT INTO tracks ' +
169  
-    '(id,codever,name,artist_id,length,import_date) ' +
170  
-    'VALUES (?,?,?,?,?,?)';
171  
-  client.query(sql, [trackID, fp.codever, fp.track, artistID, length,
172  
-    new Date()], function(err, info)
  153
+    '(name,artist_id,length,import_date) ' +
  154
+    'VALUES (?,?,?,?)';
  155
+  client.query(sql, [fp.track, artistID, length, new Date()],
  156
+    function(err, info)
173 157
   {
174  
-    if (err) return callback(err);
175  
-    if (info.affectedRows !== 1) return callback('Track insert failed');
  158
+    if (err) return callback(err, null);
  159
+    if (info.affectedRows !== 1) return callback('Track insert failed', null);
  160
+    
  161
+    var trackID = info.insertId;
176 162
     
177 163
     // Write out the codes to a file for bulk insertion into MySQL
178 164
     var tempName = temp.path({ prefix: 'echoprint-' + trackID, suffix: '.csv' });
179 165
     writeCodesToFile(tempName, fp, trackID, function(err) {
180  
-      if (err) return callback(err);
  166
+      if (err) return callback(err, null);
181 167
       
182 168
       // Bulk insert the codes
183 169
       sql = 'LOAD DATA INFILE ? IGNORE INTO TABLE codes';
@@ -185,7 +171,7 @@ function addTrack(trackID, artistID, fp, callback) {
185 171
         // Remove the temporary file
186 172
         fs.unlink(tempName, function(err2) {
187 173
           if (!err) err = err2;
188  
-          callback(err);
  174
+          callback(err, trackID);
189 175
         });
190 176
       });
191 177
     });
@@ -212,9 +198,12 @@ function writeCodesToFile(filename, fp, trackID, callback) {
212 198
   keepWriting();
213 199
 }
214 200
 
215  
-function addArtist(artistID, name, callback) {
216  
-  var sql = 'INSERT INTO artists (id,name) VALUES (?,?)';
217  
-  client.query(sql, [artistID, name], callback);
  201
+function addArtist(name, callback) {
  202
+  var sql = 'INSERT INTO artists (name) VALUES (?)';
  203
+  client.query(sql, [name], function(err, info) {
  204
+    if (err) return callback(err, null);
  205
+    callback(null, info.insertId);
  206
+  });
218 207
 }
219 208
 
220 209
 function updateTrack(trackID, name, artistID, callback) {

0 notes on commit 7638150

Please sign in to comment.
Something went wrong with that request. Please try again.