Skip to content

Instantly share code, notes, and snippets.

@DarkPark
Last active March 27, 2018 08:11
Show Gist options
  • Save DarkPark/c0bd9c4e6ca3bda5d3688e4f1702e680 to your computer and use it in GitHub Desktop.
Save DarkPark/c0bd9c4e6ca3bda5d3688e4f1702e680 to your computer and use it in GitHub Desktop.
IndexedDB tests
//indexedDB.deleteDatabase('test');
var request = window.indexedDB.open('test', 1),
amount = 1000,
ids = [],
db;
function getRandomInt ( min, max ) {
return Math.floor(Math.random() * (max - min)) + min;
}
function fill () {
var tx = db.transaction('data', 'readwrite'),
store = tx.objectStore('data'),
i, j, id;
console.log('start filling');
for ( i = 1; i <= amount; i++ ) {
for ( j = 1; j <= amount; j++ ) {
//id = String.fromCharCode.apply(null, crypto.getRandomValues(new Uint8Array(64)));
id = btoa(String.fromCharCode.apply(null, crypto.getRandomValues(new Uint8Array(6))));
ids.push(id);
store.put({
//sid: i,
id: id,
/*gid: i,
lid: j,/**/
/*id: {
global: i,
local: j
},/**/
//time: Date.now(),
//active: Math.random() > 0.5 ? 1 : 0,
//data: new Array(100).join('*')
});
}
}
console.log('finish filling');
}
request.onerror = function ( event ) {
console.error('request.onerror', event);
};
request.onupgradeneeded = function ( event ) {
var db = event.target.result,
//store = db.createObjectStore('data', {keyPath: 'id', autoIncrement: true}),
//store = db.createObjectStore('data', {keyPath: ['gid', 'lid']}),
store = db.createObjectStore('data', {keyPath: 'id'}),
//store = db.createObjectStore('data', {keyPath: ['id.global', 'id.local']}),
i, j;
//store.createIndex('id', 'id', {unique: false});
//store.createIndex('sid', 'sid', {unique: false});
store.createIndex('time', 'time', {unique: false});
store.createIndex('active', 'active', {unique: false});
store.createIndex('complex', ['sid', 'active', 'time'], {unique: false});
/*console.log('start filling');
for ( i = 1; i <= amount; i++ ) {
for ( j = 1; j <= amount; j++ ) {
store.add({
//id: {did: i, rid: i},
gid: 1,
lid: i,
time: Date.now(),
active: Math.random() > 0.5 ? 1 : 0,
data: new Array(100).join('*')
});
}
}
console.log('finish filling');/**/
// main encrypted data storage
// fields: id, keyId, iv, data, time
//store = db.createObjectStore('data', {keyPath: 'id', autoIncrement: true})
// to get all data records for the specific key
//store.createIndex('keyId', 'keyId', {unique: false});
// to get all new records on sync
//store.createIndex('time', 'time', {unique: false});
// local configuration data
// fields: name, value, time?
store = db.createObjectStore('options', {keyPath: 'name'});
// devices
// fields: id, uid, active, time
store = db.createObjectStore('devices', {keyPath: 'id', autoIncrement: true});
// to get active notes sorted by time
store.createIndex('active', ['active', 'time'], {unique: false});
// note declarations
// fields: id, uid, active, time
store = db.createObjectStore('notes', {keyPath: 'id', autoIncrement: true});
// to get active notes sorted by time
store.createIndex('active', ['active', 'time'], {unique: false});
// note versions
// fields: id, uid, noteId, active, time, values/dataIds?, tagIds?
store = db.createObjectStore('notesVersions', {keyPath: 'id', autoIncrement: true});
// to get active note versions sorted by time
store.createIndex('active', ['noteId', 'active', 'time'], {unique: false});
// note values
// fields: id, uid, versionId?, data, time?
store = db.createObjectStore('notesValues', {keyPath: 'id', autoIncrement: true});
// to get all note values for the specific note version
//store.createIndex('versionId', 'versionId', {unique: false});
// tag declarations
// fields: id, uid, time, count, dataId
store = db.createObjectStore('tags', {keyPath: 'id', autoIncrement: true});
// to get tags sorted by linked notes amount
store.createIndex('count', 'count', {unique: false});
// note verstion/value link declarations
// fields: id, time, versionId, valueId
//store = db.createObjectStore('noteVersionValue', {keyPath: 'id', autoIncrement: true});
//
//store.createIndex('versionId', 'versionId', {unique: false});
//store.createIndex('valueId', 'valueId', {unique: false});
};
request.onsuccess = function ( event ) {
var store;
db = event.target.result;
console.log(db);
db.onerror = function ( event ) {
console.error('db.onerror', event);
};
store = db.transaction('data').objectStore('data');
/*console.time('get primary record');
store.get(getRandomInt(1, amount)).onsuccess = function ( event ) {
//console.log(event.target.result);
console.timeEnd('get primary record');
};
db.transaction('data').objectStore('data').get(ids[0]).onsuccess = function ( event ) {
console.log(event.target.result);
};
/**/
/*console.time('get indexed record');
store.index('sid').get(getRandomInt(1, amount)).onsuccess = function ( event ) {
console.log(event.target.result);
console.timeEnd('get indexed record');
};/**/
/*console.time('get indexed records');
store.index('sid').openCursor(IDBKeyRange.only(getRandomInt(1, amount))).onsuccess = function ( event ) {
var cursor = event.target.result;
if ( cursor ) {
//console.log(cursor.value);
cursor.continue();
} else {
console.timeEnd('get indexed records');
}
};/**/
/*console.time('get indexed records');
store.index('sid').getAll(IDBKeyRange.only(getRandomInt(1, amount))).onsuccess = function ( event ) {
//console.log(event.target.result);
console.timeEnd('get indexed records');
};/**/
/*db.transaction('data').objectStore('data').index('complex').getAll(IDBKeyRange.bound([1, 1, 0], [1, 1, Date.now()])).onsuccess = function ( event ) {
console.log(event.target.result);
};/**/
/*db.transaction('data').objectStore('data').index('complex').getAll(IDBKeyRange.only([1, 1])).onsuccess = function ( event ) {
console.log(event.target.result);
};/**/
//db.transaction('data', 'readwrite').objectStore('data').put({sid: 1, time: 123, active: 1, data: new Array(100).join('*') });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment