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 @AutoMigration(from = 4, to = 5) 42 }, 43 version = ProtectedSignalsDatabase.DATABASE_VERSION) 44 @TypeConverters({FledgeRoomConverters.class}) 45 public abstract class ProtectedSignalsDatabase extends RoomDatabase { 46 private static final Object SINGLETON_LOCK = new Object(); 47 48 public static final int DATABASE_VERSION = 5; 49 public static final String DATABASE_NAME = 50 FileCompatUtils.getAdservicesFilename("protectedsignals.db"); 51 52 private static volatile ProtectedSignalsDatabase sSingleton; 53 54 /** Returns an instance of the ProtectedSignalsDatabase given a context. */ getInstance()55 public static ProtectedSignalsDatabase getInstance() { 56 /* This initialization pattern tends to outperform more naive approaches since it 57 * does not attempt to grab the lock if the DB is already initialized. 58 * Ref: "Effective Java" 3rd edition by Joshua Bloch (page 334) 59 */ 60 ProtectedSignalsDatabase singleReadResult = sSingleton; 61 if (singleReadResult != null) { 62 return singleReadResult; 63 } 64 synchronized (SINGLETON_LOCK) { 65 if (sSingleton == null) { 66 sSingleton = 67 FileCompatUtils.roomDatabaseBuilderHelper( 68 ApplicationContextSingleton.get(), 69 ProtectedSignalsDatabase.class, 70 DATABASE_NAME) 71 .fallbackToDestructiveMigration() 72 .build(); 73 } 74 return sSingleton; 75 } 76 } 77 78 /** 79 * Protected signals Dao. 80 * 81 * @return Dao to access protected signals storage. 82 */ protectedSignalsDao()83 public abstract ProtectedSignalsDao protectedSignalsDao(); 84 85 /** 86 * Encoder endpoints Dao 87 * 88 * @return Dao to access encoder end points 89 */ getEncoderEndpointsDao()90 public abstract EncoderEndpointsDao getEncoderEndpointsDao(); 91 92 /** 93 * Encoder Logics Dao 94 * 95 * @return Dao to access persisted encoder logic entries 96 */ getEncoderLogicMetadataDao()97 public abstract EncoderLogicMetadataDao getEncoderLogicMetadataDao(); 98 99 /** 100 * Encoded Payloads Dao 101 * 102 * @return Dao to access persisted encoded signals payloads 103 */ getEncodedPayloadDao()104 public abstract EncodedPayloadDao getEncodedPayloadDao(); 105 } 106