liquid_cache_storage/cache/
squeeze_policies.rs1use bytes::Bytes;
4
5use crate::cache::{
6 LiquidCompressorStates, cached_batch::CachedBatch, transcode_liquid_inner,
7 utils::arrow_to_bytes,
8};
9
10pub trait SqueezePolicy: std::fmt::Debug + Send + Sync {
12 fn squeeze(
15 &self,
16 entry: CachedBatch,
17 compressor: &LiquidCompressorStates,
18 ) -> (CachedBatch, Option<Bytes>);
19}
20
21#[derive(Debug, Default, Clone)]
23pub struct Evict;
24
25impl SqueezePolicy for Evict {
26 fn squeeze(
27 &self,
28 entry: CachedBatch,
29 _compressor: &LiquidCompressorStates,
30 ) -> (CachedBatch, Option<Bytes>) {
31 match entry {
32 CachedBatch::MemoryArrow(array) => {
33 let bytes = arrow_to_bytes(&array).expect("failed to convert arrow to bytes");
34 (
35 CachedBatch::DiskArrow(array.data_type().clone()),
36 Some(bytes),
37 )
38 }
39 CachedBatch::MemoryLiquid(liquid_array) => {
40 let (hybrid_array, bytes) = match liquid_array.squeeze() {
41 Some(result) => result,
42 None => {
43 let bytes = liquid_array.to_bytes();
45 let bytes = Bytes::from(bytes);
46 return (
47 CachedBatch::DiskLiquid(liquid_array.original_arrow_data_type()),
48 Some(bytes),
49 );
50 }
51 };
52 (CachedBatch::MemoryHybridLiquid(hybrid_array), Some(bytes))
53 }
54 CachedBatch::MemoryHybridLiquid(hybrid_array) => (
55 CachedBatch::DiskLiquid(hybrid_array.original_arrow_data_type()),
56 None,
57 ),
58 CachedBatch::DiskLiquid(_) | CachedBatch::DiskArrow(_) => (entry, None),
59 }
60 }
61}
62
63#[derive(Debug, Default, Clone)]
65pub struct TranscodeSqueezeEvict;
66
67impl SqueezePolicy for TranscodeSqueezeEvict {
68 fn squeeze(
69 &self,
70 entry: CachedBatch,
71 compressor: &LiquidCompressorStates,
72 ) -> (CachedBatch, Option<Bytes>) {
73 match entry {
74 CachedBatch::MemoryArrow(array) => {
75 let liquid_array = transcode_liquid_inner(&array, compressor).unwrap();
76 (CachedBatch::MemoryLiquid(liquid_array), None)
77 }
78 CachedBatch::MemoryLiquid(liquid_array) => {
79 let (hybrid_array, bytes) = match liquid_array.squeeze() {
80 Some(result) => result,
81 None => {
82 let bytes = liquid_array.to_bytes();
83 let bytes = Bytes::from(bytes);
84 return (
85 CachedBatch::DiskLiquid(liquid_array.original_arrow_data_type()),
86 Some(bytes),
87 );
88 }
89 };
90 (CachedBatch::MemoryHybridLiquid(hybrid_array), Some(bytes))
91 }
92 CachedBatch::MemoryHybridLiquid(hybrid_array) => {
93 (
95 CachedBatch::DiskLiquid(hybrid_array.original_arrow_data_type()),
96 None,
97 )
98 }
99 CachedBatch::DiskLiquid(_) | CachedBatch::DiskArrow(_) => (entry, None),
100 }
101 }
102}
103
104#[derive(Debug, Default, Clone)]
106pub struct TranscodeEvict;
107
108impl SqueezePolicy for TranscodeEvict {
109 fn squeeze(
110 &self,
111 entry: CachedBatch,
112 compressor: &LiquidCompressorStates,
113 ) -> (CachedBatch, Option<Bytes>) {
114 match entry {
115 CachedBatch::MemoryArrow(array) => {
116 let liquid_array = transcode_liquid_inner(&array, compressor).unwrap();
117 (CachedBatch::MemoryLiquid(liquid_array), None)
118 }
119 CachedBatch::MemoryLiquid(liquid_array) => {
120 let bytes = liquid_array.to_bytes();
121 let bytes = Bytes::from(bytes);
122 (
123 CachedBatch::DiskLiquid(liquid_array.original_arrow_data_type()),
124 Some(bytes),
125 )
126 }
127 CachedBatch::MemoryHybridLiquid(hybrid_array) => (
128 CachedBatch::DiskLiquid(hybrid_array.original_arrow_data_type()),
129 None,
130 ),
131 CachedBatch::DiskLiquid(_) | CachedBatch::DiskArrow(_) => (entry, None),
132 }
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139 use crate::cache::cached_batch::CachedBatch;
140 use arrow::array::{ArrayRef, Int32Array, StringArray};
141 use arrow_schema::DataType;
142 use std::sync::Arc;
143
144 fn int_array(n: i32) -> ArrayRef {
145 Arc::new(Int32Array::from_iter_values(0..n))
146 }
147
148 fn decode_arrow(bytes: &Bytes) -> ArrayRef {
149 let cursor = std::io::Cursor::new(bytes.to_vec());
150 let mut reader =
151 arrow::ipc::reader::StreamReader::try_new(cursor, None).expect("arrow stream");
152 let batch = reader
153 .next()
154 .expect("non-empty stream")
155 .expect("read stream");
156 batch.column(0).clone()
157 }
158
159 #[test]
160 fn test_squeeze_to_disk_policy() {
161 let disk = Evict;
162 let states = LiquidCompressorStates::new();
163
164 let arr = int_array(8);
166 let (new_batch, bytes) = disk.squeeze(CachedBatch::MemoryArrow(arr.clone()), &states);
167 match (new_batch, bytes) {
168 (CachedBatch::DiskArrow(DataType::Int32), Some(b)) => {
169 let decoded = decode_arrow(&b);
170 assert_eq!(decoded.as_ref(), arr.as_ref());
171 }
172 other => panic!("unexpected: {other:?}"),
173 }
174
175 let strings = Arc::new(StringArray::from(vec!["a", "b", "a"])) as ArrayRef;
177 let liquid = transcode_liquid_inner(&strings, &states).unwrap();
178 let (new_batch, bytes) = disk.squeeze(CachedBatch::MemoryLiquid(liquid.clone()), &states);
179 match (new_batch, bytes) {
180 (CachedBatch::MemoryHybridLiquid(_), Some(b)) => {
181 assert!(!b.is_empty());
182 }
183 other => panic!("unexpected: {other:?}"),
184 }
185
186 let hybrid = match liquid.squeeze() {
188 Some((h, _b)) => h,
189 None => panic!("squeeze should succeed for byte-view"),
190 };
191 let (new_batch, bytes) = disk.squeeze(CachedBatch::MemoryHybridLiquid(hybrid), &states);
192 match (new_batch, bytes) {
193 (CachedBatch::DiskLiquid(_data_type), None) => {}
194 other => panic!("unexpected: {other:?}"),
195 }
196
197 let (b1, w1) = disk.squeeze(CachedBatch::DiskArrow(DataType::Utf8), &states);
199 assert!(matches!(b1, CachedBatch::DiskArrow(DataType::Utf8)) && w1.is_none());
200 let (b2, w2) = disk.squeeze(CachedBatch::DiskLiquid(DataType::Utf8), &states);
201 assert!(matches!(b2, CachedBatch::DiskLiquid(DataType::Utf8)) && w2.is_none());
202 }
203
204 #[test]
205 fn test_squeeze_to_liquid_policy() {
206 let to_liquid = TranscodeSqueezeEvict;
207 let states = LiquidCompressorStates::new();
208
209 let arr = int_array(8);
211 let (new_batch, bytes) = to_liquid.squeeze(CachedBatch::MemoryArrow(arr.clone()), &states);
212 assert!(bytes.is_none());
213 match new_batch {
214 CachedBatch::MemoryLiquid(liq) => {
215 assert_eq!(liq.to_arrow_array().as_ref(), arr.as_ref());
216 }
217 other => panic!("unexpected: {other:?}"),
218 }
219
220 let strings = Arc::new(StringArray::from(vec!["x", "y", "x"])) as ArrayRef;
222 let liquid = transcode_liquid_inner(&strings, &states).unwrap();
223 let (new_batch, bytes) = to_liquid.squeeze(CachedBatch::MemoryLiquid(liquid), &states);
224 match (new_batch, bytes) {
225 (CachedBatch::MemoryHybridLiquid(_), Some(b)) => assert!(!b.is_empty()),
226 other => panic!("unexpected: {other:?}"),
227 }
228
229 let strings = Arc::new(StringArray::from(vec!["m", "n"])) as ArrayRef;
231 let liquid = transcode_liquid_inner(&strings, &states).unwrap();
232 let hybrid = liquid.squeeze().unwrap().0;
233 let (new_batch, bytes) =
234 to_liquid.squeeze(CachedBatch::MemoryHybridLiquid(hybrid), &states);
235 match (new_batch, bytes) {
236 (CachedBatch::DiskLiquid(DataType::Utf8), None) => {}
237 other => panic!("unexpected: {other:?}"),
238 }
239
240 let (b1, w1) = to_liquid.squeeze(CachedBatch::DiskArrow(DataType::Utf8), &states);
242 assert!(matches!(b1, CachedBatch::DiskArrow(DataType::Utf8)) && w1.is_none());
243 let (b2, w2) = to_liquid.squeeze(CachedBatch::DiskLiquid(DataType::Utf8), &states);
244 assert!(matches!(b2, CachedBatch::DiskLiquid(DataType::Utf8)) && w2.is_none());
245 }
246}