forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
362 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ = {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters