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.data.signals;
18 
19 import android.adservices.common.AdTechIdentifier;
20 
21 import androidx.annotation.NonNull;
22 import androidx.room.Dao;
23 import androidx.room.Insert;
24 import androidx.room.OnConflictStrategy;
25 import androidx.room.Query;
26 
27 import java.time.Instant;
28 import java.util.List;
29 
30 /** Dao to persist, access and delete encoded signals payload for buyers */
31 @Dao
32 public interface EncodedPayloadDao {
33 
34     /**
35      * @param logic an entry for encoded payload
36      * @return the rowId of the entry persisted
37      */
38     @Insert(onConflict = OnConflictStrategy.REPLACE)
persistEncodedPayload(DBEncodedPayload logic)39     long persistEncodedPayload(DBEncodedPayload logic);
40 
41     /**
42      * @param buyer Ad-tech owner for the encoded payload
43      * @return an instance of {@link DBEncodedPayload} if present
44      */
45     @Query("SELECT * FROM encoded_payload WHERE buyer = :buyer")
getEncodedPayload(AdTechIdentifier buyer)46     DBEncodedPayload getEncodedPayload(AdTechIdentifier buyer);
47 
48     /**
49      * @return an list of all {@link DBEncodedPayload} stored
50      */
51     @Query("SELECT * FROM encoded_payload")
getAllEncodedPayloads()52     List<DBEncodedPayload> getAllEncodedPayloads();
53 
54     /**
55      * @param buyer Ad-tech owner for the encoded payload
56      * @return true if the encoded payload for the buyer exists
57      */
58     @Query("SELECT EXISTS(SELECT 1 FROM encoded_payload WHERE buyer = :buyer)")
doesEncodedPayloadExist(AdTechIdentifier buyer)59     boolean doesEncodedPayloadExist(AdTechIdentifier buyer);
60 
61     /**
62      * @param buyer Ad-tech identifier whose encoded payload we want to delete
63      */
64     @Query("DELETE FROM encoded_payload WHERE buyer = :buyer")
deleteEncodedPayload(AdTechIdentifier buyer)65     void deleteEncodedPayload(AdTechIdentifier buyer);
66 
67     /** Deletes all persisted encoded payloads */
68     @Query("DELETE FROM encoded_payload")
deleteAllEncodedPayloads()69     void deleteAllEncodedPayloads();
70 
71     /**
72      * @return list of all the buyers that have encoded payloads
73      */
74     @Query("SELECT DISTINCT buyer FROM encoded_payload")
getAllBuyersWithEncodedPayloads()75     List<AdTechIdentifier> getAllBuyersWithEncodedPayloads();
76 
77     /**
78      * Deletes encoded payload before the {@code expiryTime}.
79      *
80      * @return the number of deleted payloads
81      */
82     @Query("DELETE FROM encoded_payload WHERE creation_time < :expiryTime")
deleteEncodedPayloadsBeforeTime(@onNull Instant expiryTime)83     int deleteEncodedPayloadsBeforeTime(@NonNull Instant expiryTime);
84 }
85