Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
54b1314a
Commit
54b1314a
authored
Dec 14, 2019
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
value: optimize json parsing
parent
51c35d8b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/value.h
+24
-12
24 additions, 12 deletions
include/opendht/value.h
src/value.cpp
+39
-41
39 additions, 41 deletions
src/value.cpp
with
63 additions
and
53 deletions
include/opendht/value.h
+
24
−
12
View file @
54b1314a
...
@@ -43,7 +43,19 @@
...
@@ -43,7 +43,19 @@
namespace
dht
{
namespace
dht
{
static
const
std
::
string
VALUE_KEY_ID
(
"id"
);
static
const
std
::
string
VALUE_KEY_DAT
(
"dat"
);
static
const
std
::
string
VALUE_KEY_PRIO
(
"p"
);
static
const
std
::
string
VALUE_KEY_PRIO
(
"p"
);
static
const
std
::
string
VALUE_KEY_SIGNATURE
(
"sig"
);
static
const
std
::
string
VALUE_KEY_SEQ
(
"seq"
);
static
const
std
::
string
VALUE_KEY_DATA
(
"data"
);
static
const
std
::
string
VALUE_KEY_OWNER
(
"owner"
);
static
const
std
::
string
VALUE_KEY_TYPE
(
"type"
);
static
const
std
::
string
VALUE_KEY_TO
(
"to"
);
static
const
std
::
string
VALUE_KEY_BODY
(
"body"
);
static
const
std
::
string
VALUE_KEY_USERTYPE
(
"utype"
);
struct
Value
;
struct
Value
;
struct
Query
;
struct
Query
;
...
@@ -488,17 +500,17 @@ struct OPENDHT_PUBLIC Value
...
@@ -488,17 +500,17 @@ struct OPENDHT_PUBLIC Value
bool
has_owner
=
owner
&&
*
owner
;
bool
has_owner
=
owner
&&
*
owner
;
pk
.
pack_map
((
user_type
.
empty
()
?
0
:
1
)
+
(
has_owner
?
(
recipient
?
5
:
4
)
:
2
));
pk
.
pack_map
((
user_type
.
empty
()
?
0
:
1
)
+
(
has_owner
?
(
recipient
?
5
:
4
)
:
2
));
if
(
has_owner
)
{
// isSigned
if
(
has_owner
)
{
// isSigned
pk
.
pack
(
std
::
string
(
"seq"
)
);
pk
.
pack
(
seq
);
pk
.
pack
(
VALUE_KEY_SEQ
);
pk
.
pack
(
seq
);
pk
.
pack
(
std
::
string
(
"owner"
)
);
owner
->
msgpack_pack
(
pk
);
pk
.
pack
(
VALUE_KEY_OWNER
);
owner
->
msgpack_pack
(
pk
);
if
(
recipient
)
{
if
(
recipient
)
{
pk
.
pack
(
std
::
string
(
"to"
)
);
pk
.
pack
(
recipient
);
pk
.
pack
(
VALUE_KEY_TO
);
pk
.
pack
(
recipient
);
}
}
}
}
pk
.
pack
(
std
::
string
(
"type"
)
);
pk
.
pack
(
type
);
pk
.
pack
(
VALUE_KEY_TYPE
);
pk
.
pack
(
type
);
pk
.
pack
(
std
::
string
(
"data"
)
);
pk
.
pack_bin
(
data
.
size
());
pk
.
pack
(
VALUE_KEY_DATA
);
pk
.
pack_bin
(
data
.
size
());
pk
.
pack_bin_body
((
const
char
*
)
data
.
data
(),
data
.
size
());
pk
.
pack_bin_body
((
const
char
*
)
data
.
data
(),
data
.
size
());
if
(
not
user_type
.
empty
())
{
if
(
not
user_type
.
empty
())
{
pk
.
pack
(
std
::
string
(
"utype"
)
);
pk
.
pack
(
user_type
);
pk
.
pack
(
VALUE_KEY_USERTYPE
);
pk
.
pack
(
user_type
);
}
}
}
}
...
@@ -510,9 +522,9 @@ struct OPENDHT_PUBLIC Value
...
@@ -510,9 +522,9 @@ struct OPENDHT_PUBLIC Value
pk
.
pack_bin_body
((
const
char
*
)
cypher
.
data
(),
cypher
.
size
());
pk
.
pack_bin_body
((
const
char
*
)
cypher
.
data
(),
cypher
.
size
());
}
else
{
}
else
{
pk
.
pack_map
(
isSigned
()
?
2
:
1
);
pk
.
pack_map
(
isSigned
()
?
2
:
1
);
pk
.
pack
(
std
::
string
(
"body"
)
);
msgpack_pack_to_sign
(
pk
);
pk
.
pack
(
VALUE_KEY_BODY
);
msgpack_pack_to_sign
(
pk
);
if
(
isSigned
())
{
if
(
isSigned
())
{
pk
.
pack
(
std
::
string
(
"sig"
)
);
pk
.
pack_bin
(
signature
.
size
());
pk
.
pack
(
VALUE_KEY_SIGNATURE
);
pk
.
pack_bin
(
signature
.
size
());
pk
.
pack_bin_body
((
const
char
*
)
signature
.
data
(),
signature
.
size
());
pk
.
pack_bin_body
((
const
char
*
)
signature
.
data
(),
signature
.
size
());
}
}
}
}
...
@@ -522,8 +534,8 @@ struct OPENDHT_PUBLIC Value
...
@@ -522,8 +534,8 @@ struct OPENDHT_PUBLIC Value
void
msgpack_pack
(
Packer
&
pk
)
const
void
msgpack_pack
(
Packer
&
pk
)
const
{
{
pk
.
pack_map
(
2
+
(
priority
?
1
:
0
));
pk
.
pack_map
(
2
+
(
priority
?
1
:
0
));
pk
.
pack
(
std
::
string
(
"id"
)
);
pk
.
pack
(
id
);
pk
.
pack
(
VALUE_KEY_ID
);
pk
.
pack
(
id
);
pk
.
pack
(
std
::
string
(
"dat"
)
);
msgpack_pack_to_encrypt
(
pk
);
pk
.
pack
(
VALUE_KEY_DAT
);
msgpack_pack_to_encrypt
(
pk
);
if
(
priority
)
{
if
(
priority
)
{
pk
.
pack
(
VALUE_KEY_PRIO
);
pk
.
pack
(
priority
);
pk
.
pack
(
VALUE_KEY_PRIO
);
pk
.
pack
(
priority
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/value.cpp
+
39
−
41
View file @
54b1314a
...
@@ -96,15 +96,15 @@ Value::size() const
...
@@ -96,15 +96,15 @@ Value::size() const
void
void
Value
::
msgpack_unpack
(
const
msgpack
::
object
&
o
)
Value
::
msgpack_unpack
(
const
msgpack
::
object
&
o
)
{
{
if
(
o
.
type
!=
msgpack
::
type
::
MAP
)
throw
msgpack
::
type_error
();
if
(
o
.
type
!=
msgpack
::
type
::
MAP
or
o
.
via
.
map
.
size
<
2
)
if
(
o
.
via
.
map
.
size
<
2
)
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
if
(
auto
rid
=
findMapValue
(
o
,
"id"
))
{
if
(
auto
rid
=
findMapValue
(
o
,
VALUE_KEY_ID
))
{
id
=
rid
->
as
<
Id
>
();
id
=
rid
->
as
<
Id
>
();
}
else
}
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
if
(
auto
rdat
=
findMapValue
(
o
,
"dat"
))
{
if
(
auto
rdat
=
findMapValue
(
o
,
VALUE_KEY_DAT
))
{
msgpack_unpack_body
(
*
rdat
);
msgpack_unpack_body
(
*
rdat
);
}
else
}
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
...
@@ -130,37 +130,37 @@ Value::msgpack_unpack_body(const msgpack::object& o)
...
@@ -130,37 +130,37 @@ Value::msgpack_unpack_body(const msgpack::object& o)
}
else
{
}
else
{
if
(
o
.
type
!=
msgpack
::
type
::
MAP
)
if
(
o
.
type
!=
msgpack
::
type
::
MAP
)
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
auto
rbody
=
findMapValue
(
o
,
"body"
);
auto
rbody
=
findMapValue
(
o
,
VALUE_KEY_BODY
);
if
(
not
rbody
)
if
(
not
rbody
)
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
if
(
auto
rdata
=
findMapValue
(
*
rbody
,
"data"
))
{
if
(
auto
rdata
=
findMapValue
(
*
rbody
,
VALUE_KEY_DATA
))
{
data
=
unpackBlob
(
*
rdata
);
data
=
unpackBlob
(
*
rdata
);
}
else
}
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
if
(
auto
rtype
=
findMapValue
(
*
rbody
,
"type"
))
{
if
(
auto
rtype
=
findMapValue
(
*
rbody
,
VALUE_KEY_TYPE
))
{
type
=
rtype
->
as
<
ValueType
::
Id
>
();
type
=
rtype
->
as
<
ValueType
::
Id
>
();
}
else
}
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
if
(
auto
rutype
=
findMapValue
(
*
rbody
,
"utype"
))
{
if
(
auto
rutype
=
findMapValue
(
*
rbody
,
VALUE_KEY_USERTYPE
))
{
user_type
=
rutype
->
as
<
std
::
string
>
();
user_type
=
rutype
->
as
<
std
::
string
>
();
}
}
if
(
auto
rowner
=
findMapValue
(
*
rbody
,
"owner"
))
{
if
(
auto
rowner
=
findMapValue
(
*
rbody
,
VALUE_KEY_OWNER
))
{
if
(
auto
rseq
=
findMapValue
(
*
rbody
,
"seq"
))
if
(
auto
rseq
=
findMapValue
(
*
rbody
,
VALUE_KEY_SEQ
))
seq
=
rseq
->
as
<
decltype
(
seq
)
>
();
seq
=
rseq
->
as
<
decltype
(
seq
)
>
();
else
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
crypto
::
PublicKey
new_owner
;
crypto
::
PublicKey
new_owner
;
new_owner
.
msgpack_unpack
(
*
rowner
);
new_owner
.
msgpack_unpack
(
*
rowner
);
owner
=
std
::
make_shared
<
const
crypto
::
PublicKey
>
(
std
::
move
(
new_owner
));
owner
=
std
::
make_shared
<
const
crypto
::
PublicKey
>
(
std
::
move
(
new_owner
));
if
(
auto
rrecipient
=
findMapValue
(
*
rbody
,
"to"
))
{
if
(
auto
rrecipient
=
findMapValue
(
*
rbody
,
VALUE_KEY_TO
))
{
recipient
=
rrecipient
->
as
<
InfoHash
>
();
recipient
=
rrecipient
->
as
<
InfoHash
>
();
}
}
if
(
auto
rsig
=
findMapValue
(
o
,
"sig"
))
{
if
(
auto
rsig
=
findMapValue
(
o
,
VALUE_KEY_SIGNATURE
))
{
signature
=
unpackBlob
(
*
rsig
);
signature
=
unpackBlob
(
*
rsig
);
}
else
}
else
throw
msgpack
::
type_error
();
throw
msgpack
::
type_error
();
...
@@ -171,32 +171,32 @@ Value::msgpack_unpack_body(const msgpack::object& o)
...
@@ -171,32 +171,32 @@ Value::msgpack_unpack_body(const msgpack::object& o)
#ifdef OPENDHT_JSONCPP
#ifdef OPENDHT_JSONCPP
Value
::
Value
(
const
Json
::
Value
&
json
)
Value
::
Value
(
const
Json
::
Value
&
json
)
{
{
id
=
Value
::
Id
(
unpackId
(
json
,
"id"
));
id
=
Value
::
Id
(
unpackId
(
json
,
VALUE_KEY_ID
));
const
auto
&
jcypher
=
json
[
"cypher"
];
const
auto
&
jcypher
=
json
[
"cypher"
];
if
(
jcypher
.
isString
())
if
(
jcypher
.
isString
())
cypher
=
base64_decode
(
jcypher
.
asString
());
cypher
=
base64_decode
(
jcypher
.
asString
());
const
auto
&
jsig
=
json
[
"sig"
];
const
auto
&
jsig
=
json
[
VALUE_KEY_SIGNATURE
];
if
(
jsig
.
isString
())
if
(
jsig
.
isString
())
signature
=
base64_decode
(
jsig
.
asString
());
signature
=
base64_decode
(
jsig
.
asString
());
const
auto
&
jseq
=
json
[
"seq"
];
const
auto
&
jseq
=
json
[
VALUE_KEY_SEQ
];
if
(
!
jseq
.
isNull
())
if
(
!
jseq
.
isNull
())
seq
=
jseq
.
asInt
();
seq
=
jseq
.
asInt
();
const
auto
&
jowner
=
json
[
"owner"
];
const
auto
&
jowner
=
json
[
VALUE_KEY_OWNER
];
if
(
jowner
.
isString
())
{
if
(
jowner
.
isString
())
{
auto
ownerStr
=
jowner
.
asString
();
auto
ownerStr
=
jowner
.
asString
();
auto
ownerBlob
=
std
::
vector
<
unsigned
char
>
(
ownerStr
.
begin
(),
ownerStr
.
end
());
auto
ownerBlob
=
std
::
vector
<
unsigned
char
>
(
ownerStr
.
begin
(),
ownerStr
.
end
());
owner
=
std
::
make_shared
<
const
crypto
::
PublicKey
>
(
ownerBlob
);
owner
=
std
::
make_shared
<
const
crypto
::
PublicKey
>
(
ownerBlob
);
}
}
const
auto
&
jto
=
json
[
"to"
];
const
auto
&
jto
=
json
[
VALUE_KEY_TO
];
if
(
jto
.
isString
())
if
(
jto
.
isString
())
recipient
=
InfoHash
(
jto
.
asString
());
recipient
=
InfoHash
(
jto
.
asString
());
const
auto
&
jtype
=
json
[
"type"
];
const
auto
&
jtype
=
json
[
VALUE_KEY_TYPE
];
if
(
!
jtype
.
isNull
())
if
(
!
jtype
.
isNull
())
type
=
jtype
.
asInt
();
type
=
jtype
.
asInt
();
const
auto
&
jdata
=
json
[
"data"
];
const
auto
&
jdata
=
json
[
VALUE_KEY_DATA
];
if
(
jdata
.
isString
())
if
(
jdata
.
isString
())
data
=
base64_decode
(
jdata
.
asString
());
data
=
base64_decode
(
jdata
.
asString
());
const
auto
&
jutype
=
json
[
"utype"
];
const
auto
&
jutype
=
json
[
VALUE_KEY_USERTYPE
];
if
(
jutype
.
isString
())
if
(
jutype
.
isString
())
user_type
=
jutype
.
asString
();
user_type
=
jutype
.
asString
();
const
auto
&
jprio
=
json
[
"prio"
];
const
auto
&
jprio
=
json
[
"prio"
];
...
@@ -208,23 +208,23 @@ Json::Value
...
@@ -208,23 +208,23 @@ Json::Value
Value
::
toJson
()
const
Value
::
toJson
()
const
{
{
Json
::
Value
val
;
Json
::
Value
val
;
val
[
"id"
]
=
std
::
to_string
(
id
);
val
[
VALUE_KEY_ID
]
=
std
::
to_string
(
id
);
if
(
isEncrypted
())
{
if
(
isEncrypted
())
{
val
[
"cypher"
]
=
base64_encode
(
cypher
);
val
[
"cypher"
]
=
base64_encode
(
cypher
);
}
else
{
}
else
{
if
(
isSigned
())
if
(
isSigned
())
val
[
"sig"
]
=
base64_encode
(
signature
);
val
[
VALUE_KEY_SIGNATURE
]
=
base64_encode
(
signature
);
bool
has_owner
=
owner
&&
*
owner
;
bool
has_owner
=
owner
&&
*
owner
;
if
(
has_owner
)
{
// isSigned
if
(
has_owner
)
{
// isSigned
val
[
"seq"
]
=
seq
;
val
[
VALUE_KEY_SEQ
]
=
seq
;
val
[
"owner"
]
=
owner
->
toString
();
val
[
VALUE_KEY_OWNER
]
=
owner
->
toString
();
if
(
recipient
)
if
(
recipient
)
val
[
"to"
]
=
recipient
.
toString
();
val
[
VALUE_KEY_TO
]
=
recipient
.
toString
();
}
}
val
[
"type"
]
=
type
;
val
[
VALUE_KEY_TYPE
]
=
type
;
val
[
"data"
]
=
base64_encode
(
data
);
val
[
VALUE_KEY_DATA
]
=
base64_encode
(
data
);
if
(
not
user_type
.
empty
())
if
(
not
user_type
.
empty
())
val
[
"utype"
]
=
user_type
;
val
[
VALUE_KEY_USERTYPE
]
=
user_type
;
}
}
if
(
priority
)
if
(
priority
)
val
[
"prio"
]
=
priority
;
val
[
"prio"
]
=
priority
;
...
@@ -235,14 +235,12 @@ uint64_t
...
@@ -235,14 +235,12 @@ uint64_t
unpackId
(
const
Json
::
Value
&
json
,
const
std
::
string
&
key
)
{
unpackId
(
const
Json
::
Value
&
json
,
const
std
::
string
&
key
)
{
uint64_t
ret
=
0
;
uint64_t
ret
=
0
;
try
{
try
{
if
(
json
.
isMember
(
key
))
{
const
auto
&
t
=
json
[
key
];
const
auto
&
t
=
json
[
key
];
if
(
t
.
isString
())
{
if
(
t
.
isString
())
{
ret
=
std
::
stoull
(
t
.
asString
());
ret
=
std
::
stoull
(
t
.
asString
());
}
else
{
}
else
{
ret
=
t
.
asLargestUInt
();
ret
=
t
.
asLargestUInt
();
}
}
}
}
catch
(...)
{}
}
catch
(...)
{}
return
ret
;
return
ret
;
}
}
...
@@ -411,13 +409,13 @@ Select::Select(const std::string& q_str) {
...
@@ -411,13 +409,13 @@ Select::Select(const std::string& q_str) {
while
(
std
::
getline
(
fields
,
token
,
','
))
{
while
(
std
::
getline
(
fields
,
token
,
','
))
{
trim_str
(
token
);
trim_str
(
token
);
if
(
token
==
"id"
)
if
(
token
==
VALUE_KEY_ID
)
field
(
Value
::
Field
::
Id
);
field
(
Value
::
Field
::
Id
);
else
if
(
token
==
"value_type"
)
else
if
(
token
==
"value_type"
)
field
(
Value
::
Field
::
ValueType
);
field
(
Value
::
Field
::
ValueType
);
else
if
(
token
==
"owner_pk"
)
else
if
(
token
==
"owner_pk"
)
field
(
Value
::
Field
::
OwnerPk
);
field
(
Value
::
Field
::
OwnerPk
);
if
(
token
==
"seq"
)
if
(
token
==
VALUE_KEY_SEQ
)
field
(
Value
::
Field
::
SeqNum
);
field
(
Value
::
Field
::
SeqNum
);
else
if
(
token
==
"user_type"
)
else
if
(
token
==
"user_type"
)
field
(
Value
::
Field
::
UserType
);
field
(
Value
::
Field
::
UserType
);
...
@@ -453,13 +451,13 @@ Where::Where(const std::string& q_str) {
...
@@ -453,13 +451,13 @@ Where::Where(const std::string& q_str) {
s
=
value_str
.
substr
(
1
,
value_str
.
size
()
-
2
);
s
=
value_str
.
substr
(
1
,
value_str
.
size
()
-
2
);
else
else
s
=
value_str
;
s
=
value_str
;
if
(
field_str
==
"id"
)
if
(
field_str
==
VALUE_KEY_ID
)
id
(
v
);
id
(
v
);
else
if
(
field_str
==
"value_type"
)
else
if
(
field_str
==
"value_type"
)
valueType
(
v
);
valueType
(
v
);
else
if
(
field_str
==
"owner_pk"
)
else
if
(
field_str
==
"owner_pk"
)
owner
(
InfoHash
(
s
));
owner
(
InfoHash
(
s
));
else
if
(
field_str
==
"seq"
)
else
if
(
field_str
==
VALUE_KEY_SEQ
)
seq
(
v
);
seq
(
v
);
else
if
(
field_str
==
"user_type"
)
else
if
(
field_str
==
"user_type"
)
userType
(
s
);
userType
(
s
);
...
@@ -519,7 +517,7 @@ std::ostream& operator<<(std::ostream& s, const dht::Select& select) {
...
@@ -519,7 +517,7 @@ std::ostream& operator<<(std::ostream& s, const dht::Select& select) {
for
(
auto
fs
=
select
.
fieldSelection_
.
begin
()
;
fs
!=
select
.
fieldSelection_
.
end
()
;
++
fs
)
{
for
(
auto
fs
=
select
.
fieldSelection_
.
begin
()
;
fs
!=
select
.
fieldSelection_
.
end
()
;
++
fs
)
{
switch
(
*
fs
)
{
switch
(
*
fs
)
{
case
Value
::
Field
::
Id
:
case
Value
::
Field
::
Id
:
s
<<
"id"
;
s
<<
VALUE_KEY_ID
;
break
;
break
;
case
Value
::
Field
::
ValueType
:
case
Value
::
Field
::
ValueType
:
s
<<
"value_type"
;
s
<<
"value_type"
;
...
@@ -531,7 +529,7 @@ std::ostream& operator<<(std::ostream& s, const dht::Select& select) {
...
@@ -531,7 +529,7 @@ std::ostream& operator<<(std::ostream& s, const dht::Select& select) {
s
<<
"owner_public_key"
;
s
<<
"owner_public_key"
;
break
;
break
;
case
Value
::
Field
::
SeqNum
:
case
Value
::
Field
::
SeqNum
:
s
<<
"seq"
;
s
<<
VALUE_KEY_SEQ
;
break
;
break
;
default:
default:
break
;
break
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment