Skip to content

Commit

Permalink
Optimize datastore performance separing text and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Nov 29, 2015
1 parent 6d28ccd commit 9600b09
Show file tree
Hide file tree
Showing 30 changed files with 1,035 additions and 499 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

var html5storage = {};
var datastorePrefix = 'sugar_datastore_';
var datastoreTextPrefix = 'sugar_datastoretext_';
var initialized = false;

//- Utility function
Expand Down Expand Up @@ -46,9 +47,11 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
datastore.create = function(metadata, callback, text) {
var callback_c = datastore.callbackChecker(callback);
var objectId = datastore.createUUID();
if (text !== undefined)
html5storage.setValue(datastoreTextPrefix + objectId, text);
html5storage.setValue(datastorePrefix + objectId, {
metadata: metadata,
text: (text === undefined) ? null : text
text: (text === undefined) ? null : { link: datastoreTextPrefix + objectId }
});
callback_c(null, objectId);
}
Expand All @@ -70,10 +73,11 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

return results;
}

// Remove an entry in the datastore
datastore.remove = function(objectId) {
html5storage.removeValue(datastorePrefix + objectId);
html5storage.removeValue(datastoreTextPrefix + objectId);
}

//- Instance datastore methods
Expand All @@ -90,7 +94,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
});
}

// Init or create objectId if need
// Init or create objectId if need
var that = this;
if (this.objectId === undefined) {
var env_objectId = window.top.sugar.environment.objectId;
Expand All @@ -107,7 +111,6 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
this.toload = false;
callback_c(null, result.metadata);
}
Expand All @@ -119,9 +122,13 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, result.text);
callback_c(null, result.metadata, text);
}
};

Expand Down Expand Up @@ -161,11 +168,14 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
this.newMetadata["buddy_name"] = sugar_settings.name;
this.newMetadata["buddy_color"] = sugar_settings.colorvalue;
}
if (this.newDataAsText != null) {
html5storage.setValue(datastoreTextPrefix + this.objectId, this.newDataAsText);
}
html5storage.setValue(datastorePrefix + this.objectId, {
metadata: this.newMetadata,
text: this.newDataAsText
text: (this.newDataAsText === undefined) ? null : { link: datastoreTextPrefix + this.objectId }
});
callback_c(null, this.newMetadata);
callback_c(null, this.newMetadata, this.newDataAsText);
};

datastore.DatastoreObject = DatastoreObject;
Expand All @@ -191,13 +201,14 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
for (var i = 0 ; i < storageloadedcalls.length ; i++) {
if (storageloadedcalls[i]) storageloadedcalls[i]();
}
storageloadedcalls = [];
});
} else {
if (then) then();
}
};
html5storage.load();

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand All @@ -210,10 +221,10 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
html5storage.setValue = function(key, value) {
if (this.test()) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = JSON.stringify(value);
var item = {};
item[key] = this.values[key];
item[key] = this.values[key];
chrome.storage.local.set(item);
} else {
window.localStorage.setItem(key, JSON.stringify(value));
Expand All @@ -237,7 +248,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
}
return null;
};

// Remove a value in the storage
html5storage.removeValue = function(key) {
if (this.test()) {
Expand All @@ -249,9 +260,9 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
window.localStorage.removeItem(key);
}
} catch (err) {}
}
}
};

// Get all values
html5storage.getAll = function() {
if (this.test()) {
Expand All @@ -265,7 +276,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
return null;
}
}
return null;
return null;
};

return datastore;
Expand Down
114 changes: 92 additions & 22 deletions activities/ActivityTemplate/lib/sugar-web/datastore/sugarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

var html5storage = {};
var datastorePrefix = 'sugar_datastore_';
var datastoreTextPrefix = 'sugar_datastoretext_';
var initialized = false;

//- Utility function
Expand Down Expand Up @@ -46,9 +47,11 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
datastore.create = function(metadata, callback, text) {
var callback_c = datastore.callbackChecker(callback);
var objectId = datastore.createUUID();
if (text !== undefined)
html5storage.setValue(datastoreTextPrefix + objectId, text);
html5storage.setValue(datastorePrefix + objectId, {
metadata: metadata,
text: (text === undefined) ? null : text
text: (text === undefined) ? null : { link: datastoreTextPrefix + objectId }
});
callback_c(null, objectId);
}
Expand All @@ -58,7 +61,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
var results = [];
if (!html5storage.test())
return results;
for (var key in localStorage) {
for (var key in html5storage.getAll()) {
if (key.substr(0, datastorePrefix.length) == datastorePrefix) {
var entry = html5storage.getValue(key);
entry.objectId = key.substr(datastorePrefix.length);
Expand All @@ -70,10 +73,11 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

return results;
}

// Remove an entry in the datastore
datastore.remove = function(objectId) {
html5storage.removeValue(datastorePrefix + objectId);
html5storage.removeValue(datastoreTextPrefix + objectId);
}

//- Instance datastore methods
Expand All @@ -90,7 +94,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
});
}

// Init or create objectId if need
// Init or create objectId if need
var that = this;
if (this.objectId === undefined) {
var env_objectId = window.top.sugar.environment.objectId;
Expand All @@ -104,13 +108,12 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load metadata
DatastoreObject.prototype.getMetadata = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
this.toload = false;
callback_c(null, result.metadata);
}
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.toload = false;
callback_c(null, result.metadata);
}
};

// Load text
Expand All @@ -119,9 +122,13 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, result.text);
callback_c(null, result.metadata, text);
}
};

Expand Down Expand Up @@ -161,11 +168,14 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
this.newMetadata["buddy_name"] = sugar_settings.name;
this.newMetadata["buddy_color"] = sugar_settings.colorvalue;
}
if (this.newDataAsText != null) {
html5storage.setValue(datastoreTextPrefix + this.objectId, this.newDataAsText);
}
html5storage.setValue(datastorePrefix + this.objectId, {
metadata: this.newMetadata,
text: this.newDataAsText
text: (this.newDataAsText === undefined) ? null : { link: datastoreTextPrefix + this.objectId }
});
callback_c(null, this.newMetadata);
callback_c(null, this.newMetadata, this.newDataAsText);
};

datastore.DatastoreObject = DatastoreObject;
Expand All @@ -174,39 +184,99 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

// -- HTML5 local storage handling

// Load storage - Need for Chrome App
var storageloadedcalls = [];
html5storage.load = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
var that = this;
// Currently load, will call then later
if (storageloadedcalls.length != 0) {
storageloadedcalls.push(then);
return;
}
storageloadedcalls.push(then);
chrome.storage.local.get(null, function(values) {
that.values = values;
// Call all waiting functions
for (var i = 0 ; i < storageloadedcalls.length ; i++) {
if (storageloadedcalls[i]) storageloadedcalls[i]();
}
storageloadedcalls = [];
});
} else {
if (then) then();
}
};
html5storage.load();

// Test if HTML5 storage is available
html5storage.test = function() {
return (typeof(Storage) !== "undefined" && typeof(window.localStorage) !== "undefined");
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
return true;
else
return (typeof(Storage) !== "undefined" && typeof(window.localStorage) !== "undefined");
};

// Set a value in the storage
html5storage.setValue = function(key, value) {
if (this.test()) {
try {
window.localStorage.setItem(key, JSON.stringify(value));
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = JSON.stringify(value);
var item = {};
item[key] = this.values[key];
chrome.storage.local.set(item);
} else {
window.localStorage.setItem(key, JSON.stringify(value));
}
} catch (err) {}
}
};

// Get a value in the storage
html5storage.getValue = function(key) {
if (this.test()) {
try {
return JSON.parse(window.localStorage.getItem(key));
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
return JSON.parse(this.values[key]);
} else {
return JSON.parse(window.localStorage.getItem(key));
}
} catch (err) {
return null;
}
}
return null;
};

// Remove a value in the storage
html5storage.removeValue = function(key) {
if (this.test()) {
try {
window.localStorage.removeItem(key);
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
} else {
window.localStorage.removeItem(key);
}
} catch (err) {}
}
}
};

// Get all values
html5storage.getAll = function() {
if (this.test()) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
return this.values;
} else {
return window.localStorage;
}
} catch (err) {
return null;
}
}
return null;
};

return datastore;
Expand Down
Loading

0 comments on commit 9600b09

Please sign in to comment.