File size: 6,050 Bytes
1abfaca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 'use strict';
/**
* Key-value bucket handler
*/
const uuidv1 = require('uuid').v1;
const mwUtil = require('../lib/mwUtil');
const HyperSwitch = require('hyperswitch');
const stringify = require('fast-json-stable-stringify');
const HTTPError = HyperSwitch.HTTPError;
const URI = HyperSwitch.URI;
const spec = HyperSwitch.utils.loadSpec(`${__dirname}/key_value.yaml`);
class KVBucket {
createBucket(hyper, req) {
const schema = this.makeSchema(req.body || {});
schema.table = req.params.bucket;
const rp = req.params;
const storeRequest = {
uri: new URI([rp.domain, 'sys', 'table', rp.bucket]),
body: schema
};
return hyper.put(storeRequest);
}
makeSchema(opts) {
const schemaVersionMajor = 5;
return {
// Combine option & bucket version into a monotonically increasing
// combined schema version. By multiplying the bucket version by 1000,
// we increase the chance of catching a reset in the option version.
version: schemaVersionMajor * 1000 + (opts.version || 0),
options: {
compression: opts.compression || [
{
algorithm: 'deflate',
block_size: 256
}
],
updates: opts.updates || {
pattern: 'timeseries'
}
},
attributes: {
key: opts.keyType || 'string',
tid: 'timeuuid',
headers: 'json',
value: opts.valueType || 'blob'
},
index: [
{ attribute: 'key', type: 'hash' }
]
};
}
getRevision(hyper, req) {
if (mwUtil.isNoCacheRequest(req)) {
throw new HTTPError({ status: 404 });
}
const rp = req.params;
const storeReq = {
uri: new URI([rp.domain, 'sys', 'table', rp.bucket, '']),
body: {
table: rp.bucket,
attributes: {
key: rp.key
},
limit: 1
}
};
return hyper.get(storeReq).then((dbResult) => {
if (dbResult.body && dbResult.body.items && dbResult.body.items.length) {
const row = dbResult.body.items[0];
return {
status: 200,
headers: row.headers,
body: row.value
};
} else {
throw new HTTPError({
status: 404,
body: {
type: 'not_found',
uri: req.uri,
method: req.method
}
});
}
});
}
putRevision(hyper, req) {
if (mwUtil.isNoStoreRequest(req)) {
return { status: 202 };
}
delete req.headers['cache-control'];
const rp = req.params;
const tid = uuidv1();
// In case specific content-type to store is not provided,
// default to the request content type. Ideally this should not happen.
if (req.headers &&
req.headers['content-type'] &&
!req.headers['x-store-content-type']) {
hyper.logger.log('warn/kv', {
msg: 'No x-store-content-type provided. Defaulting to plain content-type'
});
req.headers['x-store-content-type'] = req.headers['content-type'];
}
const headersToStore = {};
Object.keys(req.headers)
.filter((headerName) => headerName.startsWith('x-store-'))
.forEach((headerName) => {
headersToStore[headerName.replace(/^x-store-/, '')] = req.headers[headerName];
});
const doPut = () => hyper.put({
uri: new URI([rp.domain, 'sys', 'table', rp.bucket, '']),
body: {
table: rp.bucket,
attributes: {
key: rp.key,
tid,
headers: headersToStore,
value: req.body
}
}
});
if (req.headers['if-none-hash-match']) {
delete req.headers['if-none-hash-match'];
return this.getRevision(hyper, req)
.then((oldContent) => {
// TODO: proper etag-based compare.
if (stringify(req.body) === stringify(oldContent.body) &&
(!headersToStore['content-type'] ||
headersToStore['content-type'] === oldContent.headers['content-type'])) {
hyper.metrics.makeMetric({
type: 'Counter',
name: 'unchanged_rev_render', // shared with lib/parsoid
prometheus: {
name: 'restbase_unchanged_rev_render_total',
help: 'unchanged rev render count'
},
labels: {
names: ['path', 'bucket'],
omitLabelNames: true
}
}).increment(1, ['sys_kv', req.params.bucket]);
return {
status: 412,
headers: oldContent.headers
};
}
throw new HTTPError({ status: 404 });
})
.catch({ status: 404 }, doPut);
} else {
return doPut();
}
}
}
module.exports = (options) => {
const kvBucket = new KVBucket(options);
return {
spec, // Re-export from spec module
operations: {
createBucket: kvBucket.createBucket.bind(kvBucket),
getRevision: kvBucket.getRevision.bind(kvBucket),
putRevision: kvBucket.putRevision.bind(kvBucket)
}
};
};
|