aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Local/FSTLevelDBMigrations.mm
blob: b3ed935e76f17e6fd0ce0b7f214b2e2c7f452cb2 (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
/*
 * Copyright 2018 Google
 *
 * 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.
 */

#import "Firestore/Source/Local/FSTLevelDBMigrations.h"

#include <string>

#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"

#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
#include "absl/base/macros.h"
#include "absl/strings/match.h"
#include "leveldb/write_batch.h"

NS_ASSUME_NONNULL_BEGIN

// Current version of the schema defined in this file.
static FSTLevelDBSchemaVersion kSchemaVersion = 2;

using firebase::firestore::local::LevelDbTransaction;
using leveldb::DB;
using leveldb::Iterator;
using leveldb::Status;
using leveldb::Slice;
using leveldb::WriteOptions;

/**
 * Ensures that the global singleton target metadata row exists in LevelDB.
 */
static void EnsureTargetGlobal(LevelDbTransaction *transaction) {
  FSTPBTargetGlobal *targetGlobal =
      [FSTLevelDBQueryCache readTargetMetadataWithTransaction:transaction];
  if (!targetGlobal) {
    transaction->Put([FSTLevelDBTargetGlobalKey key], [FSTPBTargetGlobal message]);
  }
}

/**
 * Save the given version number as the current version of the schema of the database.
 * @param version The version to save
 * @param transaction The transaction in which to save the new version number
 */
static void SaveVersion(FSTLevelDBSchemaVersion version, LevelDbTransaction *transaction) {
  std::string key = [FSTLevelDBVersionKey key];
  std::string version_string = std::to_string(version);
  transaction->Put(key, version_string);
}

/**
 * This function counts the number of targets that currently exist in the given db. It
 * then reads the target global row, adds the count to the metadata from that row, and writes
 * the metadata back.
 *
 * It assumes the metadata has already been written and is able to be read in this transaction.
 */
static void AddTargetCount(LevelDbTransaction *transaction) {
  auto it = transaction->NewIterator();
  std::string start_key = [FSTLevelDBTargetKey keyPrefix];
  it->Seek(start_key);

  int32_t count = 0;
  while (it->Valid() && absl::StartsWith(it->key(), start_key)) {
    count++;
    it->Next();
  }

  FSTPBTargetGlobal *targetGlobal =
      [FSTLevelDBQueryCache readTargetMetadataWithTransaction:transaction];
  HARD_ASSERT(targetGlobal != nil,
              "We should have a metadata row as it was added in an earlier migration");
  targetGlobal.targetCount = count;
  transaction->Put([FSTLevelDBTargetGlobalKey key], targetGlobal);
}

@implementation FSTLevelDBMigrations

+ (FSTLevelDBSchemaVersion)schemaVersionWithTransaction:
    (firebase::firestore::local::LevelDbTransaction *)transaction {
  std::string key = [FSTLevelDBVersionKey key];
  std::string version_string;
  Status status = transaction->Get(key, &version_string);
  if (status.IsNotFound()) {
    return 0;
  } else {
    return stoi(version_string);
  }
}

+ (void)runMigrationsWithTransaction:(firebase::firestore::local::LevelDbTransaction *)transaction {
  FSTLevelDBSchemaVersion currentVersion = [self schemaVersionWithTransaction:transaction];
  // Each case in this switch statement intentionally falls through. This lets us
  // start at the current schema version and apply any migrations that have not yet
  // been applied, to bring us up to current, as defined by the kSchemaVersion constant.
  switch (currentVersion) {
    case 0:
      EnsureTargetGlobal(transaction);
      ABSL_FALLTHROUGH_INTENDED;
    case 1:
      // We're now guaranteed that the target global exists. We can safely add a count to it.
      AddTargetCount(transaction);
      ABSL_FALLTHROUGH_INTENDED;
    default:
      if (currentVersion < kSchemaVersion) {
        SaveVersion(kSchemaVersion, transaction);
      }
  }
}

@end

NS_ASSUME_NONNULL_END