#include "trackname.h"
#include "queue.h"
#include "server-queue.h"
+#include "random.h"
+
+#define BASE_WEIGHT 90000
static DB_TXN *global_tid;
struct kvp *prefs) {
const char *s;
char **track_tags;
- time_t last, now;
+ time_t last, now = time(0);
/* Reject tracks not in any collection (race between edit config and
* rescan) */
/* Reject tracks played within the last 8 hours */
if((s = kvp_get(prefs, "played_time"))) {
last = atoll(s);
- now = time(0);
if(now < last + config->replay_min)
return 0;
}
if((errno == 0 || errno == ERANGE) && n >= 0)
return n;
}
-
- return 90000;
-}
-static unsigned char random_buffer[4096];
-static size_t random_left;
-
-/** @brief Fill [buf, buf+n) with random bytes */
-static void random_bytes(unsigned char *buf, size_t n) {
- while(n > 0) {
- if(random_left > 0) {
- const size_t this_time = n > random_left ? random_left : n;
-
- memcpy(buf, random_buffer + random_left - this_time, this_time);
- n -= this_time;
- random_left -= this_time;
- } else {
- static int fd = -1;
- int r;
-
- if(fd < 0) {
- if((fd = open("/dev/urandom", O_RDONLY)) < 0)
- fatal(errno, "opening /dev/urandom");
- }
- if((r = read(fd, random_buffer, sizeof random_buffer)) < 0)
- fatal(errno, "reading /dev/urandom");
- if((size_t)r < sizeof random_buffer)
- fatal(0, "short read from /dev/urandom");
- random_left = sizeof random_buffer;
- }
+ /* Bias up tracks that were recently added */
+ if((s = kvp_get(data, "_noticed"))) {
+ const time_t noticed = atoll(s);
+
+ if(noticed + config->new_bias_age < now)
+ /* Currently we just step up the weight of tracks that are in range. A
+ * more sophisticated approach would be to linearly decay from new_bias
+ * down to BASE_WEIGHT over the course of the new_bias_age interval
+ * starting when the track is added. */
+ return config->new_bias;
}
+
+ return BASE_WEIGHT;
}
/** @brief Pick a random integer uniformly from [0, limit) */
do {
/* Actually get some random data. */
- random_bytes(buf, nby);
+ random_get(buf, nby);
/* Clobber the top byte. */
buf[0] &= m;