@@ -16,37 +16,40 @@ pub fn insert(conn: &postgres::Connection, node_name: &NodeName, logs: Vec<Struc
16
16
return Ok ( ( ) )
17
17
}
18
18
19
- let mut query = "" ;
20
- let mut parameters_positions: Vec < String > = Vec :: new ( ) ;
21
- let mut parameters: Vec < Box < ToSql > > = Vec :: new ( ) ;
22
- for ( row_index, log) in logs. into_iter ( ) . enumerate ( ) {
23
- let base_num = row_index * 6 ;
24
- parameters_positions. push ( format ! (
25
- "(${}, ${}, ${}, ${}, ${}, ${})" ,
26
- base_num + 1 ,
27
- base_num + 2 ,
28
- base_num + 3 ,
29
- base_num + 4 ,
30
- base_num + 5 ,
31
- base_num + 6
32
- ) ) ;
33
- let rfc3339with_nano_second = "%Y-%m-%dT%H:%M:%S.%f%z" ;
34
- let datetime = chrono:: DateTime :: parse_from_str ( & log. timestamp , rfc3339with_nano_second) . unwrap ( ) ;
35
- parameters. push ( Box :: new ( node_name) ) ;
36
- parameters. push ( Box :: new ( log. level ) ) ;
37
- parameters. push ( Box :: new ( log. target ) ) ;
38
- parameters. push ( Box :: new ( log. message ) ) ;
39
- parameters. push ( Box :: new ( datetime) ) ;
40
- parameters. push ( Box :: new ( log. thread_name ) ) ;
41
- }
19
+ for log_chunk in logs. chunks ( 1000 ) {
20
+ let mut query = "" ;
21
+ let mut parameters_positions: Vec < String > = Vec :: new ( ) ;
22
+ let mut parameters: Vec < Box < ToSql > > = Vec :: new ( ) ;
23
+
24
+ for ( row_index, log) in log_chunk. into_iter ( ) . enumerate ( ) {
25
+ let base_num = row_index * 6 ;
26
+ parameters_positions. push ( format ! (
27
+ "(${}, ${}, ${}, ${}, ${}, ${})" ,
28
+ base_num + 1 ,
29
+ base_num + 2 ,
30
+ base_num + 3 ,
31
+ base_num + 4 ,
32
+ base_num + 5 ,
33
+ base_num + 6
34
+ ) ) ;
35
+ let rfc3339with_nano_second = "%Y-%m-%dT%H:%M:%S.%f%z" ;
36
+ let datetime = chrono:: DateTime :: parse_from_str ( & log. timestamp , rfc3339with_nano_second) . unwrap ( ) ;
37
+ parameters. push ( Box :: new ( node_name) ) ;
38
+ parameters. push ( Box :: new ( log. level . clone ( ) ) ) ;
39
+ parameters. push ( Box :: new ( log. target . clone ( ) ) ) ;
40
+ parameters. push ( Box :: new ( log. message . clone ( ) ) ) ;
41
+ parameters. push ( Box :: new ( datetime) ) ;
42
+ parameters. push ( Box :: new ( log. thread_name . clone ( ) ) ) ;
43
+ }
42
44
43
- let full_sql = format ! (
44
- "INSERT INTO logs (name, level, target, message, timestamp, thread_name) VALUES {}" ,
45
- parameters_positions. join( ", " )
46
- ) ;
47
- let parameters_ref: Vec < & ToSql > = parameters. iter ( ) . map ( |param| param. as_ref ( ) ) . collect ( ) ;
48
- ctrace ! ( "Full query is {}" , full_sql) ;
49
- conn. execute ( & full_sql, & parameters_ref) ?;
45
+ let full_sql = format ! (
46
+ "INSERT INTO logs (name, level, target, message, timestamp, thread_name) VALUES {}" ,
47
+ parameters_positions. join( ", " )
48
+ ) ;
49
+ let parameters_ref: Vec < & ToSql > = parameters. iter ( ) . map ( |param| param. as_ref ( ) ) . collect ( ) ;
50
+ ctrace ! ( "Full query is {}" , full_sql) ;
51
+ conn. execute ( & full_sql, & parameters_ref) ?;
52
+ }
50
53
51
54
Ok ( ( ) )
52
55
}
0 commit comments