refactor to single bucket
This commit is contained in:
parent
452ccd068b
commit
b89bef6e89
7 changed files with 68 additions and 69 deletions
|
@ -5,15 +5,10 @@ const createRedisClient = require('./redis');
|
|||
|
||||
class DB {
|
||||
constructor(config) {
|
||||
const Storage =
|
||||
config.s3_buckets.length > 0 ? require('./s3') : require('./fs');
|
||||
const Storage = config.s3_bucket ? require('./s3') : require('./fs');
|
||||
this.log = mozlog('send.storage');
|
||||
|
||||
this.storage = [];
|
||||
|
||||
for (let i = 0; i < config.num_of_buckets; i++) {
|
||||
this.storage.push(new Storage(config, i, this.log));
|
||||
}
|
||||
this.storage = new Storage(config, this.log);
|
||||
|
||||
this.redis = createRedisClient(config);
|
||||
this.redis.on('error', err => {
|
||||
|
@ -26,32 +21,33 @@ class DB {
|
|||
return Math.ceil(result) * 1000;
|
||||
}
|
||||
|
||||
async getBucket(id) {
|
||||
return this.redis.hgetAsync(id, 'bucket');
|
||||
async getPrefixedId(id) {
|
||||
const prefix = await this.redis.hgetAsync(id, 'prefix');
|
||||
return `${prefix}-${id}`;
|
||||
}
|
||||
|
||||
async length(id) {
|
||||
const bucket = await this.redis.hgetAsync(id, 'bucket');
|
||||
return this.storage[bucket].length(id);
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
return this.storage.length(filePath);
|
||||
}
|
||||
|
||||
async get(id) {
|
||||
const bucket = await this.redis.hgetAsync(id, 'bucket');
|
||||
return this.storage[bucket].getStream(id);
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
return this.storage.getStream(filePath);
|
||||
}
|
||||
|
||||
async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
|
||||
const bucketTimes = config.expire_times_seconds;
|
||||
let bucket = 0;
|
||||
while (bucket < config.num_of_buckets - 1) {
|
||||
if (expireSeconds <= bucketTimes[bucket]) {
|
||||
const expireTimes = config.expire_times_seconds;
|
||||
let i;
|
||||
for (i = 0; i < expireTimes.length - 1; i++) {
|
||||
if (expireSeconds <= expireTimes[i]) {
|
||||
break;
|
||||
}
|
||||
bucket++;
|
||||
}
|
||||
|
||||
await this.storage[bucket].set(id, file);
|
||||
this.redis.hset(id, 'bucket', bucket);
|
||||
const prefix = config.expire_prefixes[i];
|
||||
const filePath = `${prefix}-${id}`;
|
||||
await this.storage.set(filePath, file);
|
||||
this.redis.hset(id, 'prefix', prefix);
|
||||
this.redis.hmset(id, meta);
|
||||
this.redis.expire(id, expireSeconds);
|
||||
}
|
||||
|
@ -61,16 +57,14 @@ class DB {
|
|||
}
|
||||
|
||||
async del(id) {
|
||||
const bucket = await this.redis.hgetAsync(id, 'bucket');
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
this.storage.del(filePath);
|
||||
this.redis.del(id);
|
||||
this.storage[bucket].del(id);
|
||||
}
|
||||
|
||||
async ping() {
|
||||
await this.redis.pingAsync();
|
||||
for (const bucket of this.storage) {
|
||||
bucket.ping();
|
||||
}
|
||||
await this.storage.ping();
|
||||
}
|
||||
|
||||
async metadata(id) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue