Compare commits

...

1 Commits

Author SHA1 Message Date
vrancurel 96f933da3e improvement: perform get bucket and object at once
Instead of doing 2 round trips we perform the 2 queries at once using an
aggregate.
2020-02-10 14:27:16 -08:00
1 changed files with 51 additions and 14 deletions

View File

@ -250,22 +250,51 @@ class MongoClientInterface {
} }
getBucketAndObject(bucketName, objName, params, log, cb) { getBucketAndObject(bucketName, objName, params, log, cb) {
this.getBucketAttributes(bucketName, log, (err, bucket) => { if (params && params.versionId) {
// eslint-disable-next-line
objName = formatVersionKey(objName, params.versionId);
}
const m = this.getCollection(METASTORE);
m.aggregate([
{ $match: { _id: bucketName } },
{
$lookup: {
from: bucketName,
pipeline: [
{ $match: { _id: objName } },
],
as: 'object',
},
},
], {}, (err, cursor) => {
if (err) { if (err) {
log.error( log.error(
'getBucketAttributes: error getting bucket attributes', 'getBucketAndObject: error getting bucket attributes',
{ error: err.message }); { error: err.message });
return cb(err); return cb(errors.InternalError);
} }
this.getObject(bucketName, objName, params, log, (err, obj) => { if (!cursor) {
if (err) { return cb(errors.NoSuchBucket);
if (err === errors.NoSuchKey) { }
cursor.on('data', doc => {
// FIXME: there should be a version of BucketInfo.deserialize()
// that properly inits w/o JSON.parse()
const bucketMDStr = JSON.stringify(doc.value);
const bucket = BucketInfo.deSerialize(bucketMDStr);
if (doc.object.length === 0) {
return cb(null, return cb(null,
{ bucket: { bucket:
BucketInfo.fromObj(bucket).serialize(), BucketInfo.fromObj(bucket).serialize(),
}); });
} }
log.error('getObject: error getting object', const obj = doc.object[0].value;
if (obj.isPHD) {
const c = this.getCollection(bucketName);
this.getLatestVersion(c, objName, log, (err, obj) => {
if (err) {
log.error(
'getLatestVersion: getting latest version',
{ error: err.message }); { error: err.message });
return cb(err); return cb(err);
} }
@ -275,6 +304,14 @@ class MongoClientInterface {
}); });
}); });
return undefined; return undefined;
}
MongoUtils.unserialize(obj);
return cb(null, {
bucket: BucketInfo.fromObj(bucket).serialize(),
obj: JSON.stringify(obj),
});
});
return undefined;
}); });
} }