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.kanon;
18 
19 import androidx.room.Dao;
20 import androidx.room.Insert;
21 import androidx.room.OnConflictStrategy;
22 import androidx.room.Query;
23 
24 import java.time.Instant;
25 import java.util.List;
26 
27 /** Dao to manage access to entities in Client parameters table. */
28 @Dao
29 public abstract class KAnonMessageDao {
30 
31     /** Inserts the given KAnonMessage in table. */
32     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertKAnonMessage(DBKAnonMessage kAnonMessage)33     public abstract void insertKAnonMessage(DBKAnonMessage kAnonMessage);
34 
35     /** Inserts the given KAnonMessages in the table. */
36     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAllKAnonMessages(List<DBKAnonMessage> messages)37     public abstract long[] insertAllKAnonMessages(List<DBKAnonMessage> messages);
38 
39     /** Returns latest N messages with given status. */
40     @Query("SELECT * FROM kanon_messages WHERE status = :status ORDER BY created_at LIMIT :count")
getNLatestKAnonMessagesWithStatus( int count, @KAnonMessageConstants.MessageStatus int status)41     public abstract List<DBKAnonMessage> getNLatestKAnonMessagesWithStatus(
42             int count, @KAnonMessageConstants.MessageStatus int status);
43 
44     /** Return the kanon message with the given kanon hash set. */
45     @Query("SELECT * FROM kanon_messages WHERE kanon_hash_set = :hashSetToSearch")
getKAnonMessagesWithMessage(String hashSetToSearch)46     public abstract List<DBKAnonMessage> getKAnonMessagesWithMessage(String hashSetToSearch);
47 
48     /** Deletes all the knaon messages with the ids in the given list of ids. */
49     @Query("DELETE FROM kanon_messages where message_id IN (:idsToDelete)")
deleteKAnonMessagesWithIds(List<Long> idsToDelete)50     public abstract void deleteKAnonMessagesWithIds(List<Long> idsToDelete);
51 
52     /** Deletes all the kanon messages. */
53     @Query("DELETE FROM kanon_messages")
deleteAllKAnonMessages()54     public abstract void deleteAllKAnonMessages();
55 
56     /** Updates the status for the messages with the given messages ids. */
57     @Query("UPDATE kanon_messages SET status = :status WHERE message_id IN (:idsToUpdate)")
updateMessagesStatus( List<Long> idsToUpdate, @KAnonMessageConstants.MessageStatus int status)58     public abstract void updateMessagesStatus(
59             List<Long> idsToUpdate, @KAnonMessageConstants.MessageStatus int status);
60 
61     @Query(
62             "DELETE from kanon_messages WHERE expiry_instant < :currentTime OR"
63                     + " (corresponding_client_parameters_expiry_instant is NOT NULL AND"
64                     + " corresponding_client_parameters_expiry_instant < :currentTime)")
removeExpiredEntities(Instant currentTime)65     public abstract void removeExpiredEntities(Instant currentTime);
66 
67     /** Returns the count of entries with the given status */
68     @Query("SELECT COUNT(*) FROM kanon_messages WHERE status = :status")
getNumberOfMessagesWithStatus( @AnonMessageConstants.MessageStatus int status)69     public abstract int getNumberOfMessagesWithStatus(
70             @KAnonMessageConstants.MessageStatus int status);
71 }
72