1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use serde_json::value::{to_raw_value, RawValue, Value};

use super::super::batch::{err_event_too_large, Batch, BatchSize, PushResult};

pub type BoxedRawValue = Box<RawValue>;

/// A `batch` implementation for storing an array of json
/// values.
///
/// Note: This has been deprecated, please do not use when creating new Sinks.
#[derive(Debug)]
pub struct JsonArrayBuffer {
    buffer: Vec<BoxedRawValue>,
    total_bytes: usize,
    settings: BatchSize<Self>,
}

impl JsonArrayBuffer {
    pub const fn new(settings: BatchSize<Self>) -> Self {
        Self {
            buffer: Vec::new(),
            total_bytes: 0,
            settings,
        }
    }
}

impl Batch for JsonArrayBuffer {
    type Input = Value;
    type Output = Vec<BoxedRawValue>;

    fn push(&mut self, item: Self::Input) -> PushResult<Self::Input> {
        let raw_item = to_raw_value(&item).expect("Value should be valid json");
        let new_len = self.total_bytes + raw_item.get().len() + 1;
        if self.is_empty() && new_len >= self.settings.bytes {
            err_event_too_large(raw_item.get().len(), self.settings.bytes)
        } else if self.buffer.len() >= self.settings.events || new_len > self.settings.bytes {
            PushResult::Overflow(item)
        } else {
            self.total_bytes = new_len;
            self.buffer.push(raw_item);
            PushResult::Ok(
                self.buffer.len() >= self.settings.events || new_len >= self.settings.bytes,
            )
        }
    }

    fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    fn fresh(&self) -> Self {
        Self::new(self.settings)
    }

    fn finish(self) -> Self::Output {
        self.buffer
    }

    fn num_items(&self) -> usize {
        self.buffer.len()
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{super::PushResult, *};
    use crate::sinks::util::BatchSettings;

    #[test]
    fn multi_object_array() {
        let mut batch_settings = BatchSettings::default();
        batch_settings.size.bytes = 9999;
        batch_settings.size.events = 2;

        let mut buffer = JsonArrayBuffer::new(batch_settings.size);

        assert_eq!(
            buffer.push(json!({
                "key1": "value1"
            })),
            PushResult::Ok(false)
        );

        assert_eq!(
            buffer.push(json!({
                "key2": "value2"
            })),
            PushResult::Ok(true)
        );

        assert!(matches!(buffer.push(json!({})), PushResult::Overflow(_)));

        assert_eq!(buffer.num_items(), 2);
        assert_eq!(buffer.total_bytes, 36);

        let json = buffer.finish();

        let wrapped = serde_json::to_string(&json!({
            "arr": json,
        }))
        .unwrap();

        let expected = serde_json::to_string(&json!({
            "arr": [
                {
                    "key1": "value1"
                },
                {
                    "key2": "value2"
                },
            ]
        }))
        .unwrap();

        assert_eq!(wrapped, expected);
    }
}