Skip to content

Adding an "extras" column to transport databases #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

package com.google.android.datatransport.runtime.scheduling.persistence;

import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
import static org.mockito.Mockito.spy;

import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
Expand All @@ -45,32 +40,8 @@ static EventStore eventStore(SQLiteEventStore store) {
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);

@Provides
@Named("CREATE_EVENTS_SQL")
static String createEventsSql() {
return CREATE_EVENTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_METADATA_SQL")
static String createEventMetadataSql() {
return CREATE_EVENT_METADATA_SQL_V1;
}

@Provides
@Named("CREATE_CONTEXTS_SQL")
static String createContextsSql() {
return CREATE_CONTEXTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_BACKEND_INDEX")
static String getCreateEventBackendIndex() {
return CREATE_EVENT_BACKEND_INDEX_V1;
}

@Provides
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
static String createEventBackendPriorityIndex() {
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
@Named("SCHEMA_VERSION")
static int schemaVersion() {
return SchemaManager.SCHEMA_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

package com.google.android.datatransport.runtime.scheduling.persistence;

import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.SchemaManager.SCHEMA_VERSION;

import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
import dagger.Binds;
Expand Down Expand Up @@ -49,32 +45,8 @@ static EventStoreConfig storeConfig() {
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);

@Provides
@Named("CREATE_EVENTS_SQL")
static String createEventsSql() {
return CREATE_EVENTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_METADATA_SQL")
static String createEventMetadataSql() {
return CREATE_EVENT_METADATA_SQL_V1;
}

@Provides
@Named("CREATE_CONTEXTS_SQL")
static String createContextsSql() {
return CREATE_CONTEXTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_BACKEND_INDEX")
static String getCreateEventBackendIndex() {
return CREATE_EVENT_BACKEND_INDEX_V1;
}

@Provides
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
static String createEventBackendPriorityIndex() {
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
@Named("SCHEMA_VERSION")
static int schemaVersion() {
return SCHEMA_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.android.datatransport.runtime;

import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import com.google.android.datatransport.Priority;
import com.google.auto.value.AutoValue;
Expand All @@ -26,6 +27,9 @@ public abstract class TransportContext {
/** Backend events are sent to. */
public abstract String getBackendName();

@Nullable
public abstract byte[] getExtras();

/**
* Priority of the event.
*
Expand Down Expand Up @@ -58,6 +62,8 @@ public abstract static class Builder {

public abstract Builder setBackendName(String name);

public abstract Builder setExtras(@Nullable byte[] extras);

/** @hide */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract Builder setPriority(Priority priority);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.android.datatransport.runtime.scheduling.persistence;

import static com.google.android.datatransport.runtime.scheduling.persistence.SchemaManager.SCHEMA_VERSION;

import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
import dagger.Binds;
import dagger.Module;
Expand All @@ -22,38 +24,6 @@

@Module
public abstract class EventStoreModule {
static final String CREATE_EVENTS_SQL_V1 =
"CREATE TABLE events "
+ "(_id INTEGER PRIMARY KEY,"
+ " context_id INTEGER NOT NULL,"
+ " transport_name TEXT NOT NULL,"
+ " timestamp_ms INTEGER NOT NULL,"
+ " uptime_ms INTEGER NOT NULL,"
+ " payload BLOB NOT NULL,"
+ " code INTEGER,"
+ " num_attempts INTEGER NOT NULL,"
+ "FOREIGN KEY (context_id) REFERENCES transport_contexts(_id) ON DELETE CASCADE)";

static final String CREATE_EVENT_METADATA_SQL_V1 =
"CREATE TABLE event_metadata "
+ "(_id INTEGER PRIMARY KEY,"
+ " event_id INTEGER NOT NULL,"
+ " name TEXT NOT NULL,"
+ " value TEXT NOT NULL,"
+ "FOREIGN KEY (event_id) REFERENCES events(_id) ON DELETE CASCADE)";

static final String CREATE_CONTEXTS_SQL_V1 =
"CREATE TABLE transport_contexts "
+ "(_id INTEGER PRIMARY KEY,"
+ " backend_name TEXT NOT NULL,"
+ " priority INTEGER NOT NULL,"
+ " next_request_ms INTEGER NOT NULL)";

static final String CREATE_EVENT_BACKEND_INDEX_V1 =
"CREATE INDEX events_backend_id on events(context_id)";

static final String CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1 =
"CREATE UNIQUE INDEX contexts_backend_priority on transport_contexts(backend_name, priority)";

@Provides
static EventStoreConfig storeConfig() {
Expand All @@ -67,32 +37,8 @@ static EventStoreConfig storeConfig() {
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);

@Provides
@Named("CREATE_EVENTS_SQL")
static String createEventsSql() {
return CREATE_EVENTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_METADATA_SQL")
static String createEventMetadataSql() {
return CREATE_EVENT_METADATA_SQL_V1;
}

@Provides
@Named("CREATE_CONTEXTS_SQL")
static String createContextsSql() {
return CREATE_CONTEXTS_SQL_V1;
}

@Provides
@Named("CREATE_EVENT_BACKEND_INDEX")
static String getCreateEventBackendIndex() {
return CREATE_EVENT_BACKEND_INDEX_V1;
}

@Provides
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
static String createEventBackendPriorityIndex() {
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
@Named("SCHEMA_VERSION")
static int schemaVersion() {
return SCHEMA_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.os.SystemClock;
import android.util.Base64;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
Expand All @@ -30,6 +31,7 @@
import com.google.android.datatransport.runtime.time.Monotonic;
import com.google.android.datatransport.runtime.time.WallTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -78,7 +80,6 @@ private SQLiteDatabase getDb() {
@Override
@Nullable
public PersistedEvent persist(TransportContext transportContext, EventInternal event) {

long newRowId =
inTransaction(
db -> {
Expand Down Expand Up @@ -127,20 +128,33 @@ private long ensureTransportContext(SQLiteDatabase db, TransportContext transpor
record.put("backend_name", transportContext.getBackendName());
record.put("priority", transportContext.getPriority().ordinal());
record.put("next_request_ms", 0);
if (transportContext.getExtras() != null) {
record.put("extras", Base64.encodeToString(transportContext.getExtras(), Base64.DEFAULT));
}

return db.insert("transport_contexts", null, record);
}

@Nullable
private Long getTransportContextId(SQLiteDatabase db, TransportContext transportContext) {
final StringBuilder selection = new StringBuilder("backend_name = ? and priority = ?");
ArrayList<String> selectionArgs =
new ArrayList<>(
Arrays.asList(
transportContext.getBackendName(),
String.valueOf(transportContext.getPriority().ordinal())));

if (transportContext.getExtras() != null) {
selection.append(" and extras = ?");
selectionArgs.add(Base64.encodeToString(transportContext.getExtras(), Base64.DEFAULT));
}

return tryWithCursor(
db.query(
"transport_contexts",
new String[] {"_id"},
"backend_name = ? and priority = ?",
new String[] {
transportContext.getBackendName(),
String.valueOf(transportContext.getPriority().ordinal())
},
selection.toString(),
selectionArgs.toArray(new String[0]),
null,
null,
null),
Expand Down
Loading