Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Really optimise activation speed #121

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ module.exports = {
},

fetchRevisions: function(context) {
if (this.readConfig('disableFetchRevisions')) {
this.log('fetchRevisions - not fetching any revisions', { verbose: true });
return {
activations: []
};
}

return this._list(context)
.then(function(revisions) {
return {
Expand All @@ -116,6 +123,12 @@ module.exports = {
},

fetchInitialRevisions: function(context) {

if (this.readConfig('fetchOnlyRelevantRevisions')) {
this.log('fetchInitialRevisions - fetching only enough revisions to find the active revision', { verbose: true });
return this._fetchActiveRevision();
}

return this._list(context)
.then(function(revisions) {
return {
Expand All @@ -124,6 +137,24 @@ module.exports = {
});
},

_fetchActiveRevision: function() {
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
var filePattern = this.readConfig('filePattern');

var options = {
bucket: bucket,
prefix: prefix,
filePattern: filePattern,
};

var s3 = new this.S3({ plugin: this });
return s3.getActiveRevision(options)
.then((activeRevision) => {
return { initialRevisions: [activeRevision] };
});
},

_list: function(/* context */) {
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
Expand Down
43 changes: 37 additions & 6 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ module.exports = CoreObject.extend({
.then((response) => response.Contents.find((element) => element.Key === revisionPrefix));
},

getActiveRevision(options) {
var client = this._client;
var bucket = options.bucket;
var prefix = options.prefix;
var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":");
var indexKey = joinUriSegments(prefix, options.filePattern);

return headObject(client, { Bucket: bucket, Key: indexKey })
.then((current) => {
if (current === undefined) {
return;
}

return this.listAllObjects(
{ Bucket: bucket, Prefix: revisionPrefix },
(element) => element.ETag === current.ETag
)
.then((elements) => {
let activeRevision = elements.find((element) => element.ETag === current.ETag);
if (!activeRevision) {
return;
}

var revision = activeRevision.Key.substr(revisionPrefix.length);
return { active: true, revision, timestamp: activeRevision.LastModified };
})
});

},

fetchRevisions: function(options) {
var client = this._client;
var bucket = options.bucket;
Expand All @@ -174,7 +204,7 @@ module.exports = CoreObject.extend({
});
},

listAllObjects: function(options) {
listAllObjects: function(options, until) {
var client = this._client;
var listObjects = RSVP.denodeify(client.listObjects.bind(client));
var allRevisions = [];
Expand All @@ -183,13 +213,14 @@ module.exports = CoreObject.extend({
return listObjects(options).then(function(response) {
[].push.apply(allRevisions, response.Contents);

if (response.IsTruncated) {
var nextMarker = response.Contents[response.Contents.length - 1].Key;
options.Marker = nextMarker;
return listObjectRecursively(options);
} else {
var isComplete = !response.IsTruncated || (until && response.Contents.some(until));

if (isComplete) {
return allRevisions;
}
var nextMarker = response.Contents[response.Contents.length - 1].Key;
options.Marker = nextMarker;
return listObjectRecursively(options);
});
}

Expand Down