Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio-main
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
Hardik Juneja
jio-main
Commits
09cc6aec
Commit
09cc6aec
authored
Oct 26, 2016
by
Vincent Bechu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mappingstorage: upgrade code quality
parent
d4671ec4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
66 deletions
+67
-66
src/jio.storage/mappingstorage.js
src/jio.storage/mappingstorage.js
+56
-57
test/jio.storage/mappingstorage.tests.js
test/jio.storage/mappingstorage.tests.js
+11
-9
No files found.
src/jio.storage/mappingstorage.js
View file @
09cc6aec
/*jslint indent:2, maxlen: 80, nomen: true */
/*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/
(
function
(
jIO
,
RSVP
)
{
(
function
(
jIO
,
RSVP
)
{
"
use strict
"
;
"
use strict
"
;
...
@@ -10,6 +11,9 @@
...
@@ -10,6 +11,9 @@
this
.
_mapping_dict_attachment
=
spec
.
mapping_dict_attachment
||
{};
this
.
_mapping_dict_attachment
=
spec
.
mapping_dict_attachment
||
{};
this
.
_query
=
spec
.
query
||
{};
this
.
_query
=
spec
.
query
||
{};
if
(
this
.
_query
.
query
!==
undefined
)
{
this
.
_query
.
query
=
QueryFactory
.
create
(
this
.
_query
.
query
);
}
this
.
_id_is_mapped
=
(
this
.
_mapping_dict
.
id
!==
undefined
this
.
_id_is_mapped
=
(
this
.
_mapping_dict
.
id
!==
undefined
&&
this
.
_mapping_dict
.
id
.
equal
!==
"
id
"
);
&&
this
.
_mapping_dict
.
id
.
equal
!==
"
id
"
);
var
property
,
query_list
=
[];
var
property
,
query_list
=
[];
...
@@ -65,10 +69,19 @@
...
@@ -65,10 +69,19 @@
return
id
;
return
id
;
}
}
if
(
storage
.
_mapping_dict
.
id
.
equal
!==
undefined
)
{
if
(
storage
.
_mapping_dict
.
id
.
equal
!==
undefined
)
{
query
=
storage
.
_mapping_dict
.
id
.
equal
+
'
: "
'
+
id
+
'
"
'
;
query
=
new
SimpleQuery
({
key
:
storage
.
_mapping_dict
.
id
.
equal
,
value
:
id
,
type
:
"
simple
"
});
if
(
storage
.
_query
.
query
!==
undefined
)
{
if
(
storage
.
_query
.
query
!==
undefined
)
{
query
+=
'
AND
'
+
storage
.
_query
.
query
;
query
=
new
ComplexQuery
({
operator
:
"
AND
"
,
query_list
:
[
query
,
storage
.
_query
.
query
],
type
:
"
complex
"
});
}
}
query
=
Query
.
objectToSearchText
(
query
);
return
storage
.
_sub_storage
.
allDocs
({
return
storage
.
_sub_storage
.
allDocs
({
"
query
"
:
query
,
"
query
"
:
query
,
"
sort_on
"
:
storage
.
_query
.
sort_on
,
"
sort_on
"
:
storage
.
_query
.
sort_on
,
...
@@ -129,7 +142,7 @@
...
@@ -129,7 +142,7 @@
return
false
;
return
false
;
}
}
function
mapDocument
(
storage
,
doc
,
delete_id
)
{
function
mapDocument
(
storage
,
doc
,
id_delete
)
{
var
mapped_doc
=
{},
var
mapped_doc
=
{},
property
,
property
,
property_list
=
[];
property_list
=
[];
...
@@ -147,7 +160,7 @@
...
@@ -147,7 +160,7 @@
}
}
}
}
}
}
if
(
delete_id
)
{
if
(
id_delete
)
{
delete
mapped_doc
.
id
;
delete
mapped_doc
.
id
;
}
}
return
mapped_doc
;
return
mapped_doc
;
...
@@ -174,13 +187,13 @@
...
@@ -174,13 +187,13 @@
}
}
MappingStorage
.
prototype
.
get
=
function
(
id
)
{
MappingStorage
.
prototype
.
get
=
function
(
id
)
{
var
tha
t
=
this
;
var
contex
t
=
this
;
return
getSubStorageId
(
this
,
id
)
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
.
push
(
function
(
sub_i
d
)
{
return
that
.
_sub_storage
.
get
(
id_mappe
d
);
return
context
.
_sub_storage
.
get
(
sub_i
d
);
})
})
.
push
(
function
(
doc
)
{
.
push
(
function
(
doc
)
{
return
mapDocument
(
tha
t
,
doc
,
true
);
return
mapDocument
(
contex
t
,
doc
,
true
);
})
})
.
push
(
undefined
,
function
(
error
)
{
.
push
(
undefined
,
function
(
error
)
{
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
+
id
throw
new
jIO
.
util
.
jIOError
(
"
Cannot find document
"
+
id
...
@@ -198,20 +211,20 @@
...
@@ -198,20 +211,20 @@
};
};
MappingStorage
.
prototype
.
put
=
function
(
id
,
doc
)
{
MappingStorage
.
prototype
.
put
=
function
(
id
,
doc
)
{
var
tha
t
=
this
,
var
contex
t
=
this
,
mapped_doc
=
unmapDocument
(
this
,
doc
);
mapped_doc
=
unmapDocument
(
this
,
doc
);
return
getSubStorageId
(
this
,
id
)
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
.
push
(
function
(
sub_i
d
)
{
if
(
tha
t
.
_id_is_mapped
)
{
if
(
contex
t
.
_id_is_mapped
)
{
mapped_doc
[
tha
t
.
_mapping_dict
.
id
.
equal
]
=
id
;
mapped_doc
[
contex
t
.
_mapping_dict
.
id
.
equal
]
=
id
;
}
}
if
(
id
===
undefined
)
{
if
(
id
===
undefined
)
{
throw
new
Error
();
throw
new
Error
();
}
}
return
that
.
_sub_storage
.
put
(
id_mappe
d
,
mapped_doc
);
return
context
.
_sub_storage
.
put
(
sub_i
d
,
mapped_doc
);
})
})
.
push
(
undefined
,
function
()
{
.
push
(
undefined
,
function
()
{
return
tha
t
.
_sub_storage
.
post
(
mapped_doc
);
return
contex
t
.
_sub_storage
.
post
(
mapped_doc
);
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
id
;
return
id
;
...
@@ -219,10 +232,10 @@
...
@@ -219,10 +232,10 @@
};
};
MappingStorage
.
prototype
.
remove
=
function
(
id
)
{
MappingStorage
.
prototype
.
remove
=
function
(
id
)
{
var
tha
t
=
this
;
var
contex
t
=
this
;
return
getSubStorageId
(
this
,
id
)
return
getSubStorageId
(
this
,
id
)
.
push
(
function
(
id_mappe
d
)
{
.
push
(
function
(
sub_i
d
)
{
return
that
.
_sub_storage
.
remove
(
id_mappe
d
);
return
context
.
_sub_storage
.
remove
(
sub_i
d
);
})
})
.
push
(
function
()
{
.
push
(
function
()
{
return
id
;
return
id
;
...
@@ -281,20 +294,11 @@
...
@@ -281,20 +294,11 @@
MappingStorage
.
prototype
.
bulk
=
function
(
id_list
)
{
MappingStorage
.
prototype
.
bulk
=
function
(
id_list
)
{
var
i
,
var
i
,
tha
t
=
this
,
contex
t
=
this
,
mapped_result
=
[],
mapped_result
=
[],
promise_list
=
id_list
.
map
(
function
(
parameter
)
{
promise_list
=
id_list
.
map
(
function
(
parameter
)
{
return
getSubStorageId
(
tha
t
,
parameter
.
parameter_list
[
0
])
return
getSubStorageId
(
contex
t
,
parameter
.
parameter_list
[
0
])
.
push
(
function
(
id
)
{
.
push
(
function
(
id
)
{
if
(
parameter
.
method
===
"
put
"
)
{
return
{
"
method
"
:
parameter
.
method
,
"
parameter_list
"
:
[
id
,
unmapDocument
(
parameter
.
parameter_list
[
1
])
]
};
}
return
{
"
method
"
:
parameter
.
method
,
"
parameter_list
"
:
[
id
]};
return
{
"
method
"
:
parameter
.
method
,
"
parameter_list
"
:
[
id
]};
});
});
});
});
...
@@ -304,18 +308,18 @@
...
@@ -304,18 +308,18 @@
return
RSVP
.
all
(
promise_list
);
return
RSVP
.
all
(
promise_list
);
})
})
.
push
(
function
(
id_list_mapped
)
{
.
push
(
function
(
id_list_mapped
)
{
return
tha
t
.
_sub_storage
.
bulk
(
id_list_mapped
);
return
contex
t
.
_sub_storage
.
bulk
(
id_list_mapped
);
})
})
.
push
(
function
(
result
)
{
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
length
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
result
.
length
;
i
+=
1
)
{
mapped_result
.
push
(
mapDocument
(
tha
t
,
result
[
i
],
false
));
mapped_result
.
push
(
mapDocument
(
contex
t
,
result
[
i
],
false
));
}
}
return
mapped_result
;
return
mapped_result
;
});
});
};
};
MappingStorage
.
prototype
.
buildQuery
=
function
(
option
)
{
MappingStorage
.
prototype
.
buildQuery
=
function
(
option
)
{
var
tha
t
=
this
,
var
contex
t
=
this
,
i
,
i
,
query
,
query
,
property
,
property
,
...
@@ -323,27 +327,16 @@
...
@@ -323,27 +327,16 @@
sort_on
=
[];
sort_on
=
[];
function
mapQuery
(
one_query
)
{
function
mapQuery
(
one_query
)
{
var
i
,
result
=
"
(
"
,
key
;
var
i
,
query_list
=
[]
;
if
(
one_query
.
type
===
"
complex
"
)
{
if
(
one_query
.
type
===
"
complex
"
)
{
for
(
i
=
0
;
i
<
one_query
.
query_list
.
length
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
one_query
.
query_list
.
length
;
i
+=
1
)
{
result
+=
"
(
"
+
mapQuery
(
one_query
.
query_list
[
i
])
+
"
)
"
;
query_list
.
push
(
mapQuery
(
one_query
.
query_list
[
i
]));
if
(
i
<
one_query
.
query_list
.
length
-
1
)
{
result
+=
"
"
+
one_query
.
operator
+
"
"
;
}
}
result
+=
"
)
"
;
return
result
;
}
if
(
that
.
_mapping_dict
.
hasOwnProperty
(
one_query
.
key
))
{
key
=
that
.
_mapping_dict
[
one_query
.
key
].
equal
;
}
else
{
if
(
that
.
_map_all_property
)
{
key
=
one_query
.
key
;
}
}
one_query
.
query_list
=
query_list
;
return
one_query
;
}
}
return
(
key
?
key
+
"
:
"
:
""
)
+
one_query
.
key
=
mapProperty
(
context
,
one_query
.
key
,
{},
{});
(
one_query
.
operator
?
"
"
+
one_query
.
operator
:
""
)
+
return
one_query
;
'
"
'
+
one_query
.
value
+
'
"
'
;
}
}
if
(
option
.
sort_on
!==
undefined
)
{
if
(
option
.
sort_on
!==
undefined
)
{
...
@@ -382,16 +375,22 @@
...
@@ -382,16 +375,22 @@
select_list
.
push
(
this
.
_mapping_dict
.
id
.
equal
);
select_list
.
push
(
this
.
_mapping_dict
.
id
.
equal
);
}
}
if
(
option
.
query
!==
undefined
)
{
if
(
option
.
query
!==
undefined
)
{
query
=
mapQuery
(
jIO
.
QueryFactory
.
create
(
option
.
query
));
query
=
mapQuery
(
QueryFactory
.
create
(
option
.
query
));
}
}
if
(
this
.
_query
.
query
!==
undefined
)
{
if
(
this
.
_query
.
query
!==
undefined
)
{
if
(
query
!==
undefined
)
{
if
(
query
===
undefined
)
{
query
+=
'
AND
'
;
query
=
this
.
_query
.
query
;
}
else
{
query
=
""
;
}
}
query
+=
this
.
_query
.
query
;
query
=
new
ComplexQuery
({
operator
:
"
AND
"
,
query_list
:
[
query
,
this
.
_query
.
query
],
type
:
"
complex
"
});
}
if
(
query
!==
undefined
)
{
query
=
Query
.
objectToSearchText
(
query
);
}
}
return
this
.
_sub_storage
.
allDocs
(
return
this
.
_sub_storage
.
allDocs
(
{
{
...
@@ -404,8 +403,8 @@
...
@@ -404,8 +403,8 @@
.
push
(
function
(
result
)
{
.
push
(
function
(
result
)
{
for
(
i
=
0
;
i
<
result
.
data
.
total_rows
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
result
.
data
.
total_rows
;
i
+=
1
)
{
result
.
data
.
rows
[
i
].
value
=
result
.
data
.
rows
[
i
].
value
=
mapDocument
(
tha
t
,
result
.
data
.
rows
[
i
].
value
,
false
);
mapDocument
(
contex
t
,
result
.
data
.
rows
[
i
].
value
,
false
);
if
(
result
.
data
.
rows
[
i
].
id
!==
undefined
&&
tha
t
.
_id_is_mapped
)
{
if
(
result
.
data
.
rows
[
i
].
id
!==
undefined
&&
contex
t
.
_id_is_mapped
)
{
result
.
data
.
rows
[
i
].
id
=
result
.
data
.
rows
[
i
].
id
=
result
.
data
.
rows
[
i
].
value
.
id
;
result
.
data
.
rows
[
i
].
value
.
id
;
delete
result
.
data
.
rows
[
i
].
value
.
id
;
delete
result
.
data
.
rows
[
i
].
value
.
id
;
...
...
test/jio.storage/mappingstorage.tests.js
View file @
09cc6aec
...
@@ -48,12 +48,14 @@
...
@@ -48,12 +48,14 @@
},
},
mapping_dict
:
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}},
mapping_dict
:
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}},
map_all_property
:
true
,
map_all_property
:
true
,
query
:
{
"
query
"
:
"
foo
"
},
query
:
{
"
query
"
:
'
foo: "bar"
'
},
mapping_dict_attachment
:
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}}
mapping_dict_attachment
:
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}}
});
});
deepEqual
(
jio
.
__storage
.
_mapping_dict
,
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}});
deepEqual
(
jio
.
__storage
.
_mapping_dict
,
{
"
bar
"
:
{
"
equal
"
:
"
foo
"
}});
deepEqual
(
jio
.
__storage
.
_query
,
{
"
query
"
:
"
foo
"
});
equal
(
jio
.
__storage
.
_query
.
query
.
key
,
"
foo
"
);
equal
(
jio
.
__storage
.
_query
.
query
.
value
,
"
bar
"
);
equal
(
jio
.
__storage
.
_query
.
query
.
type
,
"
simple
"
);
deepEqual
(
jio
.
__storage
.
_mapping_dict_attachment
,
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}});
deepEqual
(
jio
.
__storage
.
_mapping_dict_attachment
,
{
"
foo
"
:
{
"
get
"
:
"
bar
"
}});
equal
(
jio
.
__storage
.
_map_all_property
,
true
);
equal
(
jio
.
__storage
.
_map_all_property
,
true
);
});
});
...
@@ -144,7 +146,7 @@
...
@@ -144,7 +146,7 @@
};
};
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
equal
(
options
.
query
,
'
otherId: "42"
'
,
"
allDoc 2713 called
"
);
equal
(
options
.
query
,
'
otherId:
"42"
'
,
"
allDoc 2713 called
"
);
return
[{
id
:
"
2713
"
}];
return
[{
id
:
"
2713
"
}];
};
};
...
@@ -189,7 +191,7 @@
...
@@ -189,7 +191,7 @@
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
equal
(
equal
(
options
.
query
,
options
.
query
,
'
otherId: "42" AND otherTitle: "foo"
'
,
'
( otherId: "42" AND otherTitle: "foo" )
'
,
"
allDoc 2713 called
"
"
allDoc 2713 called
"
);
);
return
[{
id
:
"
2713
"
}];
return
[{
id
:
"
2713
"
}];
...
@@ -235,7 +237,7 @@
...
@@ -235,7 +237,7 @@
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
equal
(
equal
(
options
.
query
,
options
.
query
,
'
otherId: "42"
'
,
'
otherId:
"42"
'
,
"
allDoc 2713 called
"
"
allDoc 2713 called
"
);
);
return
[{
id
:
"
2713
"
}];
return
[{
id
:
"
2713
"
}];
...
@@ -348,7 +350,7 @@
...
@@ -348,7 +350,7 @@
};
};
Storage2713
.
prototype
.
buildQuery
=
function
(
option
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
option
)
{
equal
(
option
.
query
,
'
otherId: "42"
'
,
"
allDocs 2713 called
"
);
equal
(
option
.
query
,
'
otherId:
"42"
'
,
"
allDocs 2713 called
"
);
return
[];
return
[];
};
};
...
@@ -446,7 +448,7 @@
...
@@ -446,7 +448,7 @@
};
};
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
options
)
{
equal
(
options
.
query
,
'
otherId: "42"
'
,
"
allDoc 2713 called
"
);
equal
(
options
.
query
,
'
otherId:
"42"
'
,
"
allDoc 2713 called
"
);
return
[{
id
:
"
2713
"
}];
return
[{
id
:
"
2713
"
}];
};
};
...
@@ -905,10 +907,10 @@
...
@@ -905,10 +907,10 @@
};
};
Storage2713
.
prototype
.
buildQuery
=
function
(
option
)
{
Storage2713
.
prototype
.
buildQuery
=
function
(
option
)
{
if
(
option
.
query
===
'
otherId: "id1"
'
)
{
if
(
option
.
query
===
'
otherId:
"id1"
'
)
{
return
[{
id
:
"
foo
"
}];
return
[{
id
:
"
foo
"
}];
}
}
if
(
option
.
query
===
'
otherId: "id2"
'
)
{
if
(
option
.
query
===
'
otherId:
"id2"
'
)
{
return
[{
id
:
"
bar
"
}];
return
[{
id
:
"
bar
"
}];
}
}
throw
new
Error
(
"
invalid option:
"
+
option
.
toString
());
throw
new
Error
(
"
invalid option:
"
+
option
.
toString
());
...
...
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