Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Turris
ucollect
Commits
2f606830
Verified
Commit
2f606830
authored
Dec 18, 2016
by
Michal 'vorner' Vaner
Browse files
Experiment: Buffering to avoid too much locking
parent
0b65d490
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/master/amihacked/process/src/main.rs
View file @
2f606830
...
...
@@ -119,9 +119,38 @@ struct Record {
type
ResultSum
=
HashMap
<
String
,
HashMap
<
String
,
u32
>>
;
fn
json_output
(
sum
:
&
mut
ResultSum
,
last
:
&
mut
Option
<
IpAddr
>
)
{
struct
MultiBuf
{
buffer
:
Vec
<
String
>
}
impl
MultiBuf
{
fn
new
()
->
MultiBuf
{
MultiBuf
{
buffer
:
Vec
::
with_capacity
(
1024
)
}
}
fn
flush
(
&
mut
self
)
{
let
stdout
=
std
::
io
::
stdout
();
let
mut
lock
=
stdout
.lock
();
for
s
in
self
.buffer
.drain
(
0
..
)
{
writeln!
(
lock
,
"{}"
,
s
)
.unwrap
();
}
}
fn
write
(
&
mut
self
,
data
:
String
)
{
if
self
.buffer
.len
()
==
self
.buffer
.capacity
()
{
self
.flush
();
}
self
.buffer
.push
(
data
);
}
}
impl
Drop
for
MultiBuf
{
fn
drop
(
&
mut
self
)
{
self
.flush
();
}
}
fn
json_output
(
buf
:
&
mut
MultiBuf
,
sum
:
&
mut
ResultSum
,
last
:
&
mut
Option
<
IpAddr
>
)
{
if
let
Some
(
ip
)
=
*
last
{
println
!
(
"{} {}"
,
ip
,
serde_json
::
to_string
(
&
sum
)
.unwrap
());
buf
.write
(
format
!
(
"{} {}"
,
ip
,
serde_json
::
to_string
(
&
sum
)
.unwrap
())
)
;
*
sum
=
HashMap
::
new
();
}
}
...
...
@@ -138,6 +167,7 @@ fn aggregate(sort: &mut Child) {
let
mut
output
=
sort
.stdout
.as_mut
()
.unwrap
();
let
mut
reader
=
csv
::
Reader
::
from_reader
(
&
mut
output
)
.has_headers
(
false
);
let
mut
sum
:
ResultSum
=
HashMap
::
new
();
let
mut
buf
=
MultiBuf
::
new
();
for
row
in
reader
.decode
()
{
let
row
:
Record
=
row
.unwrap
();
let
ip
:
IpAddr
=
row
.ip
.parse
()
.unwrap
();
...
...
@@ -145,12 +175,12 @@ fn aggregate(sort: &mut Child) {
continue
;
}
if
Some
(
ip
)
!=
last
{
json_output
(
&
mut
sum
,
&
mut
last
);
json_output
(
&
mut
buf
,
&
mut
sum
,
&
mut
last
);
}
last
=
Some
(
ip
);
*
sum
.entry
(
row
.kind
)
.or_insert_with
(
HashMap
::
new
)
.entry
(
row
.date
)
.or_insert
(
0
)
+=
row
.cnt
;
}
json_output
(
&
mut
sum
,
&
mut
last
);
json_output
(
&
mut
buf
,
&
mut
sum
,
&
mut
last
);
}
fn
jsonize
(
pool
:
&
scoped_pool
::
Pool
,
prefixes
:
&
HashSet
<
String
>
)
{
...
...
Write
Preview
Supports
Markdown
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