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 encoding logic for buyers */
31 @Dao
32 public interface EncoderLogicMetadataDao {
33 
34     /**
35      * @param logic an entry for encoding logic metadata
36      * @return the rowId of the entry persisted
37      */
38     @Insert(onConflict = OnConflictStrategy.REPLACE)
persistEncoderLogicMetadata(DBEncoderLogicMetadata logic)39     long persistEncoderLogicMetadata(DBEncoderLogicMetadata logic);
40 
41     /**
42      * @param buyer Ad-tech owner for the encoding logic
43      * @param failedCount times of continues failure of the encoding logic
44      */
45     // common_typos_disable
46     @Query("UPDATE encoder_logics SET failed_encoding_count = :failedCount WHERE buyer = :buyer")
47     // common_typos_enable
updateEncoderFailedCount(AdTechIdentifier buyer, int failedCount)48     int updateEncoderFailedCount(AdTechIdentifier buyer, int failedCount);
49 
50     /**
51      * @param buyer Ad-tech owner for the encoding logic
52      * @return an instance of {@link DBEncoderLogicMetadata} if present
53      */
54     @Query("SELECT * FROM encoder_logics WHERE buyer = :buyer") // NOTYPO
getMetadata(AdTechIdentifier buyer)55     DBEncoderLogicMetadata getMetadata(AdTechIdentifier buyer);
56 
57     /**
58      * @param buyer Ad-tech owner for the encoding logic
59      * @return true if the encoder for the buyer exists
60      */
61     @Query("SELECT EXISTS(SELECT 1 FROM encoder_logics WHERE buyer = :buyer)") // NOTYPO
doesEncoderExist(AdTechIdentifier buyer)62     boolean doesEncoderExist(AdTechIdentifier buyer);
63 
64     /**
65      * @return list of all registered encoder logic
66      */
67     @Query("SELECT * FROM encoder_logics") // NOTYPO
getAllRegisteredEncoders()68     List<DBEncoderLogicMetadata> getAllRegisteredEncoders();
69 
70     /**
71      * @return list of all the buyers which have their encoder logic registered
72      */
73     @Query("SELECT DISTINCT buyer FROM encoder_logics") // NOTYPO
getAllBuyersWithRegisteredEncoders()74     List<AdTechIdentifier> getAllBuyersWithRegisteredEncoders();
75 
76     /**
77      * @return list of buyers which registered encoders before the time instant
78      */
79     @Query("SELECT DISTINCT buyer FROM encoder_logics WHERE creation_time < :time") // NOTYPO
getBuyersWithEncodersBeforeTime(@onNull Instant time)80     List<AdTechIdentifier> getBuyersWithEncodersBeforeTime(@NonNull Instant time);
81 
82     /**
83      * @param buyer Ad-tech identifier whose encoding logic we delete
84      */
85     @Query("DELETE FROM encoder_logics WHERE buyer = :buyer") // NOTYPO
deleteEncoder(AdTechIdentifier buyer)86     void deleteEncoder(AdTechIdentifier buyer);
87 
88     /** Deletes all persisted encoding logic */
89     @Query("DELETE FROM encoder_logics") // NOTYPO
deleteAllEncoders()90     void deleteAllEncoders();
91 }
92