1 /*
2  * Copyright (C) 2022 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 androidx.room.AutoMigration;
20 import androidx.room.Database;
21 import androidx.room.RoomDatabase;
22 import androidx.room.TypeConverters;
23 
24 import com.android.adservices.data.common.FledgeRoomConverters;
25 import com.android.adservices.service.common.compat.FileCompatUtils;
26 import com.android.adservices.shared.common.ApplicationContextSingleton;
27 
28 /** Room based database for protected signals. */
29 @Database(
30         entities = {
31             DBProtectedSignal.class,
32             DBEncoderEndpoint.class,
33             DBEncoderLogicMetadata.class,
34             DBEncodedPayload.class,
35             DBSignalsUpdateMetadata.class,
36         },
37         autoMigrations = {
38             @AutoMigration(from = 1, to = 2),
39             @AutoMigration(from = 2, to = 3),
40             @AutoMigration(from = 3, to = 4)
41         },
42         version = ProtectedSignalsDatabase.DATABASE_VERSION)
43 @TypeConverters({FledgeRoomConverters.class})
44 public abstract class ProtectedSignalsDatabase extends RoomDatabase {
45     private static final Object SINGLETON_LOCK = new Object();
46 
47     public static final int DATABASE_VERSION = 4;
48     public static final String DATABASE_NAME =
49             FileCompatUtils.getAdservicesFilename("protectedsignals.db");
50 
51     private static volatile ProtectedSignalsDatabase sSingleton;
52 
53     /** Returns an instance of the ProtectedSignalsDatabase given a context. */
getInstance()54     public static ProtectedSignalsDatabase getInstance() {
55         /* This initialization pattern tends to outperform more naive approaches since it
56          * does not attempt to grab the lock if the DB is already initialized.
57          * Ref: "Effective Java" 3rd edition by Joshua Bloch (page 334)
58          */
59         ProtectedSignalsDatabase singleReadResult = sSingleton;
60         if (singleReadResult != null) {
61             return singleReadResult;
62         }
63         synchronized (SINGLETON_LOCK) {
64             if (sSingleton == null) {
65                 sSingleton =
66                         FileCompatUtils.roomDatabaseBuilderHelper(
67                                         ApplicationContextSingleton.get(),
68                                         ProtectedSignalsDatabase.class,
69                                         DATABASE_NAME)
70                                 .fallbackToDestructiveMigration()
71                                 .build();
72             }
73             return sSingleton;
74         }
75     }
76 
77     /**
78      * Protected signals Dao.
79      *
80      * @return Dao to access protected signals storage.
81      */
protectedSignalsDao()82     public abstract ProtectedSignalsDao protectedSignalsDao();
83 
84     /**
85      * Encoder endpoints Dao
86      *
87      * @return Dao to access encoder end points
88      */
getEncoderEndpointsDao()89     public abstract EncoderEndpointsDao getEncoderEndpointsDao();
90 
91     /**
92      * Encoder Logics Dao
93      *
94      * @return Dao to access persisted encoder logic entries
95      */
getEncoderLogicMetadataDao()96     public abstract EncoderLogicMetadataDao getEncoderLogicMetadataDao();
97 
98     /**
99      * Encoded Payloads Dao
100      *
101      * @return Dao to access persisted encoded signals payloads
102      */
getEncodedPayloadDao()103     public abstract EncodedPayloadDao getEncodedPayloadDao();
104 }
105