• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adselection;
18 
19 import android.content.Context;
20 
21 import androidx.annotation.NonNull;
22 import androidx.room.AutoMigration;
23 import androidx.room.Database;
24 import androidx.room.Room;
25 import androidx.room.RoomDatabase;
26 import androidx.room.TypeConverters;
27 
28 import com.android.adservices.data.common.FledgeRoomConverters;
29 
30 import java.util.Objects;
31 
32 /** Room based database for Ad Selection. */
33 @Database(
34         entities = {
35             DBAdSelection.class,
36             DBBuyerDecisionLogic.class,
37             DBAdSelectionOverride.class,
38             DBAdSelectionFromOutcomesOverride.class,
39             DBRegisteredAdInteraction.class,
40             DBBuyerDecisionOverride.class
41         },
42         version = AdSelectionDatabase.DATABASE_VERSION,
43         autoMigrations = {
44             @AutoMigration(from = 1, to = 2),
45             @AutoMigration(from = 2, to = 3),
46             @AutoMigration(from = 3, to = 4),
47             @AutoMigration(from = 4, to = 5)
48         })
49 @TypeConverters({FledgeRoomConverters.class})
50 public abstract class AdSelectionDatabase extends RoomDatabase {
51     private static final Object SINGLETON_LOCK = new Object();
52 
53     public static final int DATABASE_VERSION = 5;
54     // TODO(b/230653780): Should we separate the DB.
55     public static final String DATABASE_NAME = "adselection.db";
56 
57     private static volatile AdSelectionDatabase sSingleton = null;
58 
59     /** Returns an instance of the AdSelectionDatabase given a context. */
getInstance(@onNull Context context)60     public static AdSelectionDatabase getInstance(@NonNull Context context) {
61         Objects.requireNonNull(context, "Context must be provided.");
62         // Initialization pattern recommended on page 334 of "Effective Java" 3rd edition
63         AdSelectionDatabase singleReadResult = sSingleton;
64         if (singleReadResult != null) {
65             return singleReadResult;
66         }
67         synchronized (SINGLETON_LOCK) {
68             if (sSingleton == null) {
69                 sSingleton =
70                         Room.databaseBuilder(context, AdSelectionDatabase.class, DATABASE_NAME)
71                                 .fallbackToDestructiveMigration()
72                                 .build();
73             }
74             return sSingleton;
75         }
76     }
77 
78     /**
79      * @return a Dao to access entities in AdSelection database.
80      */
adSelectionEntryDao()81     public abstract AdSelectionEntryDao adSelectionEntryDao();
82 }
83