summaryrefslogtreecommitdiff
path: root/adservices/service-core/java/com/android/adservices/data/topics/TopicsTables.java
blob: abbf6259e8791affa5d6a937797c9a4c97802496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices.data.topics;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/** Container class for Topics API table definitions and constants. */
public final class TopicsTables {

    static final String TOPICS_TABLE_PREFIX = "topics_";

    /** Topics Taxonomy Table. */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface TaxonomyContract {
        String TABLE = TOPICS_TABLE_PREFIX + "taxonomy";
        String ID = "_id";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
        String TOPIC = "topic";
    }

    /** Table Create Statement for the Topics Epoch table */
    @VisibleForTesting
    public static final String CREATE_TABLE_TOPICS_TAXONOMY =
            "CREATE TABLE "
                    + TaxonomyContract.TABLE
                    + "("
                    + TaxonomyContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + TaxonomyContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + TaxonomyContract.MODEL_VERSION
                    + " INTEGER NOT NULL, "
                    + TaxonomyContract.TOPIC
                    + " INTEGER NOT NULL"
                    + ")";

    /**
     * This table has apps' classification Topics generated by the ML Classifier. In each epoch
     * computation, the ML Classifier will generate topics for each app that uses the Topics API in
     * the epoch.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface AppClassificationTopicsContract {
        String TABLE = TOPICS_TABLE_PREFIX + "app_classification_topics";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String APP = "app";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
        String TOPIC = "topic";
    }

    /** Create Statement for the returned Topics table */
    @VisibleForTesting
    public static final String CREATE_TABLE_APP_CLASSIFICATION_TOPICS =
            "CREATE TABLE "
                    + AppClassificationTopicsContract.TABLE
                    + "("
                    + AppClassificationTopicsContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + AppClassificationTopicsContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + AppClassificationTopicsContract.APP
                    + " TEXT NOT NULL, "
                    + AppClassificationTopicsContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + AppClassificationTopicsContract.MODEL_VERSION
                    + " INTEGER NOT NULL, "
                    + AppClassificationTopicsContract.TOPIC
                    + " INTEGER NOT NULL"
                    + ")";

    /**
     * This table has callers and which topics they can learn. Caller can be either (1) app in case
     * the app called the Topics API directly. (2) sdk in case the sdk called the Topics API.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface CallerCanLearnTopicsContract {
        String TABLE = TOPICS_TABLE_PREFIX + "caller_can_learn_topic";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String CALLER = "caller";
        String TOPIC = "topic";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
    }

    /** Create Statement for the Caller Learned Topic table. */
    @VisibleForTesting
    public static final String CREATE_TABLE_CALLER_CAN_LEARN_TOPICS =
            "CREATE TABLE "
                    + CallerCanLearnTopicsContract.TABLE
                    + "("
                    + CallerCanLearnTopicsContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + CallerCanLearnTopicsContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + CallerCanLearnTopicsContract.CALLER
                    + " TEXT NOT NULL, "
                    + CallerCanLearnTopicsContract.TOPIC
                    + " INTEGER NOT NULL, "
                    + CallerCanLearnTopicsContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + CallerCanLearnTopicsContract.MODEL_VERSION
                    + " INTEGER NOT NULL"
                    + ")";

    // TODO(b/223446202): Make this table to configurable numbers of top topics.

    /**
     * Top Topics Table. There are top 5 topics and 1 random topic. In case there is not enough
     * usage to generate top 5 topics, random ones will be generated.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface TopTopicsContract {
        String TABLE = TOPICS_TABLE_PREFIX + "top_topics";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String TOPIC1 = "topic1";
        String TOPIC2 = "topic2";
        String TOPIC3 = "topic3";
        String TOPIC4 = "topic4";
        String TOPIC5 = "topic5";
        String RANDOM_TOPIC = "random_topic";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
    }

    /** Table Create Statement for the Top Topics table */
    @VisibleForTesting
    public static final String CREATE_TABLE_TOP_TOPICS =
            "CREATE TABLE "
                    + TopTopicsContract.TABLE
                    + "("
                    + TopTopicsContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + TopTopicsContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TOPIC1
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TOPIC2
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TOPIC3
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TOPIC4
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TOPIC5
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.RANDOM_TOPIC
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + TopTopicsContract.MODEL_VERSION
                    + " INTEGER NOT NULL"
                    + ")";

    /**
     * The returned topic for the app or for the sdk. Note: for App usages directly without any SDK,
     * the SDK Name is set to empty string.
     */
    public interface ReturnedTopicContract {
        String TABLE = TOPICS_TABLE_PREFIX + "returned_topics";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String APP = "app";
        String SDK = "sdk";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
        String TOPIC = "topic";
    }

    /** Create Statement for the returned Topics table */
    @VisibleForTesting
    public static final String CREATE_TABLE_RETURNED_TOPIC =
            "CREATE TABLE "
                    + ReturnedTopicContract.TABLE
                    + "("
                    + ReturnedTopicContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + ReturnedTopicContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + ReturnedTopicContract.APP
                    + " TEXT NOT NULL, "
                    + ReturnedTopicContract.SDK
                    + " TEXT NOT NULL, "
                    + ReturnedTopicContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + ReturnedTopicContract.MODEL_VERSION
                    + " INTEGER NOT NULL, "
                    + ReturnedTopicContract.TOPIC
                    + " INTEGER NOT NULL"
                    + ")";

    /**
     * Table to store the app/sdk usage history. Whenever an app or sdk calls the Topics API, one
     * entry will be generated with the timestamp.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface UsageHistoryContract {
        String TABLE = TOPICS_TABLE_PREFIX + "usage_history";
        String EPOCH_ID = "epoch_id";
        String APP = "app";
        String SDK = "sdk";
    }

    /** Create Statement for the Usage History table */
    @VisibleForTesting
    public static final String CREATE_TABLE_USAGE_HISTORY =
            "CREATE TABLE "
                    + UsageHistoryContract.TABLE
                    + "("
                    + UsageHistoryContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + UsageHistoryContract.APP
                    + " TEXT NOT NULL, "
                    + UsageHistoryContract.SDK
                    + " TEXT"
                    + ")";

    /**
     * Table to store history for app only Whenever an app calls the Topics API, one entry will be
     * generated.
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    public interface AppUsageHistoryContract {
        String TABLE = TOPICS_TABLE_PREFIX + "app_usage_history";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String APP = "app";
    }

    /** Create Statement for the Usage History App Only table */
    @VisibleForTesting
    public static final String CREATE_TABLE_APP_USAGE_HISTORY =
            "CREATE TABLE "
                    + AppUsageHistoryContract.TABLE
                    + "("
                    + AppUsageHistoryContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + AppUsageHistoryContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + AppUsageHistoryContract.APP
                    + " TEXT NOT NULL"
                    + ")";

    /** Table to store all blocked {@link Topic}s. Blocked topics are controlled by user. */
    public interface BlockedTopicsContract {
        String TABLE = TOPICS_TABLE_PREFIX + "blocked";
        String ID = "_id";
        String TAXONOMY_VERSION = "taxonomy_version";
        String MODEL_VERSION = "model_version";
        String TOPIC = "topic";
    }

    /** Create Statement for the blocked topics table. */
    @VisibleForTesting
    public static final String CREATE_TABLE_BLOCKED_TOPICS =
            "CREATE TABLE "
                    + BlockedTopicsContract.TABLE
                    + "("
                    + BlockedTopicsContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + BlockedTopicsContract.TAXONOMY_VERSION
                    + " INTEGER NOT NULL, "
                    + BlockedTopicsContract.MODEL_VERSION
                    + " INTEGER NOT NULL, "
                    + BlockedTopicsContract.TOPIC
                    + " INTEGER NOT NULL"
                    + ")";

    /**
     * Table to store the original timestamp when the user calls Topics API. This table should have
     * only 1 row that stores the origin.
     */
    public interface EpochOriginContract {
        String TABLE = TOPICS_TABLE_PREFIX + "epoch_origin";
        String ONE_ROW_CHECK = "one_row_check"; // to constrain 1 origin
        String ORIGIN = "origin";
    }

    /**
     * At the first time inserting a record, it won't persist one_row_check field so that this first
     * entry will have one_row_check = 1. Therefore, further persisting is not allowed as primary
     * key cannot be duplicated value and one_row_check is constrained to only equal to 1 to forbid
     * any increment.
     */
    @VisibleForTesting
    public static final String CREATE_TABLE_EPOCH_ORIGIN =
            "CREATE TABLE "
                    + EpochOriginContract.TABLE
                    + "("
                    + EpochOriginContract.ONE_ROW_CHECK
                    + " INTEGER PRIMARY KEY DEFAULT 1, "
                    + EpochOriginContract.ORIGIN
                    + " INTEGER NOT NULL, "
                    + "CONSTRAINT one_row_constraint CHECK ("
                    + EpochOriginContract.ONE_ROW_CHECK
                    + " = 1) "
                    + ")";

    /**
     * Table to store classified topic to apps mapping. In an epoch, an app is a contributor to a
     * topic if the app has called Topics API in this epoch and is classified to the topic.
     */
    public interface TopicContributorsContract {
        String TABLE = TOPICS_TABLE_PREFIX + "topic_contributors";
        String ID = "_id";
        String EPOCH_ID = "epoch_id";
        String TOPIC = "topic";
        String APP = "app";
    }

    /** The SQLite query to create topic_contributors table if it doesn't exist */
    public static final String CREATE_TABLE_TOPIC_CONTRIBUTORS =
            "CREATE TABLE IF NOT EXISTS "
                    + TopicContributorsContract.TABLE
                    + "("
                    + TopicContributorsContract.ID
                    + " INTEGER PRIMARY KEY, "
                    + TopicContributorsContract.EPOCH_ID
                    + " INTEGER NOT NULL, "
                    + TopicContributorsContract.TOPIC
                    + " INTEGER NOT NULL, "
                    + AppUsageHistoryContract.APP
                    + " TEXT NOT NULL"
                    + ")";

    /** Consolidated list of create statements for all tables. */
    public static final List<String> CREATE_STATEMENTS =
            Collections.unmodifiableList(
                    Arrays.asList(
                            CREATE_TABLE_TOPICS_TAXONOMY,
                            CREATE_TABLE_APP_CLASSIFICATION_TOPICS,
                            CREATE_TABLE_TOP_TOPICS,
                            CREATE_TABLE_RETURNED_TOPIC,
                            CREATE_TABLE_USAGE_HISTORY,
                            CREATE_TABLE_APP_USAGE_HISTORY,
                            CREATE_TABLE_CALLER_CAN_LEARN_TOPICS,
                            CREATE_TABLE_BLOCKED_TOPICS,
                            CREATE_TABLE_EPOCH_ORIGIN,
                            CREATE_TABLE_TOPIC_CONTRIBUTORS));
    // TODO(b/227393493): Should support a test if new table is added.
    // *******************************************************************************************
    // * NOTE: Please check below steps before adding a new table:
    // * 1) TopicsDao -> ALL_TOPICS_TABLES: User Consent to clear all tables
    // * 2) EpochManager -> TABLE_INFO_FOR_EPOCH_GARBAGE_COLLECTION: GC for dat of old epochs.
    // * 3) AppUpdateManager -> TABLE_INFO_TO_ERASE_APP_DATA: clear app data for app uninstallation
    // * 4) DbHelper -> onUpgrade: Handle any new schema change
    // *******************************************************************************************

    // Private constructor to prevent instantiation.
    private TopicsTables() {}
}