Skip to content

Commit

Permalink
Implement Memory Storage System
Browse files Browse the repository at this point in the history
Created a memory storage system that holds all data in main memory. This
system will be used in tests.

Needed to update the eslint rules to allow us to have an instance-level
method that does not use |this|.

Issue: shaka-project#1248
Change-Id: If1ebbec62bfdae57ee06df9fdef9955b30579d52
  • Loading branch information
vaage committed Mar 6, 2018
1 parent e62b935 commit d8a4b20
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ module.exports = {
// "Best practices" rules we should be able to pass, but are not part of "eslint:recommended": {{{
"accessor-pairs": "error",
"array-callback-return": "error",
"class-methods-use-this": "error",
"no-alert": "error",
"no-caller": "error",
"no-catch-shadow": "error",
Expand Down Expand Up @@ -117,6 +116,7 @@ module.exports = {
// }}}

// Style rules we don't need: {{{
"class-methods-use-this": "off", // causes issues when implementing an interface
"dot-notation": "off", // We use bracket notation in tests on purpose
"eqeqeq": "off", // Compiler handles type checking in advance
"guard-for-in": "off",
Expand Down
3 changes: 3 additions & 0 deletions build/types/offline
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
+../../lib/offline/db_engine.js
+../../lib/offline/download_manager.js
+../../lib/offline/i_storage_engine.js
+../../lib/offline/memory/storage_cell.js
+../../lib/offline/memory/storage_mechanism.js
+../../lib/offline/memory/storage_table.js
+../../lib/offline/offline_manifest_parser.js
+../../lib/offline/offline_scheme.js
+../../lib/offline/offline_uri.js
Expand Down
166 changes: 166 additions & 0 deletions lib/offline/memory/storage_cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.offline.memory.StorageCell');

goog.require('shaka.offline.memory.StorageTable');
goog.require('shaka.util.MapUtils');



/**
* @implements {shakaExtern.StorageCell}
*/
shaka.offline.memory.StorageCell = class {
constructor() {
/**
* @private {!shaka.offline.memory.StorageTable<shakaExtern.SegmentDataDB>}
*/
this.segments_ = new shaka.offline.memory.StorageTable();

/**
* @private {!shaka.offline.memory.StorageTable<shakaExtern.ManifestDB>}
*/
this.manifests_ = new shaka.offline.memory.StorageTable();
}


/**
* @override
*/
destroy() {
// Unlike other storage cells, there is no way to keep the data around
// after we destroy the cell.
this.segments_.clear();
this.manifests_.clear();

return Promise.resolve();
}


/**
* @override
*/
hasFixedKeySpace() {
// This cell will allow new segments and manifests to be added.
return false;
}


/**
* @override
*/
addSegments(segments) {
let keys = this.segments_.getKeys(segments.length);

for (let i = 0; i < keys.length; i++) {
this.segments_.set(keys[i], segments[i]);
}

return Promise.resolve(keys);
}


/**
* @override
*/
removeSegments(keys) {
this.segments_.remove(keys);
return Promise.resolve();
}


/**
* @override
*/
getSegments(keys) {
const MapUtils = shaka.util.MapUtils;

/** @type {!Object.<number, shakaExtern.SegmentDataDB>}*/
let map = this.segments_.get(keys);

// Make sure that every key was found.
if (!keys.every((key) => key in map)) {
return Promise.reject();
}

return Promise.resolve(MapUtils.values(map));
}


/**
* @override
*/
addManifests(manifests) {
let keys = this.manifests_.getKeys(manifests.length);

for (let i = 0; i < keys.length; i++) {
this.manifests_.set(keys[i], manifests[i]);
}

return Promise.resolve(keys);
}


/**
* @override
*/
updateManifests(manifests) {
shaka.util.MapUtils.forEach(manifests, (key, value) => {
this.manifests_.set(key, value);
});

return Promise.resolve();
}


/**
* @override
*/
removeManifests(keys) {
this.manifests_.remove(keys);
return Promise.resolve();
}


/**
* @override
*/
getManifests(keys) {
const MapUtils = shaka.util.MapUtils;

/** @type {!Object.<number, shakaExtern.ManifestDB>}*/
let map = this.manifests_.get(keys);

// Make sure that every key was found.
if (!keys.every((key) => key in map)) {
return Promise.reject();
}

return Promise.resolve(MapUtils.values(map));
}


/**
* @override
*/
getAllManifests() {
/** @type {!Object.<number, shakaExtern.ManifestDB>}*/
let map = this.manifests_.all();
return Promise.resolve(map);
}
};
72 changes: 72 additions & 0 deletions lib/offline/memory/storage_mechanism.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.offline.memory.StorageMechanism');

goog.require('shaka.offline.memory.StorageCell');



/**
* @implements {shakaExtern.StorageMechanism}
*/
shaka.offline.memory.StorageMechanism = class {
constructor() {
/** @private {shaka.offline.memory.StorageCell} */
this.cell_ = null;
}


/**
* @override
*/
init() {
this.cell_ = new shaka.offline.memory.StorageCell();
return Promise.resolve();
}


/**
* @override
*/
destroy() {
let cell = this.cell_;
this.cell_ = null;
return cell.destroy();
}


/**
* @override
*/
getCells() {
return {
'0': this.cell_
};
}


/**
* @override
*/
erase() {
// Since we need to be initialized after an erase, just set the cell to a
// new instance.
this.cell_ = new shaka.offline.memory.StorageCell();
return Promise.resolve();
}
};
117 changes: 117 additions & 0 deletions lib/offline/memory/storage_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.offline.memory.StorageTable');

goog.require('shaka.util.MapUtils');



/**
* @template T
*/
shaka.offline.memory.StorageTable = class {
constructor() {
/** @private {!Object.<number, !T>} */
this.values_ = {};
}


/**
* Make a shallow copy of the internal map.
*
* @return {!Object.<number, !T>}
*/
all() {
/** @type {!Object.<number, !T>}*/
let map = {};

shaka.util.MapUtils.forEach(this.values_, (key, value) => {
map[key] = value;
});

return map;
}


/**
* @param {number} count
* @return {!Array.<number>}
*/
getKeys(count) {
/** @type {number} */
let maxKey = 0;

shaka.util.MapUtils.forEach(this.values_, (key, value) => {
maxKey = Math.max(maxKey, key);
});

let keys = [];

for (let i = 0; i < count; i++) {
keys.push(maxKey + 1 + i);
}

return keys;
}


/**
* @param {number} key
* @param {!T} value
*/
set(key, value) {
this.values_[key] = value;
}


/**
* @param {!Array.<number>} keys
* @return {!Object.<number, !T>}
*/
get(keys) {
/** @type {!Object.<number, !T>} */
let map = {};

keys.forEach((key) => {
let value = this.values_[key];
if (value) {
map[key] = value;
}
});

return map;
}


/**
* @param {!Array.<number>} keys
*/
remove(keys) {
keys.forEach((key) => {
delete this.values_[key];
});
}


/**
* Clear all the values out of the internal table.
*/
clear() {
this.values_ = {};
}
};
4 changes: 3 additions & 1 deletion shaka-player.uncompiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ goog.require('shaka.media.PresentationTimeline');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.net.DataUriPlugin');
goog.require('shaka.net.HttpXHRPlugin');
goog.require('shaka.net.HttpFetchPlugin');
goog.require('shaka.net.HttpXHRPlugin');
goog.require('shaka.offline.OfflineManifestParser');
goog.require('shaka.offline.OfflineScheme');
goog.require('shaka.offline.Storage');
goog.require('shaka.offline.memory.StorageCell');
goog.require('shaka.offline.memory.StorageMechanism');
goog.require('shaka.polyfill.Fullscreen');
goog.require('shaka.polyfill.IndexedDB');
goog.require('shaka.polyfill.InputEvent');
Expand Down

0 comments on commit d8a4b20

Please sign in to comment.