Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
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
Roque
jio
Commits
f31d0c39
Commit
f31d0c39
authored
Nov 08, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gidstorage.js upgraded for JIO v2
parent
df53bc45
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
196 additions
and
212 deletions
+196
-212
src/jio.storage/gidstorage.js
src/jio.storage/gidstorage.js
+196
-212
No files found.
src/jio.storage/gidstorage.js
View file @
f31d0c39
...
...
@@ -17,7 +17,7 @@
*/
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global define, jIO
, setTimeout
*/
/*global define, jIO */
/**
* JIO GID Storage. Type = 'gid'.
...
...
@@ -55,7 +55,7 @@
}([
'
jio
'
],
function
(
jIO
)
{
"
use strict
"
;
var
dcmi_types
,
metadata_actions
,
content_type_re
;
var
dcmi_types
,
metadata_actions
,
content_type_re
,
tool
;
dcmi_types
=
{
'
Collection
'
:
'
Collection
'
,
'
Dataset
'
:
'
Dataset
'
,
...
...
@@ -163,8 +163,14 @@
}
}
};
content_type_re
=
/^
([
a-z
]
+
\/[
a-zA-Z0-9
\+\-\.]
+
)(?:\s
*;
\s
*charset
\s
*=
\s
*
([
a-zA-Z0-9
\-]
+
))?
$/
;
content_type_re
=
new
RegExp
(
'
^([a-z]+
\\
/[a-zA-Z0-9
\\
+
\\
-
\\
.]+)
'
+
'
((?:
\\
s*;
\\
s*[a-zA-Z
\\
+
\\
-
\\
.]+
\\
s*=
'
+
'
\\
s*[a-zA-Z0-9
\\
-
\\
+
\\
.,]+)*)$
'
);
tool
=
{
"
deepClone
"
:
jIO
.
util
.
deepClone
};
/**
* Creates a gid from metadata and constraints.
...
...
@@ -213,10 +219,9 @@
* Convert a gid to a complex query.
*
* @param {Object,String} gid The gid
* @param {Object} constraints The constraints
* @return {Object} A complex serialized object
*/
function
gidToComplexQuery
(
gid
,
contraints
)
{
function
gidToComplexQuery
(
gid
)
{
var
k
,
i
,
result
=
[],
meta
,
content
;
if
(
typeof
gid
===
'
string
'
)
{
gid
=
JSON
.
parse
(
gid
);
...
...
@@ -276,10 +281,10 @@
* to generate global ids can be define in the storage description. It allows
* us use duplicating storage with different sub storage kind.
*
* @class
g
idStorage
* @class
G
idStorage
*/
function
gidStorage
(
spec
,
my
)
{
var
that
=
my
.
basicStorage
(
spec
,
my
)
,
priv
=
{};
function
GidStorage
(
spec
)
{
var
that
=
this
,
priv
=
{};
priv
.
sub_storage
=
spec
.
sub_storage
;
priv
.
constraints
=
spec
.
constraints
||
{
...
...
@@ -289,15 +294,6 @@
}
};
// Overrides
that
.
specToStore
=
function
()
{
return
{
"
sub_storage
"
:
priv
.
sub_storage
,
"
constraints
"
:
priv
.
constraints
};
};
// JIO Commands
/**
...
...
@@ -313,54 +309,50 @@
* @param {Command} command The JIO command
* @param {String} method The command method
*/
priv
.
putOrPost
=
function
(
command
,
method
)
{
setTimeout
(
function
()
{
var
gid
,
complex_query
,
doc
=
command
.
cloneDoc
();
priv
.
putOrPost
=
function
(
command
,
metadata
,
method
)
{
var
gid
,
complex_query
,
doc
=
tool
.
deepClone
(
metadata
);
gid
=
gidFormat
(
doc
,
priv
.
constraints
);
if
(
gid
===
undefined
||
(
doc
.
_id
&&
gid
!==
doc
.
_id
))
{
return
that
.
error
({
"
status
"
:
400
,
"
statusText
"
:
"
Bad Request
"
,
"
error
"
:
"
bad_request
"
,
"
message
"
:
"
Cannot
"
+
method
+
"
document
"
,
"
reason
"
:
"
metadata should respect constraints
"
});
return
command
.
error
(
"
bad_request
"
,
"
metadata should respect constraints
"
,
"
Cannot
"
+
method
+
"
document
"
);
}
complex_query
=
gidToComplexQuery
(
gid
);
that
.
addJob
(
'
allDocs
'
,
priv
.
sub_storage
,
{},
{
command
.
storage
(
priv
.
sub_storage
).
allDocs
(
{
"
query
"
:
complex_query
,
"
wildcard_character
"
:
null
},
function
(
response
)
{
}).
then
(
function
(
response
)
{
var
update_method
=
method
;
response
=
response
.
data
;
if
(
response
.
total_rows
!==
0
)
{
if
(
method
===
'
post
'
)
{
return
that
.
error
({
"
status
"
:
409
,
"
statusText
"
:
"
Conflict
"
,
"
error
"
:
"
conflict
"
,
"
message
"
:
"
Cannot
"
+
method
+
"
document
"
,
"
reason
"
:
"
Document already exist
"
});
return
command
.
error
(
"
conflict
"
,
"
Document already exist
"
,
"
Cannot
"
+
method
+
"
document
"
);
}
doc
=
command
.
cloneDoc
(
);
doc
=
tool
.
deepClone
(
metadata
);
doc
.
_id
=
response
.
rows
[
0
].
id
;
}
else
{
doc
=
command
.
cloneDoc
(
);
doc
=
tool
.
deepClone
(
metadata
);
delete
doc
.
_id
;
update_method
=
'
post
'
;
}
that
.
addJob
(
update_method
,
priv
.
sub_storage
,
doc
,
{
},
function
(
response
)
{
command
.
storage
(
priv
.
sub_storage
)[
update_method
](
doc
).
then
(
function
(
response
)
{
response
.
id
=
gid
;
that
.
success
(
response
);
command
.
success
(
response
);
},
function
(
err
)
{
err
.
message
=
"
Cannot
"
+
method
+
"
document
"
;
that
.
error
(
err
);
command
.
error
(
err
);
});
},
function
(
err
)
{
err
.
message
=
"
Cannot
"
+
method
+
"
document
"
;
that
.
error
(
err
);
});
command
.
error
(
err
);
});
};
...
...
@@ -373,52 +365,49 @@
*
* @method putGetOrRemoveAttachment
* @private
* @param {Command} command The JIO command
* @param {Object} command The JIO command
* @param {Object} doc The command parameters
* @param {String} method The command method
*/
priv
.
putGetOrRemoveAttachment
=
function
(
command
,
method
)
{
setTimeout
(
function
()
{
var
gid_object
,
complex_query
,
doc
=
command
.
cloneDoc
();
priv
.
putGetOrRemoveAttachment
=
function
(
command
,
doc
,
method
)
{
var
gid_object
,
complex_query
;
gid_object
=
gidParse
(
doc
.
_id
,
priv
.
constraints
);
if
(
gid_object
===
undefined
)
{
return
that
.
error
({
"
status
"
:
400
,
"
statusText
"
:
"
Bad Request
"
,
"
error
"
:
"
bad_request
"
,
"
message
"
:
"
Cannot
"
+
method
+
"
attachment
"
,
"
reason
"
:
"
metadata should respect constraints
"
});
return
command
.
error
(
"
bad_request
"
,
"
metadata should respect constraints
"
,
"
Cannot
"
+
method
+
"
attachment
"
);
}
complex_query
=
gidToComplexQuery
(
gid_object
);
that
.
addJob
(
'
allDocs
'
,
priv
.
sub_storage
,
{},
{
command
.
storage
(
priv
.
sub_storage
).
allDocs
(
{
"
query
"
:
complex_query
,
"
wildcard_character
"
:
null
},
function
(
response
)
{
}).
then
(
function
(
response
)
{
response
=
response
.
data
;
if
(
response
.
total_rows
===
0
)
{
return
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot
"
+
method
+
"
attachment
"
,
"
reason
"
:
"
Document already exist
"
});
return
command
.
error
(
"
not_found
"
,
"
Document already exist
"
,
"
Cannot
"
+
method
+
"
attachment
"
);
}
gid_object
=
doc
.
_id
;
doc
.
_id
=
response
.
rows
[
0
].
id
;
that
.
addJob
(
method
+
"
Attachment
"
,
priv
.
sub_storage
,
doc
,
{
},
function
(
response
)
{
command
.
storage
(
priv
.
sub_storage
)[
method
+
"
Attachment
"
](
doc
).
then
(
function
(
response
)
{
if
(
method
!==
'
get
'
)
{
response
.
id
=
gid_object
;
}
that
.
success
(
response
);
command
.
success
(
response
);
},
function
(
err
)
{
err
.
message
=
"
Cannot
"
+
method
+
"
attachment
"
;
that
.
error
(
err
);
command
.
error
(
err
);
});
},
function
(
err
)
{
err
.
message
=
"
Cannot
"
+
method
+
"
attachment
"
;
that
.
error
(
err
);
});
command
.
error
(
err
);
});
};
...
...
@@ -428,8 +417,8 @@
* @method post
* @param {Command} command The JIO command
*/
that
.
post
=
function
(
command
)
{
priv
.
putOrPost
(
command
,
'
post
'
);
that
.
post
=
function
(
command
,
metadata
)
{
priv
.
putOrPost
(
command
,
metadata
,
'
post
'
);
};
/**
...
...
@@ -438,8 +427,8 @@
* @method put
* @param {Command} command The JIO command
*/
that
.
put
=
function
(
command
)
{
priv
.
putOrPost
(
command
,
'
put
'
);
that
.
put
=
function
(
command
,
metadata
)
{
priv
.
putOrPost
(
command
,
metadata
,
'
put
'
);
};
/**
...
...
@@ -449,50 +438,45 @@
* @method putAttachment
* @param {Command} command The JIO command
*/
that
.
putAttachment
=
function
(
command
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
'
put
'
);
that
.
putAttachment
=
function
(
command
,
param
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
param
,
'
put
'
);
};
/**
* Gets a document thank to its gid, a sub allDocs and a complex query.
*
* @method get
* @param {
Command
} command The JIO command
* @param {
Object
} command The JIO command
*/
that
.
get
=
function
(
command
)
{
setTimeout
(
function
()
{
that
.
get
=
function
(
command
,
param
)
{
var
gid_object
,
complex_query
;
gid_object
=
gidParse
(
command
.
getDocId
()
,
priv
.
constraints
);
gid_object
=
gidParse
(
param
.
_id
,
priv
.
constraints
);
if
(
gid_object
===
undefined
)
{
return
that
.
error
({
"
status
"
:
400
,
"
statusText
"
:
"
Bad Request
"
,
"
error
"
:
"
bad_request
"
,
"
message
"
:
"
Cannot get document
"
,
"
reason
"
:
"
metadata should respect constraints
"
});
return
command
.
error
(
"
bad_request
"
,
"
metadata should respect constraints
"
,
"
Cannot get document
"
);
}
complex_query
=
gidToComplexQuery
(
gid_object
);
that
.
addJob
(
'
allDocs
'
,
priv
.
sub_storage
,
{},
{
command
.
storage
(
priv
.
sub_storage
).
allDocs
(
{
"
query
"
:
complex_query
,
"
wildcard_character
"
:
null
,
"
include_docs
"
:
true
},
function
(
response
)
{
}).
then
(
function
(
response
)
{
response
=
response
.
data
;
if
(
response
.
total_rows
===
0
)
{
return
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not Found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot get document
"
,
"
reason
"
:
"
missing
"
});
}
response
.
rows
[
0
].
doc
.
_id
=
command
.
getDocId
();
return
that
.
success
(
response
.
rows
[
0
].
doc
);
return
command
.
error
(
"
not_found
"
,
"
missing
"
,
"
Cannot get document
"
);
}
response
.
rows
[
0
].
doc
.
_id
=
param
.
_id
;
return
command
.
success
({
"
data
"
:
response
.
rows
[
0
].
doc
});
},
function
(
err
)
{
err
.
message
=
"
Cannot get document
"
;
return
that
.
error
(
err
);
});
return
command
.
error
(
err
);
});
};
...
...
@@ -503,8 +487,8 @@
* @method getAttachment
* @param {Command} command The JIO command
*/
that
.
getAttachment
=
function
(
command
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
'
get
'
);
that
.
getAttachment
=
function
(
command
,
param
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
param
,
'
get
'
);
};
/**
...
...
@@ -513,47 +497,43 @@
* @method remove
* @param {Command} command The JIO command.
*/
that
.
remove
=
function
(
command
)
{
setTimeout
(
function
()
{
var
gid_object
,
complex_query
,
doc
=
command
.
cloneDoc
();
that
.
remove
=
function
(
command
,
doc
)
{
var
gid_object
,
complex_query
;
gid_object
=
gidParse
(
doc
.
_id
,
priv
.
constraints
);
if
(
gid_object
===
undefined
)
{
return
that
.
error
({
"
status
"
:
400
,
"
statusText
"
:
"
Bad Request
"
,
"
error
"
:
"
bad_request
"
,
"
message
"
:
"
Cannot remove document
"
,
"
reason
"
:
"
metadata should respect constraints
"
});
return
command
.
error
(
"
bad_request
"
,
"
metadata should respect constraints
"
,
"
Cannot remove document
"
);
}
complex_query
=
gidToComplexQuery
(
gid_object
);
that
.
addJob
(
'
allDocs
'
,
priv
.
sub_storage
,
{},
{
command
.
storage
(
priv
.
sub_storage
).
allDocs
(
{
"
query
"
:
complex_query
,
"
wildcard_character
"
:
null
},
function
(
response
)
{
}).
then
(
function
(
response
)
{
response
=
response
.
data
;
if
(
response
.
total_rows
===
0
)
{
return
that
.
error
({
"
status
"
:
404
,
"
statusText
"
:
"
Not found
"
,
"
error
"
:
"
not_found
"
,
"
message
"
:
"
Cannot remove document
"
,
"
reason
"
:
"
missing
"
});
return
command
.
error
(
"
not_found
"
,
"
missing
"
,
"
Cannot remove document
"
);
}
gid_object
=
doc
.
_id
;
doc
=
{
"
_id
"
:
response
.
rows
[
0
].
id
};
that
.
addJob
(
'
remove
'
,
priv
.
sub_storage
,
doc
,
{
},
function
(
response
)
{
command
.
storage
(
priv
.
sub_storage
).
remove
(
doc
).
then
(
function
(
response
)
{
response
.
id
=
gid_object
;
that
.
success
(
response
);
command
.
success
(
response
);
},
function
(
err
)
{
err
.
message
=
"
Cannot remove document
"
;
that
.
error
(
err
);
command
.
error
(
err
);
});
},
function
(
err
)
{
err
.
message
=
"
Cannot remove document
"
;
that
.
error
(
err
);
});
command
.
error
(
err
);
});
};
...
...
@@ -564,8 +544,8 @@
* @method removeAttachment
* @param {Command} command The JIO command
*/
that
.
removeAttachment
=
function
(
command
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
'
remove
'
);
that
.
removeAttachment
=
function
(
command
,
param
)
{
priv
.
putGetOrRemoveAttachment
(
command
,
param
,
'
remove
'
);
};
/**
...
...
@@ -574,16 +554,20 @@
* @method allDocs
* @param {Command} command The JIO command
*/
that
.
allDocs
=
function
(
command
)
{
setTimeout
(
function
()
{
var
options
=
command
.
cloneOption
(),
include_docs
;
that
.
allDocs
=
function
(
command
,
param
,
options
)
{
/*jslint unparam: true */
var
include_docs
;
include_docs
=
options
.
include_docs
;
options
.
include_docs
=
true
;
that
.
addJob
(
'
allDocs
'
,
priv
.
sub_storage
,
{
},
options
,
function
(
response
)
{
var
result
=
[],
doc_gids
=
{},
i
,
row
,
gid
;
command
.
storage
(
priv
.
sub_storage
).
allDocs
(
options
).
then
(
function
(
response
)
{
/*jslint ass: true */
var
result
=
[],
doc_gids
=
{},
row
,
gid
;
response
=
response
.
data
;
while
((
row
=
response
.
rows
.
shift
())
!==
undefined
)
{
if
((
gid
=
gidFormat
(
row
.
doc
,
priv
.
constraints
))
!==
undefined
)
{
gid
=
gidFormat
(
row
.
doc
,
priv
.
constraints
);
if
(
gid
!==
undefined
)
{
if
(
!
doc_gids
[
gid
])
{
doc_gids
[
gid
]
=
true
;
row
.
id
=
gid
;
...
...
@@ -599,17 +583,17 @@
}
doc_gids
=
undefined
;
// free memory
row
=
undefined
;
that
.
success
({
"
total_rows
"
:
result
.
length
,
"
rows
"
:
result
});
command
.
success
({
"
data
"
:
{
"
total_rows
"
:
result
.
length
,
"
rows
"
:
result
}});
},
function
(
err
)
{
err
.
message
=
"
Cannot get all documents
"
;
return
that
.
error
(
err
);
});
return
command
.
error
(
err
);
});
};
return
that
;
}
jIO
.
addStorage
Type
(
'
gid
'
,
g
idStorage
);
jIO
.
addStorage
(
'
gid
'
,
G
idStorage
);
}));
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