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.cobalt.data;
18 
19 import androidx.annotation.NonNull;
20 import androidx.room.ColumnInfo;
21 import androidx.room.Entity;
22 import androidx.room.PrimaryKey;
23 
24 import com.google.auto.value.AutoValue;
25 import com.google.auto.value.AutoValue.CopyAnnotations;
26 import com.google.cobalt.SystemProfile;
27 import com.google.common.hash.Hashing;
28 
29 /**
30  * Stores the mapping between system profile hashes and the protocol buffers they were generated
31  * from.
32  */
33 @AutoValue
34 @CopyAnnotations
35 @Entity(tableName = "SystemProfiles")
36 abstract class SystemProfileEntity {
37     /** The system profile hash. */
38     @CopyAnnotations
39     @ColumnInfo(name = "system_profile_hash")
40     @PrimaryKey
41     @NonNull
systemProfileHash()42     abstract long systemProfileHash();
43 
44     /** The system profile value. */
45     @CopyAnnotations
46     @ColumnInfo(name = "system_profile")
47     @NonNull
systemProfile()48     abstract SystemProfile systemProfile();
49 
50     /**
51      * Creates a {@link SystemProfileEntity}.
52      *
53      * <p>Used by Room to instantiate objects.
54      */
55     @NonNull
create(long systemProfileHash, SystemProfile systemProfile)56     static SystemProfileEntity create(long systemProfileHash, SystemProfile systemProfile) {
57         return new AutoValue_SystemProfileEntity(systemProfileHash, systemProfile);
58     }
59 
getSystemProfileHash(SystemProfile systemProfile)60     static long getSystemProfileHash(SystemProfile systemProfile) {
61         return Hashing.farmHashFingerprint64().hashBytes(systemProfile.toByteArray()).asLong();
62     }
63 }
64