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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xiaowu Zhang
jio
Commits
0f5db310
Commit
0f5db310
authored
Aug 21, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
localstorage memory mode added
parent
02686913
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
25 deletions
+61
-25
src/jio.storage/localstorage.js
src/jio.storage/localstorage.js
+61
-25
No files found.
src/jio.storage/localstorage.js
View file @
0f5db310
...
@@ -5,8 +5,7 @@
...
@@ -5,8 +5,7 @@
*/
*/
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, localStorage: true, setTimeout: true,
/*global jIO, localStorage, setTimeout, complex_queries, define */
complex_queries, define */
/**
/**
* JIO Local Storage. Type = 'local'.
* JIO Local Storage. Type = 'local'.
...
@@ -16,6 +15,9 @@
...
@@ -16,6 +15,9 @@
*
*
* {
* {
* "type": "local",
* "type": "local",
* "mode": <string>,
* // - "localStorage" // default
* // - "memory"
* "username": <non empty string>, // to define user space
* "username": <non empty string>, // to define user space
* "application_name": <string> // default 'untitled'
* "application_name": <string> // default 'untitled'
* }
* }
...
@@ -94,11 +96,13 @@
...
@@ -94,11 +96,13 @@
return
true
;
return
true
;
}
}
var
ram
=
{},
memorystorage
,
localstorage
;
/*
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* Wrapper for the localStorage used to simplify instion of any kind of
* values
* values
*/
*/
var
localstorage
=
{
localstorage
=
{
getItem
:
function
(
item
)
{
getItem
:
function
(
item
)
{
var
value
=
localStorage
.
getItem
(
item
);
var
value
=
localStorage
.
getItem
(
item
);
return
value
===
null
?
null
:
JSON
.
parse
(
value
);
return
value
===
null
?
null
:
JSON
.
parse
(
value
);
...
@@ -111,6 +115,23 @@
...
@@ -111,6 +115,23 @@
}
}
};
};
/*
* Wrapper for the localStorage used to simplify instion of any kind of
* values
*/
memorystorage
=
{
getItem
:
function
(
item
)
{
var
value
=
ram
[
item
];
return
value
===
undefined
?
null
:
JSON
.
parse
(
value
);
},
setItem
:
function
(
item
,
value
)
{
ram
[
item
]
=
JSON
.
stringify
(
value
);
},
removeItem
:
function
(
item
)
{
delete
ram
[
item
];
}
};
jIO
.
addStorageType
(
'
local
'
,
function
(
spec
,
my
)
{
jIO
.
addStorageType
(
'
local
'
,
function
(
spec
,
my
)
{
spec
=
spec
||
{};
spec
=
spec
||
{};
...
@@ -125,11 +146,26 @@
...
@@ -125,11 +146,26 @@
priv
.
localpath
=
'
jio/localstorage/
'
+
priv
.
username
+
'
/
'
+
priv
.
localpath
=
'
jio/localstorage/
'
+
priv
.
username
+
'
/
'
+
priv
.
application_name
;
priv
.
application_name
;
switch
(
spec
.
mode
)
{
case
"
memory
"
:
priv
.
database
=
ram
;
priv
.
storage
=
memorystorage
;
priv
.
mode
=
"
memory
"
;
break
;
default
:
priv
.
database
=
localStorage
;
priv
.
storage
=
localstorage
;
priv
.
mode
=
"
localStorage
"
;
break
;
}
// ===================== overrides ======================
// ===================== overrides ======================
that
.
specToStore
=
function
()
{
that
.
specToStore
=
function
()
{
return
{
return
{
"
application_name
"
:
priv
.
application_name
,
"
application_name
"
:
priv
.
application_name
,
"
username
"
:
priv
.
username
"
username
"
:
priv
.
username
,
"
mode
"
:
priv
.
mode
};
};
};
};
...
@@ -152,13 +188,13 @@
...
@@ -152,13 +188,13 @@
if
(
!
doc_id
)
{
if
(
!
doc_id
)
{
doc_id
=
generateUuid
();
doc_id
=
generateUuid
();
}
}
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc_id
);
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
doc_id
);
if
(
doc
===
null
)
{
if
(
doc
===
null
)
{
// the document does not exist
// the document does not exist
doc
=
command
.
cloneDoc
();
doc
=
command
.
cloneDoc
();
doc
.
_id
=
doc_id
;
doc
.
_id
=
doc_id
;
delete
doc
.
_attachments
;
delete
doc
.
_attachments
;
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
doc_id
,
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
doc_id
,
doc
);
that
.
success
({
that
.
success
({
"
ok
"
:
true
,
"
ok
"
:
true
,
"
id
"
:
doc_id
"
id
"
:
doc_id
...
@@ -184,7 +220,7 @@
...
@@ -184,7 +220,7 @@
that
.
put
=
function
(
command
)
{
that
.
put
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
,
tmp
;
var
doc
,
tmp
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
===
null
)
{
if
(
doc
===
null
)
{
// the document does not exist
// the document does not exist
doc
=
command
.
cloneDoc
();
doc
=
command
.
cloneDoc
();
...
@@ -196,7 +232,7 @@
...
@@ -196,7 +232,7 @@
doc
=
tmp
;
doc
=
tmp
;
}
}
// write
// write
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
that
.
success
({
"
ok
"
:
true
,
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
()
"
id
"
:
command
.
getDocId
()
...
@@ -212,7 +248,7 @@
...
@@ -212,7 +248,7 @@
that
.
putAttachment
=
function
(
command
)
{
that
.
putAttachment
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
;
var
doc
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
if
(
doc
===
null
)
{
if
(
doc
===
null
)
{
// the document does not exist
// the document does not exist
that
.
error
({
that
.
error
({
...
@@ -234,11 +270,11 @@
...
@@ -234,11 +270,11 @@
};
};
// upload data
// upload data
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
(),
command
.
getAttachmentId
(),
command
.
getAttachmentData
());
command
.
getAttachmentData
());
// write document
// write document
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
that
.
success
({
that
.
success
({
"
ok
"
:
true
,
"
ok
"
:
true
,
"
id
"
:
command
.
getDocId
(),
"
id
"
:
command
.
getDocId
(),
...
@@ -254,7 +290,7 @@
...
@@ -254,7 +290,7 @@
*/
*/
that
.
get
=
function
(
command
)
{
that
.
get
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
=
local
storage
.
getItem
(
var
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
);
);
if
(
doc
!==
null
)
{
if
(
doc
!==
null
)
{
...
@@ -278,7 +314,7 @@
...
@@ -278,7 +314,7 @@
*/
*/
that
.
getAttachment
=
function
(
command
)
{
that
.
getAttachment
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
=
local
storage
.
getItem
(
var
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
()
"
/
"
+
command
.
getAttachmentId
()
);
);
...
@@ -304,7 +340,7 @@
...
@@ -304,7 +340,7 @@
that
.
remove
=
function
(
command
)
{
that
.
remove
=
function
(
command
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
var
doc
,
i
,
attachment_list
;
var
doc
,
i
,
attachment_list
;
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
attachment_list
=
[];
attachment_list
=
[];
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
)
{
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
)
{
if
(
typeof
doc
.
_attachments
===
"
object
"
)
{
if
(
typeof
doc
.
_attachments
===
"
object
"
)
{
...
@@ -324,10 +360,10 @@
...
@@ -324,10 +360,10 @@
"
reason
"
:
"
missing
"
"
reason
"
:
"
missing
"
});
});
}
}
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
// delete all attachments
// delete all attachments
for
(
i
=
0
;
i
<
attachment_list
.
length
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
attachment_list
.
length
;
i
+=
1
)
{
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
attachment_list
[
i
]);
"
/
"
+
attachment_list
[
i
]);
}
}
that
.
success
({
that
.
success
({
...
@@ -354,7 +390,7 @@
...
@@ -354,7 +390,7 @@
"
reason
"
:
"
missing
"
"
reason
"
:
"
missing
"
});
});
};
};
doc
=
local
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
doc
=
priv
.
storage
.
getItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
());
// remove attachment from document
// remove attachment from document
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
&&
if
(
doc
!==
null
&&
typeof
doc
===
"
object
"
&&
typeof
doc
.
_attachments
===
"
object
"
)
{
typeof
doc
.
_attachments
===
"
object
"
)
{
...
@@ -364,9 +400,9 @@
...
@@ -364,9 +400,9 @@
if
(
objectIsEmpty
(
doc
.
_attachments
))
{
if
(
objectIsEmpty
(
doc
.
_attachments
))
{
delete
doc
.
_attachments
;
delete
doc
.
_attachments
;
}
}
local
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
priv
.
storage
.
setItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
(),
doc
);
doc
);
local
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
priv
.
storage
.
removeItem
(
priv
.
localpath
+
"
/
"
+
command
.
getDocId
()
+
"
/
"
+
command
.
getAttachmentId
());
"
/
"
+
command
.
getAttachmentId
());
that
.
success
({
that
.
success
({
"
ok
"
:
true
,
"
ok
"
:
true
,
...
@@ -401,15 +437,15 @@
...
@@ -401,15 +437,15 @@
option
.
select_list
===
undefined
&&
option
.
select_list
===
undefined
&&
option
.
include_docs
===
undefined
))
{
option
.
include_docs
===
undefined
))
{
rows
=
[];
rows
=
[];
for
(
i
in
localStorag
e
)
{
for
(
i
in
priv
.
databas
e
)
{
if
(
localStorag
e
.
hasOwnProperty
(
i
))
{
if
(
priv
.
databas
e
.
hasOwnProperty
(
i
))
{
// filter non-documents
// filter non-documents
if
(
path_re
.
test
(
i
))
{
if
(
path_re
.
test
(
i
))
{
row
=
{
value
:
{}
};
row
=
{
value
:
{}
};
row
.
id
=
i
.
split
(
'
/
'
).
slice
(
-
1
)[
0
];
row
.
id
=
i
.
split
(
'
/
'
).
slice
(
-
1
)[
0
];
row
.
key
=
row
.
id
;
row
.
key
=
row
.
id
;
if
(
command
.
getOption
(
'
include_docs
'
))
{
if
(
command
.
getOption
(
'
include_docs
'
))
{
row
.
doc
=
JSON
.
parse
(
localS
torage
.
getItem
(
i
));
row
.
doc
=
JSON
.
parse
(
priv
.
s
torage
.
getItem
(
i
));
}
}
rows
.
push
(
row
);
rows
.
push
(
row
);
}
}
...
@@ -418,10 +454,10 @@
...
@@ -418,10 +454,10 @@
that
.
success
({
"
rows
"
:
rows
,
"
total_rows
"
:
rows
.
length
});
that
.
success
({
"
rows
"
:
rows
,
"
total_rows
"
:
rows
.
length
});
}
else
{
}
else
{
// create complex query object from returned results
// create complex query object from returned results
for
(
i
in
localStorag
e
)
{
for
(
i
in
priv
.
databas
e
)
{
if
(
localStorag
e
.
hasOwnProperty
(
i
))
{
if
(
priv
.
databas
e
.
hasOwnProperty
(
i
))
{
if
(
path_re
.
test
(
i
))
{
if
(
path_re
.
test
(
i
))
{
document_list
.
push
(
local
storage
.
getItem
(
i
));
document_list
.
push
(
priv
.
storage
.
getItem
(
i
));
}
}
}
}
}
}
...
...
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