1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.service.adselection;
18 
19 import java.util.Arrays;
20 
21 /** Data compressor interface */
22 public interface AuctionServerDataCompressor {
23     /**
24      * Buffer size controls the size of the batches we read from the input stream. If the batches
25      * are too big then will consume more memory. If the batches are two small then will consume
26      * more cycles. This number (1KB) is a commonly used sweet-spot. Can be changed when further
27      * data is available.
28      */
29     int BUFFER_SIZE = 1024;
30 
31     /** Compresses buyer input map collected from device */
compress(UncompressedData data)32     CompressedData compress(UncompressedData data);
33 
34     /** Decompresses given input */
decompress(CompressedData compressedData)35     UncompressedData decompress(CompressedData compressedData);
36 
37     /**
38      * Represents formatted data, input to {@link AuctionServerDataCompressor#compress} and output
39      * from {@link AuctionServerDataCompressor#decompress}
40      */
41     class UncompressedData {
42         private final byte[] mData;
43 
UncompressedData(byte[] data)44         private UncompressedData(byte[] data) {
45             this.mData = data;
46         }
47 
48         /**
49          * @return data
50          */
getData()51         public byte[] getData() {
52             return Arrays.copyOf(mData, mData.length);
53         }
54 
55         /** Creates {@link UncompressedData} */
create(byte[] data)56         public static UncompressedData create(byte[] data) {
57             return new UncompressedData(Arrays.copyOf(data, data.length));
58         }
59     }
60 
61     /**
62      * Represents formatted data, input to {@link AuctionServerDataCompressor#decompress} and output
63      * from {@link AuctionServerDataCompressor#compress}
64      */
65     class CompressedData {
66         private final byte[] mData;
67 
CompressedData(byte[] data)68         private CompressedData(byte[] data) {
69             this.mData = data;
70         }
71 
72         /**
73          * @return data
74          */
getData()75         public byte[] getData() {
76             return Arrays.copyOf(mData, mData.length);
77         }
78 
79         /** Creates {@link CompressedData} */
create(byte[] data)80         public static CompressedData create(byte[] data) {
81             return new CompressedData(Arrays.copyOf(data, data.length));
82         }
83     }
84 }
85