Skip to content

Commit

Permalink
Create DBUtils
Browse files Browse the repository at this point in the history
Create DBUtils to hold code that will be useful to updating code
outside of DBEngine.

Issue #1047

Change-Id: If759fd0d778f42e6114e82e9274a053af420ad35
vaage committed Dec 14, 2017
1 parent 4ce9634 commit 80c5fff
Showing 3 changed files with 71 additions and 59 deletions.
1 change: 1 addition & 0 deletions build/types/offline
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The offline storage system and manifest parser plugin.

+../../lib/offline/db_engine.js
+../../lib/offline/db_utils.js
+../../lib/offline/download_manager.js
+../../lib/offline/i_storage_engine.js
+../../lib/offline/offline_manifest_parser.js
90 changes: 31 additions & 59 deletions lib/offline/db_engine.js
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
goog.provide('shaka.offline.DBEngine');

goog.require('goog.asserts');
goog.require('shaka.offline.DBUtils');
goog.require('shaka.offline.IStorageEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
@@ -80,26 +81,6 @@ shaka.offline.DBEngine.Operation;
shaka.offline.DBEngine.DB_VERSION_ = 1;


/**
* @enum {string}
* @private
*/
shaka.offline.DBEngine.Mode_ = {
READ_ONLY: 'readonly',
READ_WRITE: 'readwrite'
};


/**
* @enum {string}
* @private
*/
shaka.offline.DBEngine.Store_ = {
MANIFEST: 'manifest',
SEGMENT: 'segment'
};


/**
* Determines if the browsers supports IndexedDB.
* @return {boolean}
@@ -136,11 +117,11 @@ shaka.offline.DBEngine.prototype.init = function() {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var READ_ONLY = DBEngine.Mode_.READ_ONLY;
var READ_ONLY = shaka.offline.DBUtils.Mode.READ_ONLY;
/** @const */
var MANIFEST = DBEngine.Store_.MANIFEST;
var MANIFEST = shaka.offline.DBUtils.Store.MANIFEST;
/** @const */
var SEGMENT = DBEngine.Store_.SEGMENT;
var SEGMENT = shaka.offline.DBUtils.Store.SEGMENT;

/** @type {!shaka.offline.DBEngine} */
var self = this;
@@ -189,9 +170,9 @@ shaka.offline.DBEngine.prototype.destroy = function() {
/** @override */
shaka.offline.DBEngine.prototype.getManifest = function(key) {
/** @const */
var Store = shaka.offline.DBEngine.Store_;
var MANIFEST = shaka.offline.DBUtils.Store.MANIFEST;

return this.get_(Store.MANIFEST, key).then(function(manifest) {
return this.get_(MANIFEST, key).then(function(manifest) {
return manifest ?
shaka.offline.DBEngine.sanitizeManifest_(manifest) :
null;
@@ -201,10 +182,9 @@ shaka.offline.DBEngine.prototype.getManifest = function(key) {

/** @override */
shaka.offline.DBEngine.prototype.forEachManifest = function(each) {
/** @const */
var Store = shaka.offline.DBEngine.Store_;

return this.forEach_(Store.MANIFEST, each);
return this.forEach_(
shaka.offline.DBUtils.Store.MANIFEST,
each);
};


@@ -214,7 +194,7 @@ shaka.offline.DBEngine.prototype.addManifest = function(value) {
var key = this.nextManifestId_++;

return this.insert_(
shaka.offline.DBEngine.Store_.MANIFEST,
shaka.offline.DBUtils.Store.MANIFEST,
key,
value);
};
@@ -223,7 +203,7 @@ shaka.offline.DBEngine.prototype.addManifest = function(value) {
/** @override */
shaka.offline.DBEngine.prototype.updateManifest = function(key, value) {
return this.insert_(
shaka.offline.DBEngine.Store_.MANIFEST,
shaka.offline.DBUtils.Store.MANIFEST,
key,
value);
};
@@ -233,7 +213,7 @@ shaka.offline.DBEngine.prototype.updateManifest = function(key, value) {
shaka.offline.DBEngine.prototype.removeManifests =
function(keys, onKeyRemoved) {
return this.remove_(
shaka.offline.DBEngine.Store_.MANIFEST,
shaka.offline.DBUtils.Store.MANIFEST,
keys,
onKeyRemoved);
};
@@ -242,15 +222,15 @@ shaka.offline.DBEngine.prototype.removeManifests =
/** @override */
shaka.offline.DBEngine.prototype.getSegment = function(key) {
return this.get_(
shaka.offline.DBEngine.Store_.SEGMENT,
shaka.offline.DBUtils.Store.SEGMENT,
key);
};


/** @override */
shaka.offline.DBEngine.prototype.forEachSegment = function(each) {
return this.forEach_(
shaka.offline.DBEngine.Store_.SEGMENT,
shaka.offline.DBUtils.Store.SEGMENT,
each);
};

@@ -261,7 +241,7 @@ shaka.offline.DBEngine.prototype.addSegment = function(value) {
var key = this.nextSegmentId_++;

return this.insert_(
shaka.offline.DBEngine.Store_.SEGMENT,
shaka.offline.DBUtils.Store.SEGMENT,
key,
value);
};
@@ -271,24 +251,22 @@ shaka.offline.DBEngine.prototype.addSegment = function(value) {
shaka.offline.DBEngine.prototype.removeSegments =
function(keys, onKeyRemoved) {
return this.remove_(
shaka.offline.DBEngine.Store_.SEGMENT,
shaka.offline.DBUtils.Store.SEGMENT,
keys,
onKeyRemoved);
};


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

/** @type {IDBRequest} */
var request;
@@ -299,17 +277,15 @@ shaka.offline.DBEngine.prototype.get_ = function(store, key) {


/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {shaka.offline.DBUtils.Store} store
* @param {function(number, T)} each
* @return {!Promise}
* @template T
* @private
*/
shaka.offline.DBEngine.prototype.forEach_ = function(store, each) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var READ_ONLY = DBEngine.Mode_.READ_ONLY;
var READ_ONLY = shaka.offline.DBUtils.Mode.READ_ONLY;

return this.createTransaction_(store, READ_ONLY, function(store) {
/** @type {IDBRequest} */
@@ -327,7 +303,7 @@ shaka.offline.DBEngine.prototype.forEach_ = function(store, each) {


/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {shaka.offline.DBUtils.Store} store
* @param {number} key
* @param {T} value
* @return {!Promise<number>}
@@ -336,9 +312,7 @@ shaka.offline.DBEngine.prototype.forEach_ = function(store, each) {
*/
shaka.offline.DBEngine.prototype.insert_ = function(store, key, value) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var READ_WRITE = DBEngine.Mode_.READ_WRITE;
var READ_WRITE = shaka.offline.DBUtils.Mode.READ_WRITE;

// TODO (vaage) : Replace this with auto key.
value['key'] = key;
@@ -350,7 +324,7 @@ shaka.offline.DBEngine.prototype.insert_ = function(store, key, value) {


/**
* @param {shaka.offline.DBEngine.Store_} store
* @param {shaka.offline.DBUtils.Store} store
* @param {!Array<number>} keys
* @param {?function(number)} onKeyRemoved
* @return {!Promise}
@@ -359,9 +333,7 @@ shaka.offline.DBEngine.prototype.insert_ = function(store, key, value) {
*/
shaka.offline.DBEngine.prototype.remove_ = function(store, keys, onKeyRemoved) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var READ_WRITE = DBEngine.Mode_.READ_WRITE;
var READ_WRITE = shaka.offline.DBUtils.Mode.READ_WRITE;

return this.createTransaction_(store, READ_WRITE, function(store) {
keys.forEach(function(key) {
@@ -393,11 +365,9 @@ shaka.offline.DBEngine.prototype.createTransaction_ = function(storeName,
type,
action) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const {string} */
var READ_ONLY = DBEngine.Mode_.READ_ONLY;
/** @const {string} */
var READ_WRITE = DBEngine.Mode_.READ_WRITE;
var READ_ONLY = shaka.offline.DBUtils.Mode.READ_ONLY;
/** @const */
var READ_WRITE = shaka.offline.DBUtils.Mode.READ_WRITE;

/** @type {!shaka.offline.DBEngine} */
var self = this;
@@ -491,6 +461,8 @@ shaka.offline.DBEngine.prototype.createConnection_ = function() {
shaka.offline.DBEngine.createConnection_ = function(promise, name, retries) {
/** @const */
var DBEngine = shaka.offline.DBEngine;
/** @const */
var DBUtils = shaka.offline.DBUtils;

var indexedDB = window.indexedDB;
var request = indexedDB.open(name, DBEngine.DB_VERSION_);
@@ -506,8 +478,8 @@ shaka.offline.DBEngine.createConnection_ = function(promise, name, retries) {
'Version 0 database should be empty');

// TODO(vaage) : Change to use auto increment keys.
db.createObjectStore(DBEngine.Store_.MANIFEST, {keyPath: 'key'});
db.createObjectStore(DBEngine.Store_.SEGMENT, {keyPath: 'key'});
db.createObjectStore(DBUtils.Store.MANIFEST, {keyPath: 'key'});
db.createObjectStore(DBUtils.Store.SEGMENT, {keyPath: 'key'});

upgraded = true;
};
39 changes: 39 additions & 0 deletions lib/offline/db_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @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.DBUtils');


/**
* @enum {string}
*/
shaka.offline.DBUtils.Mode = {
READ_ONLY: 'readonly',
READ_WRITE: 'readwrite'
};


/**
* The name for the stores that are used in the current version
* of indexed db.
*
* @enum {string}
*/
shaka.offline.DBUtils.Store = {
MANIFEST: 'manifest',
SEGMENT: 'segment'
};

0 comments on commit 80c5fff

Please sign in to comment.