Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-andersen committed Jun 17, 2024
1 parent 5d8d687 commit fbc7ad9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@

package com.google.firebase.firestore.local;

import androidx.annotation.NonNull;

import com.google.protobuf.ByteString;

/**
* General purpose cache for global values.
*
* <p>Global state that cuts across components should be saved here. Following are contained herein:
*
* <p>`db_token` tracks server interaction across Listen and Write streams. This facilitates cache
* <p>`sessionToken` tracks server interaction across Listen and Write streams. This facilitates cache
* synchronization and invalidation.
*/
interface GlobalsCache {

@NonNull
ByteString getSessionsToken();

void setSessionToken(ByteString value);
void setSessionToken(@NonNull ByteString value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@

package com.google.firebase.firestore.local;

import androidx.annotation.NonNull;

import com.google.protobuf.ByteString;

/** In-memory cache of global values */
final class MemoryGlobalsCache implements GlobalsCache {

private ByteString sessionToken;
private ByteString sessionToken = ByteString.EMPTY;

@NonNull
@Override
public ByteString getSessionsToken() {
return sessionToken;
}

@Override
public void setSessionToken(ByteString value) {
public void setSessionToken(@NonNull ByteString value) {
sessionToken = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.firebase.firestore.local;

import androidx.annotation.NonNull;

import com.google.protobuf.ByteString;

public class SQLiteGlobalsCache implements GlobalsCache {
Expand All @@ -25,24 +27,25 @@ public SQLiteGlobalsCache(SQLitePersistence persistence) {
this.db = persistence;
}

@NonNull
@Override
public ByteString getSessionsToken() {
byte[] bytes = get(SESSION_TOKEN);
return bytes == null ? null : ByteString.copyFrom(bytes);
return bytes == null ? ByteString.EMPTY : ByteString.copyFrom(bytes);
}

@Override
public void setSessionToken(ByteString value) {
public void setSessionToken(@NonNull ByteString value) {
set(SESSION_TOKEN, value.toByteArray());
}

private byte[] get(String name) {
private byte[] get(@NonNull String name) {
return db.query("SELECT value FROM globals WHERE name = ?")
.binding(name)
.firstValue(row -> row.getBlob(0));
}

private void set(String name, byte[] value) {
private void set(@NonNull String name, @NonNull byte[] value) {
db.execute(
"INSERT OR REPLACE INTO globals "
+ "(name, value) "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public void setAndGetDbToken() {
globalsCache.setSessionToken(value);
assertEquals(value, globalsCache.getSessionsToken());
}

@Test
public void setAndGetEmptyDbToken() {
globalsCache.setSessionToken(ByteString.EMPTY);
assertEquals(ByteString.EMPTY, globalsCache.getSessionsToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ public void createsIndexingTables() {
assertTableExists("index_state");
}

@Test
public void createsGlobalsTable() {
schema.runSchemaUpgrades(0, 17);
assertTableExists("globals");
}

@Test
public void createsOverlaysAndMigrationTable() {
// 14 is the version we enable Overlay
Expand Down

0 comments on commit fbc7ad9

Please sign in to comment.