Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Vincent Bechu
jio
Commits
5a302480
Commit
5a302480
authored
Apr 03, 2014
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get,post,put,remove indexexDB performance improved
parent
b60b42a1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
262 additions
and
89 deletions
+262
-89
src/jio.storage/indexeddbstorage.js
src/jio.storage/indexeddbstorage.js
+262
-89
No files found.
src/jio.storage/indexeddbstorage.js
View file @
5a302480
...
@@ -81,33 +81,36 @@
...
@@ -81,33 +81,36 @@
}
}
// XXX doc string
// XXX doc string
IndexedDBStorage
.
prototype
.
createDBIfNecessary
=
function
()
{
function
openRequestOnUpgradeNeeded
(
shared
,
event
)
{
var
status
,
open_req
=
indexedDB
.
open
(
this
.
_database_name
);
if
(
shared
.
aborted
)
{
return
;
}
// No request.abort() is provided so we cannot cancel database creation
shared
.
db
=
event
.
target
.
result
;
return
new
Promise
(
function
(
resolve
,
reject
)
{
open_req
.
onupgradeneeded
=
function
()
{
// *Called at t + 1*
var
store
,
db
=
open_req
.
result
;
// If we reach this point, the database is created.
// There is no way to cancel the operation from here.
// So let's continue.
try
{
try
{
store
=
db
.
createObjectStore
(
"
metadata
"
,
{
shared
.
store
=
shared
.
db
.
createObjectStore
(
"
metadata
"
,
{
"
keyPath
"
:
"
_id
"
"
keyPath
"
:
"
_id
"
//"autoIncrement": true
});
});
// `createObjectStore` can throw InvalidStateError - open_req.onerror
// `createObjectStore` can throw InvalidStateError - open_req.onerror
// and db.onerror won't be called.
// and db.onerror won't be called.
store
.
createIndex
(
"
_id
"
,
"
_id
"
);
shared
.
store
.
createIndex
(
"
_id
"
,
"
_id
"
);
// `store.createIndex` can throw an error
// `store.createIndex` can throw an error
status
=
"
created
"
;
shared
.
db_created
=
true
;
//
store.transaction.oncomplete = function () {
shared
.
store
.
transaction
.
oncomplete
=
function
()
{
// // *Called at t + 2*
delete
shared
.
store
;
//
};
};
}
catch
(
e
)
{
}
catch
(
e
)
{
reject
(
e
);
shared
.
reject
(
e
);
db
.
close
();
shared
.
db
.
close
();
shared
.
aborted
=
true
;
}
}
};
}
IndexedDBStorage
.
prototype
.
createDBIfNecessary
=
function
()
{
var
shared
=
{},
open_req
=
indexedDB
.
open
(
this
.
_database_name
);
// No request.abort() is provided so we cannot cancel database creation
return
new
Promise
(
function
(
resolve
,
reject
)
{
shared
.
reject
=
reject
;
open_req
.
onupgradeneeded
=
openRequestOnUpgradeNeeded
.
bind
(
open_req
,
shared
);
open_req
.
onerror
=
function
()
{
open_req
.
onerror
=
function
()
{
if
(
open_req
.
result
)
{
open_req
.
result
.
close
();
}
if
(
open_req
.
result
)
{
open_req
.
result
.
close
();
}
reject
(
open_req
.
error
);
reject
(
open_req
.
error
);
...
@@ -115,7 +118,7 @@
...
@@ -115,7 +118,7 @@
open_req
.
onsuccess
=
function
()
{
open_req
.
onsuccess
=
function
()
{
// *Called at t + 3*
// *Called at t + 3*
open_req
.
result
.
close
();
open_req
.
result
.
close
();
resolve
(
s
tatus
||
"
no_content
"
);
resolve
(
s
hared
.
db_created
?
"
created
"
:
"
no_content
"
);
};
};
});
});
};
};
...
@@ -280,51 +283,169 @@
...
@@ -280,51 +283,169 @@
// XXX doc string
// XXX doc string
IndexedDBStorage
.
prototype
.
get
=
function
(
command
,
param
)
{
IndexedDBStorage
.
prototype
.
get
=
function
(
command
,
param
)
{
this
.
createDBIfNecessary
().
var
shared
=
{
"
connector
"
:
this
};
then
(
this
.
getMetadata
.
bind
(
this
,
param
.
_id
)).
new
Promise
(
function
(
resolve
,
reject
)
{
then
(
function
(
data
)
{
shared
.
reject
=
reject
;
command
.
success
({
"
data
"
:
data
});
},
command
.
error
,
command
.
notify
);
// Open DB //
var
open_req
=
indexedDB
.
open
(
shared
.
connector
.
_database_name
);
open_req
.
onerror
=
function
(
event
)
{
reject
(
event
.
target
.
errorCode
);
};
// Create DB if necessary //
open_req
.
onupgradeneeded
=
openRequestOnUpgradeNeeded
.
bind
(
open_req
,
shared
);
open_req
.
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
try
{
shared
.
db
=
event
.
target
.
result
;
// Open transaction //
shared
.
tx
=
shared
.
db
.
transaction
(
"
metadata
"
,
"
readonly
"
);
shared
.
tx
.
onerror
=
function
()
{
reject
(
shared
.
tx
.
error
);
shared
.
db
.
close
();
};
shared
.
onCancel
=
function
()
{
shared
.
tx
.
abort
();
shared
.
db
.
close
();
};
// Get index //
shared
.
store
=
shared
.
tx
.
objectStore
(
"
metadata
"
);
shared
.
index_request
=
shared
.
store
.
index
(
"
_id
"
);
// Get metadata //
shared
.
index_request
.
get
(
param
.
_id
).
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
if
(
event
.
target
.
result
===
undefined
)
{
reject
({
"
status
"
:
404
});
return
;
}
shared
.
final_result
=
{
"
data
"
:
event
.
target
.
result
};
};
// Respond to jIO //
shared
.
tx
.
oncomplete
=
function
()
{
resolve
(
shared
.
final_result
);
shared
.
db
.
close
();
};
}
catch
(
e1
)
{
reject
(
e1
);
shared
.
db
.
close
();
}
};
}).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
};
// XXX doc string
// XXX doc string
IndexedDBStorage
.
prototype
.
post
=
function
(
command
,
metadata
)
{
IndexedDBStorage
.
prototype
.
post
=
function
(
command
,
metadata
)
{
var
promise
;
var
shared
=
{
"
connector
"
:
this
};
promise
=
this
.
createDBIfNecessary
();
if
(
!
metadata
.
_id
)
{
if
(
metadata
.
_id
)
{
metadata
.
_id
=
generateUuid
();
promise
=
promise
.
}
then
(
this
.
getMetadata
.
bind
(
this
,
metadata
.
_id
)).
new
Promise
(
function
(
resolve
,
reject
)
{
then
(
function
()
{
shared
.
reject
=
reject
;
throw
"
conflict
"
;
},
function
(
error
)
{
// Open DB //
if
(
error
===
"
not_found
"
)
{
var
open_req
=
indexedDB
.
open
(
shared
.
connector
.
_database_name
);
open_req
.
onerror
=
function
(
event
)
{
reject
(
event
.
target
.
errorCode
);
};
// Create DB if necessary //
open_req
.
onupgradeneeded
=
openRequestOnUpgradeNeeded
.
bind
(
open_req
,
shared
);
open_req
.
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
try
{
shared
.
db
=
event
.
target
.
result
;
// Open transaction //
shared
.
tx
=
shared
.
db
.
transaction
(
"
metadata
"
,
"
readwrite
"
);
shared
.
tx
.
onerror
=
function
()
{
reject
(
shared
.
tx
.
error
);
shared
.
db
.
close
();
};
shared
.
onCancel
=
function
()
{
shared
.
tx
.
abort
();
shared
.
db
.
close
();
};
// Get index //
shared
.
store
=
shared
.
tx
.
objectStore
(
"
metadata
"
);
shared
.
index_request
=
shared
.
store
.
index
(
"
_id
"
);
// Get metadata //
shared
.
index_request
.
get
(
metadata
.
_id
).
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
if
(
event
.
target
.
result
!==
undefined
)
{
shared
.
db
.
close
();
reject
({
"
status
"
:
409
,
"
reason
"
:
"
document already exist
"
});
return
;
return
;
}
}
throw
error
;
delete
metadata
.
_attachments
;
});
// Push metadata //
}
else
{
shared
.
store
.
put
(
metadata
);
metadata
.
_id
=
generateUuid
();
};
shared
.
tx
.
oncomplete
=
function
()
{
// Respond to jIO //
shared
.
db
.
close
();
resolve
({
"
id
"
:
metadata
.
_id
});
};
}
catch
(
e1
)
{
reject
(
e1
);
shared
.
db
.
close
();
}
}
promise
.
then
(
this
.
putMetadata
.
bind
(
this
,
metadata
)).
};
then
(
function
()
{
}).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
command
.
success
({
"
id
"
:
metadata
.
_id
});
},
command
.
error
,
command
.
notify
);
};
};
// XXX doc string
// XXX doc string
IndexedDBStorage
.
prototype
.
put
=
function
(
command
,
metadata
)
{
IndexedDBStorage
.
prototype
.
put
=
function
(
command
,
metadata
)
{
var
status
;
var
shared
=
{
"
connector
"
:
this
};
this
.
createDBIfNecessary
().
new
Promise
(
function
(
resolve
,
reject
)
{
then
(
this
.
getMetadata
.
bind
(
this
,
metadata
.
_id
)).
shared
.
reject
=
reject
;
then
(
null
,
function
(
error
)
{
if
(
error
===
"
not_found
"
)
{
// Open DB //
status
=
"
created
"
;
var
open_req
=
indexedDB
.
open
(
shared
.
connector
.
_database_name
);
return
{};
open_req
.
onerror
=
function
(
event
)
{
}
reject
(
event
.
target
.
errorCode
);
throw
error
;
};
}).
then
(
function
(
data
)
{
// Create DB if necessary //
var
i
,
l
,
key
,
array
;
open_req
.
onupgradeneeded
=
openRequestOnUpgradeNeeded
.
bind
(
open_req
,
shared
);
open_req
.
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
try
{
shared
.
db
=
event
.
target
.
result
;
// Open transaction //
shared
.
tx
=
shared
.
db
.
transaction
(
"
metadata
"
,
"
readwrite
"
);
shared
.
tx
.
onerror
=
function
()
{
reject
(
shared
.
tx
.
error
);
shared
.
db
.
close
();
};
shared
.
onCancel
=
function
()
{
shared
.
tx
.
abort
();
shared
.
db
.
close
();
};
// Get index //
shared
.
store
=
shared
.
tx
.
objectStore
(
"
metadata
"
);
shared
.
index
=
shared
.
store
.
index
(
"
_id
"
);
// Get metadata //
shared
.
index
.
get
(
metadata
.
_id
).
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
var
i
,
l
,
key
,
array
,
data
;
if
(
event
.
target
.
result
!==
undefined
)
{
shared
.
found
=
true
;
data
=
event
.
target
.
result
;
// Update metadata //
array
=
Object
.
keys
(
metadata
);
array
=
Object
.
keys
(
metadata
);
for
(
i
=
0
,
l
=
array
.
length
;
i
<
l
;
i
+=
1
)
{
for
(
i
=
0
,
l
=
array
.
length
;
i
<
l
;
i
+=
1
)
{
key
=
array
[
i
];
key
=
array
[
i
];
...
@@ -333,33 +454,85 @@
...
@@ -333,33 +454,85 @@
if
(
data
.
_attachments
)
{
if
(
data
.
_attachments
)
{
metadata
.
_attachments
=
data
.
_attachments
;
metadata
.
_attachments
=
data
.
_attachments
;
}
}
}).
}
else
{
then
(
this
.
putMetadata
.
bind
(
this
,
metadata
)).
delete
metadata
.
_attachments
;
then
(
function
()
{
}
command
.
success
(
status
||
"
no_content
"
);
// Push metadata //
},
command
.
error
,
command
.
notify
);
shared
.
store
.
put
(
metadata
);
};
shared
.
tx
.
oncomplete
=
function
()
{
// Respond to jIO //
shared
.
db
.
close
();
resolve
({
"
status
"
:
shared
.
found
?
204
:
201
});
};
}
catch
(
e1
)
{
reject
(
e1
);
shared
.
db
.
close
();
}
};
}).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
};
// XXX doc string
// XXX doc string
IndexedDBStorage
.
prototype
.
remove
=
function
(
command
,
param
)
{
IndexedDBStorage
.
prototype
.
remove
=
function
(
command
,
param
)
{
var
this_
=
this
;
var
shared
=
{
"
connector
"
:
this
};
this
.
createDBIfNecessary
().
new
Promise
(
function
(
resolve
,
reject
)
{
then
(
this
.
getMetadata
.
bind
(
this
,
param
.
_id
)).
shared
.
reject
=
reject
;
then
(
function
(
metadata
)
{
var
attachments
=
metadata
.
_attachments
,
promise
;
// Open DB //
promise
=
this_
.
removeMetadata
(
param
.
_id
);
var
open_req
=
indexedDB
.
open
(
shared
.
connector
.
_database_name
);
if
(
typeof
attachments
===
"
object
"
&&
attachments
!==
null
)
{
open_req
.
onerror
=
function
(
event
)
{
promise
.
then
(
function
(
answer
)
{
reject
(
event
.
target
.
errorCode
);
return
this_
.
removeAttachments
(
};
param
.
_id
,
Object
.
keys
(
attachments
)
// Create DB if necessary //
).
then
(
null
,
function
()
{
return
;
}).
open_req
.
onupgradeneeded
=
then
(
function
()
{
return
answer
;
});
openRequestOnUpgradeNeeded
.
bind
(
open_req
,
shared
);
});
open_req
.
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
try
{
shared
.
db
=
event
.
target
.
result
;
// Open transaction //
shared
.
tx
=
shared
.
db
.
transaction
(
"
metadata
"
,
"
readwrite
"
);
shared
.
tx
.
onerror
=
function
()
{
reject
(
shared
.
tx
.
error
);
shared
.
db
.
close
();
};
shared
.
onCancel
=
function
()
{
shared
.
tx
.
abort
();
shared
.
db
.
close
();
};
// Get index //
shared
.
store
=
shared
.
tx
.
objectStore
(
"
metadata
"
);
shared
.
index
=
shared
.
store
.
index
(
"
_id
"
);
// Get metadata //
shared
.
index
.
get
(
param
.
_id
).
onsuccess
=
function
(
event
)
{
if
(
shared
.
aborted
)
{
return
;
}
if
(
event
.
target
.
result
===
undefined
)
{
shared
.
db
.
close
();
reject
({
"
status
"
:
404
});
return
;
}
}
return
promise
;
// Delete metadata //
}).
shared
.
store
[
"
delete
"
](
param
.
_id
);
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
// XXX Delete attachments //
};
shared
.
tx
.
oncomplete
=
function
()
{
// Respond to jIO //
shared
.
db
.
close
();
resolve
();
};
}
catch
(
e1
)
{
reject
(
e1
);
shared
.
db
.
close
();
}
};
}).
then
(
command
.
success
,
command
.
error
,
command
.
notify
);
};
};
// XXX doc string
// XXX doc string
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment