Skip to content

Commit

Permalink
Remove reserve id from StorageEngine
Browse files Browse the repository at this point in the history
Removed the reserve id from storage engine. Now adding an item to a
storage engine will return a promise that resolves with the id it
was stored under.

Change-Id: Id4d2c4c51a359593f1308aeb5fe580ab460ebeb8
  • Loading branch information
vaage committed Dec 12, 2017
1 parent b53f5a0 commit dff382b
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 337 deletions.
6 changes: 0 additions & 6 deletions externs/shaka/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ shakaExtern.StoredContent;

/**
* @typedef {{
* key: number,
* originalManifestUri: string,
* duration: number,
* size: number,
Expand All @@ -109,8 +108,6 @@ shakaExtern.StoredContent;
* appMetadata: Object
* }}
*
* @property {number} key
* The key that uniquely identifies the manifest.
* @property {string} originalManifestUri
* The URI that the manifest was originally loaded from.
* @property {number} duration
Expand Down Expand Up @@ -223,12 +220,9 @@ shakaExtern.SegmentDB;

/**
* @typedef {{
* key: number,
* data: ArrayBuffer
* }}
*
* @property {number} key
* A key that uniquely describes the segment.
* @property {ArrayBuffer} data
* The data contents of the segment.
*/
Expand Down
47 changes: 28 additions & 19 deletions lib/offline/db_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,22 @@ shaka.offline.DBEngine.prototype.forEachManifest = function(each) {


/** @override */
shaka.offline.DBEngine.prototype.insertManifest = function(value) {
shaka.offline.DBEngine.prototype.addManifest = function(value) {
/** @type {number} */
var key = this.nextManifestId_++;

return this.insert_(
shaka.offline.DBEngine.Store_.MANIFEST,
key,
value);
};


/** @override */
shaka.offline.DBEngine.prototype.updateManifest = function(key, value) {
return this.insert_(
shaka.offline.DBEngine.Store_.MANIFEST,
key,
value);
};

Expand All @@ -226,12 +239,6 @@ shaka.offline.DBEngine.prototype.removeManifests =
};


/** @override */
shaka.offline.DBEngine.prototype.reserveManifestId = function() {
return this.nextManifestId_++;
};


/** @override */
shaka.offline.DBEngine.prototype.getSegment = function(key) {
return this.get_(
Expand All @@ -249,9 +256,13 @@ shaka.offline.DBEngine.prototype.forEachSegment = function(each) {


/** @override */
shaka.offline.DBEngine.prototype.insertSegment = function(value) {
shaka.offline.DBEngine.prototype.addSegment = function(value) {
/** @type {number} */
var key = this.nextSegmentId_++;

return this.insert_(
shaka.offline.DBEngine.Store_.SEGMENT,
key,
value);
};

Expand All @@ -266,12 +277,6 @@ shaka.offline.DBEngine.prototype.removeSegments =
};


/** @override */
shaka.offline.DBEngine.prototype.reserveSegmentId = function() {
return this.nextSegmentId_++;
};


/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {number} key
Expand All @@ -295,7 +300,7 @@ shaka.offline.DBEngine.prototype.get_ = function(store, key) {

/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {function(T)} each
* @param {function(number, T)} each
* @return {!Promise}
* @template T
* @private
Expand All @@ -313,7 +318,7 @@ shaka.offline.DBEngine.prototype.forEach_ = function(store, each) {
request.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
each(cursor.value);
each(cursor.key, cursor.value);
cursor.continue();
}
};
Expand All @@ -323,20 +328,24 @@ shaka.offline.DBEngine.prototype.forEach_ = function(store, each) {

/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {number} key
* @param {T} value
* @return {!Promise}
* @return {!Promise<number>}
* @template T
* @private
*/
shaka.offline.DBEngine.prototype.insert_ = function(store, value) {
shaka.offline.DBEngine.prototype.insert_ = function(store, key, value) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var READ_WRITE = DBEngine.Mode_.READ_WRITE;

// TODO (vaage) : Replace this with auto key.
value['key'] = key;

return this.createTransaction_(store, READ_WRITE, function(store) {
store.put(value);
});
}).then(function() { return key; });
};


Expand Down
53 changes: 30 additions & 23 deletions lib/offline/download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ shaka.offline.DownloadManager = function(
* download(). This is used to cleanup in destroy().
* @private {!Array.<number>}
*/
this.storedSegments_ = [];
this.storedSegmentIds_ = [];

/** @private {shaka.offline.IStorageEngine} */
this.storageEngine_ = storageEngine;
Expand Down Expand Up @@ -91,7 +91,7 @@ shaka.offline.DownloadManager.prototype.followProgress = function(callback) {
* startByte: number,
* endByte: ?number,
* bandwidthSize: number,
* segmentId: number
* onStore: function(number)
* }}
*
* @property {!Array.<string>} uris
Expand All @@ -102,16 +102,16 @@ shaka.offline.DownloadManager.prototype.followProgress = function(callback) {
* The byte index the segment ends at, if present.
* @property {number} bandwidthSize
* The size of the segment as estimated by the bandwidth and segment duration.
* @property {number} segmentId
* The key that the segment should be saved under in storage.
* @property {function(number)} onStore
* A callback for when a segment as been added to the storage.
*/
shaka.offline.DownloadManager.Segment;


/** @override */
shaka.offline.DownloadManager.prototype.destroy = function() {
var storage = this.storageEngine_;
var segments = this.storedSegments_;
var segments = this.storedSegmentIds_;
var p = this.promise_ || Promise.resolve();

// Don't try to remove segments if there are none. That may trigger an error
Expand All @@ -123,7 +123,7 @@ shaka.offline.DownloadManager.prototype.destroy = function() {
// Don't destroy() storageEngine since it is owned by Storage.

this.segments_ = {};
this.storedSegments_ = [];
this.storedSegmentIds_ = [];
this.storageEngine_ = null;
this.netEngine_ = null;
this.retryParams_ = null;
Expand All @@ -139,19 +139,19 @@ shaka.offline.DownloadManager.prototype.destroy = function() {
* @param {string} type
* @param {!shaka.media.SegmentReference|!shaka.media.InitSegmentReference} ref
* @param {number} bandwidthSize
* @param {number} segmentId
* The data to store in the database with the data. The |data| field of this
* object will contain the downloaded data.
* @param {function(number)} onStore
* A callback for when the segment has been saved to storage. The parameter
* will be the id the segment was saved under.
*/
shaka.offline.DownloadManager.prototype.addSegment = function(
type, ref, bandwidthSize, segmentId) {
type, ref, bandwidthSize, onStore) {
this.segments_[type] = this.segments_[type] || [];
this.segments_[type].push({
uris: ref.getUris(),
startByte: ref.startByte,
endByte: ref.endByte,
bandwidthSize: bandwidthSize,
segmentId: segmentId
onStore: onStore
});
};

Expand All @@ -161,7 +161,7 @@ shaka.offline.DownloadManager.prototype.addSegment = function(
* manifest object.
*
* @param {shakaExtern.ManifestDB} manifest
* @return {!Promise}
* @return {!Promise<number>}
*/
shaka.offline.DownloadManager.prototype.downloadAndStore = function(manifest) {
var MapUtils = shaka.util.MapUtils;
Expand Down Expand Up @@ -196,9 +196,10 @@ shaka.offline.DownloadManager.prototype.downloadAndStore = function(manifest) {
this.segments_ = {};

this.promise_ = Promise.all(async).then(function() {
return this.storageEngine_.insertManifest(manifest);
}.bind(this)).then(function() {
this.storedSegments_ = [];
return this.storageEngine_.addManifest(manifest);
}.bind(this)).then(function(id) {
this.storedSegmentIds_ = [];
return id;
}.bind(this));
return this.promise_;
};
Expand Down Expand Up @@ -233,14 +234,14 @@ shaka.offline.DownloadManager.prototype.downloadSegment_ = function(segment) {
}
byteCount = response.data.byteLength;

this.storedSegments_.push(segment.segmentId);

return this.storageEngine_.insertSegment({
key: segment.segmentId,
/** @type {shakaExtern.SegmentDataDB} */
var segmentDb = {
data: response.data
});
};

return this.storageEngine_.addSegment(segmentDb);
}.bind(this))
.then(function() {
.then(function(id) {
if (!this.manifest_) {
return Promise.reject(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand All @@ -251,6 +252,9 @@ shaka.offline.DownloadManager.prototype.downloadSegment_ = function(segment) {
this.manifest_.size += byteCount;

this.markAsDone_(segment);
this.storedSegmentIds_.push(id);
segment.onStore(id);

this.updateProgress_();
}.bind(this));
};
Expand Down Expand Up @@ -296,7 +300,10 @@ shaka.offline.DownloadManager.prototype.updateProgress_ = function() {
0 :
(this.downloadActual_ / this.downloadExpected_);

/** @type {number} */
var size = this.manifest_.size;

this.progressListeners_.forEach(function(listener) {
listener(progress, this.manifest_.size);
}.bind(this));
listener(progress, size);
});
};
40 changes: 17 additions & 23 deletions lib/offline/i_storage_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,29 @@ shaka.offline.IStorageEngine.prototype.getManifest;
/**
* Iterate over all the manifests in storage.
*
* @param {function(shakaExtern.ManifestDB)} each
* @param {function(number, shakaExtern.ManifestDB)} each
* @return {!Promise}
*/
shaka.offline.IStorageEngine.prototype.forEachManifest;


/**
* Insert or update a manifest in storage.
* Add a manifest to storage.
*
* @param {shakaExtern.ManifestDB} value
* @return {!Promise.<number>}
*/
shaka.offline.IStorageEngine.prototype.addManifest;


/**
* Update a manifest already in storage.
*
* @param {number} key
* @param {shakaExtern.ManifestDB} value
* @return {!Promise}
*/
shaka.offline.IStorageEngine.prototype.insertManifest;
shaka.offline.IStorageEngine.prototype.updateManifest;


/**
Expand All @@ -69,14 +79,6 @@ shaka.offline.IStorageEngine.prototype.insertManifest;
shaka.offline.IStorageEngine.prototype.removeManifests;


/**
* Reserve an id for a new manifest.
*
* @return {number}
*/
shaka.offline.IStorageEngine.prototype.reserveManifestId;


/**
* Get a single segment from storage using the key associated
* to the segment.
Expand All @@ -90,19 +92,19 @@ shaka.offline.IStorageEngine.prototype.getSegment;
/**
* Iterate over all the segments in storage.
*
* @param {function(shakaExtern.SegmentDataDB)} each
* @param {function(number, shakaExtern.SegmentDataDB)} each
* @return {!Promise}
*/
shaka.offline.IStorageEngine.prototype.forEachSegment;


/**
* Insert or update a segment in storage.
* Add a segment to storage.
*
* @param {shakaExtern.SegmentDataDB} value
* @return {!Promise}
* @return {!Promise.<number>}
*/
shaka.offline.IStorageEngine.prototype.insertSegment;
shaka.offline.IStorageEngine.prototype.addSegment;


/**
Expand All @@ -116,11 +118,3 @@ shaka.offline.IStorageEngine.prototype.insertSegment;
* @return {!Promise}
*/
shaka.offline.IStorageEngine.prototype.removeSegments;


/**
* Reserve an id for a new segment.
*
* @return {number}
*/
shaka.offline.IStorageEngine.prototype.reserveSegmentId;
2 changes: 1 addition & 1 deletion lib/offline/offline_manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ shaka.offline.OfflineManifestParser.prototype.onExpirationUpdated = function(
manifest.expiration > expiration) {
shaka.log.debug('Updating expiration for stored content');
manifest.expiration = expiration;
return storageEngine.insertManifest(manifest);
return storageEngine.updateManifest(manifestId, manifest);
}
})
.catch(function(error) {
Expand Down
Loading

0 comments on commit dff382b

Please sign in to comment.