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.signals;
18 
19 import android.adservices.common.AdTechIdentifier;
20 
21 import androidx.annotation.NonNull;
22 import androidx.room.ColumnInfo;
23 import androidx.room.Entity;
24 import androidx.room.PrimaryKey;
25 
26 import com.google.auto.value.AutoValue;
27 
28 import java.time.Instant;
29 
30 /**
31  * Represents an entry for encoder logic metadata for a buyer, the actual logic will be persisted on
32  * the device as flat files. This DB entry is meant for record keeping of file-system storage.
33  */
34 @AutoValue
35 @AutoValue.CopyAnnotations
36 @Entity(tableName = DBEncoderLogicMetadata.TABLE_NAME, inheritSuperIndices = true)
37 public abstract class DBEncoderLogicMetadata {
38 
39     // Due to the table name already into the M train.
40     // Retain the table name as table delete is not permitted.
41     public static final String TABLE_NAME = "encoder_logics"; // NOTYPO
42 
43     /** The ad-tech buyer who owns the logic */
44     @AutoValue.CopyAnnotations
45     @ColumnInfo(name = "buyer")
46     @PrimaryKey
47     @NonNull
getBuyer()48     public abstract AdTechIdentifier getBuyer();
49 
50     /** The version provided by ad-tech when this encoding logic was downloaded */
51     @AutoValue.CopyAnnotations
52     @ColumnInfo(name = "version")
getVersion()53     public abstract int getVersion();
54 
55     /** The time at which this entry for encoding logic was persisted */
56     @AutoValue.CopyAnnotations
57     @ColumnInfo(name = "creation_time", index = true)
58     @NonNull
getCreationTime()59     public abstract Instant getCreationTime();
60 
61     /** The number of time the JS failed to execute in a row. */
62     @AutoValue.CopyAnnotations
63     @ColumnInfo(name = "failed_encoding_count", defaultValue = "0")
getFailedEncodingCount()64     public abstract int getFailedEncodingCount();
65 
66     /**
67      * @return an instance of {@link DBEncoderLogicMetadata}
68      */
create( @onNull AdTechIdentifier buyer, int version, @NonNull Instant creationTime, int failedEncodingCount)69     public static DBEncoderLogicMetadata create(
70             @NonNull AdTechIdentifier buyer,
71             int version,
72             @NonNull Instant creationTime,
73             int failedEncodingCount) {
74         return builder()
75                 .setBuyer(buyer)
76                 .setVersion(version)
77                 .setCreationTime(creationTime)
78                 .setFailedEncodingCount(failedEncodingCount)
79                 .build();
80     }
81 
82     /**
83      * @return a builder for creating a {@link DBEncoderLogicMetadata}
84      */
builder()85     public static DBEncoderLogicMetadata.Builder builder() {
86         return new AutoValue_DBEncoderLogicMetadata.Builder().setFailedEncodingCount(0);
87     }
88 
89     /** Provides a builder to create an instance of {@link DBEncoderLogicMetadata} */
90     @AutoValue.Builder
91     public abstract static class Builder {
92 
93         /** For more details see {@link #getBuyer()} */
setBuyer(@onNull AdTechIdentifier value)94         public abstract Builder setBuyer(@NonNull AdTechIdentifier value);
95 
96         /** For more details see {@link #getVersion()} */
setVersion(int value)97         public abstract Builder setVersion(int value);
98 
99         /** For more details see {@link #getCreationTime()} */
setCreationTime(@onNull Instant value)100         public abstract Builder setCreationTime(@NonNull Instant value);
101 
102         /** For more details see {@link #getFailedEncodingCount()} */
setFailedEncodingCount(int failedEncodingCount)103         public abstract Builder setFailedEncodingCount(int failedEncodingCount);
104 
105         /**
106          * @return an instance of {@link DBEncoderLogicMetadata}
107          */
build()108         public abstract DBEncoderLogicMetadata build();
109     }
110 }
111