refactored storage, style tweaks

This commit is contained in:
Danny Coates 2020-07-25 11:22:57 -07:00
parent 8fb770a4ea
commit 55df061567
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
8 changed files with 42 additions and 30 deletions

View file

@ -32,27 +32,31 @@ class DB {
return Math.ceil(result) * 1000;
}
async getPrefixedId(id) {
async getPrefixedInfo(id) {
const [prefix, dead, flagged] = await this.redis.hmgetAsync(
id,
'prefix',
'dead',
'flagged'
);
if (dead || flagged) {
throw new Error('id not available');
}
return `${prefix}-${id}`;
return {
filePath: `${prefix}-${id}`,
flagged,
dead
};
}
async length(id) {
const filePath = await this.getPrefixedId(id);
const { filePath } = await this.getPrefixedInfo(id);
return this.storage.length(filePath);
}
async get(id) {
const filePath = await this.getPrefixedId(id);
return this.storage.getStream(filePath);
const info = await this.getPrefixedInfo(id);
if (info.dead || info.flagged) {
throw new Error(info.flagged ? 'flagged' : 'dead');
}
return this.storage.getStream(info.filePath);
}
async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
@ -75,18 +79,19 @@ class DB {
this.redis.hincrby(id, key, increment);
}
kill(id) {
async kill(id) {
const { filePath } = await this.getPrefixedInfo(id);
this.storage.del(filePath);
this.redis.hset(id, 'dead', 1);
}
async flag(id, key) {
// this.redis.persist(id);
await this.kill(id);
this.redis.hmset(id, { flagged: 1, key });
this.redis.sadd('flagged', id);
}
async del(id) {
const filePath = await this.getPrefixedId(id);
const { filePath } = await this.getPrefixedInfo(id);
this.storage.del(filePath);
this.redis.del(id);
}