Skip to content

Commit 3cbdbf2

Browse files
author
Greg Soltis
authored
Schema migrations for LevelDB (firebase#728)
* Implement schema versions * Style fixes * newlines, copyrights, assumptions * Fix nullability * Raw ptr -> shared_ptr * kVersionTableGlobal -> kVersionGlobalTable * Drop utils, move into static methods * Drop extra include * Add a few more comments * Move version constant into migrations file * formatting? * Fix comment
1 parent 6474a82 commit 3cbdbf2

12 files changed

+327
-73
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/* End PBXAggregateTarget section */
2424

2525
/* Begin PBXBuildFile section */
26+
132E32A6C1989C284BFE10B2 /* FSTLevelDBMigrationsTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 132E36114FC5BA5DBE3CB260 /* FSTLevelDBMigrationsTests.mm */; };
2627
3B843E4C1F3A182900548890 /* remote_store_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 3B843E4A1F3930A400548890 /* remote_store_spec_test.json */; };
2728
5436F32420008FAD006E51E3 /* string_printf_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 5436F32320008FAD006E51E3 /* string_printf_test.cc */; };
2829
54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A531FC913E500713A1A /* secure_random_test.cc */; };
@@ -67,8 +68,6 @@
6768
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB356EF6200EA5EB0089B766 /* field_value_test.cc */; };
6869
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CF82019382300D97691 /* target_id_generator_test.cc */; };
6970
AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CFC201A2EE200D97691 /* string_util_test.cc */; };
70-
AB380D02201BC69F00D97691 /* bits_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D01201BC69F00D97691 /* bits_test.cc */; };
71-
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D03201BC6E400D97691 /* ordered_code_test.cc */; };
7271
AB382F7C1FE02A1F007CA955 /* FIRDocumentReferenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */; };
7372
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */; };
7473
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
@@ -78,7 +77,6 @@
7877
AB99452C1FE3018D00DFC1E6 /* FIRQuerySnapshotTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB99452B1FE3018D00DFC1E6 /* FIRQuerySnapshotTests.m */; };
7978
AB99452E1FE30AC800DFC1E6 /* FIRFieldValueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB99452D1FE30AC800DFC1E6 /* FIRFieldValueTests.m */; };
8079
ABAEEF4F1FD5F8B100C966CB /* FIRQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = ABAEEF4E1FD5F8B100C966CB /* FIRQueryTests.m */; };
81-
ABE6637A201FA81900ED349A /* database_id_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB71064B201FA60300344F18 /* database_id_test.cc */; };
8280
ABF341051FE860CA00C48322 /* FSTAPIHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = ABF341021FE8593500C48322 /* FSTAPIHelpers.m */; };
8381
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABF6506B201131F8005F2C74 /* timestamp_test.cc */; };
8482
AFE6114F0D4DAECBA7B7C089 /* Pods_Firestore_IntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */; };
@@ -199,6 +197,7 @@
199197
/* Begin PBXFileReference section */
200198
04DF37A117F88A9891379ED6 /* Pods-Firestore_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Tests/Pods-Firestore_Tests.release.xcconfig"; sourceTree = "<group>"; };
201199
12F4357299652983A615F886 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
200+
132E36114FC5BA5DBE3CB260 /* FSTLevelDBMigrationsTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTLevelDBMigrationsTests.mm; sourceTree = "<group>"; };
202201
32AD40BF6B0E849B07FFD05E /* Pods_SwiftBuildTest.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftBuildTest.framework; sourceTree = BUILT_PRODUCTS_DIR; };
203202
3B843E4A1F3930A400548890 /* remote_store_spec_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = remote_store_spec_test.json; sourceTree = "<group>"; };
204203
42491D7DC8C8CD245CC22B93 /* Pods-SwiftBuildTest.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftBuildTest.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftBuildTest/Pods-SwiftBuildTest.debug.xcconfig"; sourceTree = "<group>"; };
@@ -254,11 +253,8 @@
254253
AB356EF6200EA5EB0089B766 /* field_value_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = field_value_test.cc; sourceTree = "<group>"; };
255254
AB380CF82019382300D97691 /* target_id_generator_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = target_id_generator_test.cc; sourceTree = "<group>"; };
256255
AB380CFC201A2EE200D97691 /* string_util_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_util_test.cc; path = ../../core/test/firebase/firestore/util/string_util_test.cc; sourceTree = "<group>"; };
257-
AB380D01201BC69F00D97691 /* bits_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = bits_test.cc; path = ../../core/test/firebase/firestore/util/bits_test.cc; sourceTree = "<group>"; };
258-
AB380D03201BC6E400D97691 /* ordered_code_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ordered_code_test.cc; path = ../../core/test/firebase/firestore/util/ordered_code_test.cc; sourceTree = "<group>"; };
259256
AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; };
260257
AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; };
261-
AB71064B201FA60300344F18 /* database_id_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_id_test.cc; sourceTree = "<group>"; };
262258
AB7BAB332012B519001E0872 /* geo_point_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = geo_point_test.cc; path = ../../core/test/firebase/firestore/geo_point_test.cc; sourceTree = "<group>"; };
263259
AB9945251FE2D71100DFC1E6 /* FIRCollectionReferenceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FIRCollectionReferenceTests.m; sourceTree = "<group>"; };
264260
AB9945271FE2DE0C00DFC1E6 /* FIRSnapshotMetadataTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FIRSnapshotMetadataTests.m; sourceTree = "<group>"; };
@@ -410,10 +406,8 @@
410406
children = (
411407
548DB926200D590300E00ABC /* assert_test.cc */,
412408
54740A521FC913E500713A1A /* autoid_test.cc */,
413-
AB380D01201BC69F00D97691 /* bits_test.cc */,
414409
548DB928200D59F600E00ABC /* comparison_test.cc */,
415410
54C2294E1FECABAE007D065B /* log_test.cc */,
416-
AB380D03201BC6E400D97691 /* ordered_code_test.cc */,
417411
54740A531FC913E500713A1A /* secure_random_test.cc */,
418412
5436F32320008FAD006E51E3 /* string_printf_test.cc */,
419413
AB380CFC201A2EE200D97691 /* string_util_test.cc */,
@@ -426,13 +420,21 @@
426420
children = (
427421
AB380CF7201937B800D97691 /* core */,
428422
AB356EF5200E9D1A0089B766 /* model */,
423+
54764FAD1FAA0C650085E60A /* Port */,
429424
54740A561FC913EB00713A1A /* util */,
430425
54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */,
431426
AB7BAB332012B519001E0872 /* geo_point_test.cc */,
432427
);
433428
name = GoogleTests;
434429
sourceTree = "<group>";
435430
};
431+
54764FAD1FAA0C650085E60A /* Port */ = {
432+
isa = PBXGroup;
433+
children = (
434+
);
435+
name = Port;
436+
sourceTree = "<group>";
437+
};
436438
6003F581195388D10070C39A = {
437439
isa = PBXGroup;
438440
children = (
@@ -555,7 +557,6 @@
555557
children = (
556558
AB356EF6200EA5EB0089B766 /* field_value_test.cc */,
557559
ABF6506B201131F8005F2C74 /* timestamp_test.cc */,
558-
AB71064B201FA60300344F18 /* database_id_test.cc */,
559560
);
560561
name = model;
561562
path = ../../core/test/firebase/firestore/model;
@@ -595,31 +596,32 @@
595596
DE51B1621F0D48AC0013853F /* Local */ = {
596597
isa = PBXGroup;
597598
children = (
599+
61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */,
600+
DE51B16A1F0D48AC0013853F /* FSTLocalStoreTests.h */,
601+
DE51B1701F0D48AC0013853F /* FSTMutationQueueTests.h */,
602+
DE51B1721F0D48AC0013853F /* FSTPersistenceTestHelpers.h */,
603+
DE51B1741F0D48AC0013853F /* FSTQueryCacheTests.h */,
604+
DE51B1771F0D48AC0013853F /* FSTRemoteDocumentCacheTests.h */,
598605
DE51B1631F0D48AC0013853F /* FSTEagerGarbageCollectorTests.m */,
599-
DE51B1641F0D48AC0013853F /* FSTLevelDBKeyTests.mm */,
600606
DE51B1651F0D48AC0013853F /* FSTLevelDBLocalStoreTests.m */,
601-
DE51B1661F0D48AC0013853F /* FSTLevelDBMutationQueueTests.mm */,
602607
DE51B1671F0D48AC0013853F /* FSTLevelDBQueryCacheTests.m */,
603-
DE51B1681F0D48AC0013853F /* FSTLevelDBRemoteDocumentCacheTests.mm */,
604608
DE51B1691F0D48AC0013853F /* FSTLocalSerializerTests.m */,
605-
DE51B16A1F0D48AC0013853F /* FSTLocalStoreTests.h */,
606609
DE51B16B1F0D48AC0013853F /* FSTLocalStoreTests.m */,
607610
DE51B16C1F0D48AC0013853F /* FSTMemoryLocalStoreTests.m */,
608611
DE51B16D1F0D48AC0013853F /* FSTMemoryMutationQueueTests.m */,
609612
DE51B16E1F0D48AC0013853F /* FSTMemoryQueryCacheTests.m */,
610613
DE51B16F1F0D48AC0013853F /* FSTMemoryRemoteDocumentCacheTests.m */,
611-
DE51B1701F0D48AC0013853F /* FSTMutationQueueTests.h */,
612614
DE51B1711F0D48AC0013853F /* FSTMutationQueueTests.m */,
613-
DE51B1721F0D48AC0013853F /* FSTPersistenceTestHelpers.h */,
614615
DE51B1731F0D48AC0013853F /* FSTPersistenceTestHelpers.m */,
615-
DE51B1741F0D48AC0013853F /* FSTQueryCacheTests.h */,
616616
DE51B1751F0D48AC0013853F /* FSTQueryCacheTests.m */,
617617
DE51B1761F0D48AC0013853F /* FSTReferenceSetTests.m */,
618-
DE51B1771F0D48AC0013853F /* FSTRemoteDocumentCacheTests.h */,
619618
DE51B1781F0D48AC0013853F /* FSTRemoteDocumentCacheTests.m */,
620619
DE51B1791F0D48AC0013853F /* FSTRemoteDocumentChangeBufferTests.m */,
620+
DE51B1641F0D48AC0013853F /* FSTLevelDBKeyTests.mm */,
621+
DE51B1661F0D48AC0013853F /* FSTLevelDBMutationQueueTests.mm */,
622+
DE51B1681F0D48AC0013853F /* FSTLevelDBRemoteDocumentCacheTests.mm */,
621623
DE51B17A1F0D48AC0013853F /* FSTWriteGroupTests.mm */,
622-
61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */,
624+
132E36114FC5BA5DBE3CB260 /* FSTLevelDBMigrationsTests.mm */,
623625
);
624626
path = Local;
625627
sourceTree = "<group>";
@@ -1225,7 +1227,6 @@
12251227
files = (
12261228
DE2EF0881F3D0B6E003D0CDC /* FSTTreeSortedDictionaryTests.m in Sources */,
12271229
DE51B1FD1F0D492C0013853F /* FSTSpecTests.m in Sources */,
1228-
ABE6637A201FA81900ED349A /* database_id_test.cc in Sources */,
12291230
ABAEEF4F1FD5F8B100C966CB /* FIRQueryTests.m in Sources */,
12301231
ABF341051FE860CA00C48322 /* FSTAPIHelpers.m in Sources */,
12311232
DE51B1CC1F0D48C00013853F /* FIRGeoPointTests.m in Sources */,
@@ -1276,7 +1277,6 @@
12761277
DE51B1E01F0D490D0013853F /* FSTMemoryQueryCacheTests.m in Sources */,
12771278
DE51B1E91F0D490D0013853F /* FSTLevelDBMutationQueueTests.mm in Sources */,
12781279
54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */,
1279-
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */,
12801280
DE51B1E61F0D490D0013853F /* FSTRemoteDocumentCacheTests.m in Sources */,
12811281
61E1D8B11FCF6C5700753285 /* StringViewTests.mm in Sources */,
12821282
DE51B1D91F0D490D0013853F /* FSTEagerGarbageCollectorTests.m in Sources */,
@@ -1293,7 +1293,6 @@
12931293
DE51B1FC1F0D492C0013853F /* FSTMockDatastore.m in Sources */,
12941294
DE51B1CE1F0D48CD0013853F /* FSTEventManagerTests.m in Sources */,
12951295
DE51B1E41F0D490D0013853F /* FSTQueryCacheTests.m in Sources */,
1296-
AB380D02201BC69F00D97691 /* bits_test.cc in Sources */,
12971296
DE51B1CD1F0D48CD0013853F /* FSTDatabaseInfoTests.m in Sources */,
12981297
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */,
12991298
548DB929200D59F600E00ABC /* comparison_test.cc in Sources */,
@@ -1302,6 +1301,7 @@
13021301
54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */,
13031302
DE51B1DD1F0D490D0013853F /* FSTLocalStoreTests.m in Sources */,
13041303
D5B25474286C9800CE42B8C2 /* FSTTestDispatchQueue.m in Sources */,
1304+
132E32A6C1989C284BFE10B2 /* FSTLevelDBMigrationsTests.mm in Sources */,
13051305
);
13061306
runOnlyForDeploymentPostprocessing = 0;
13071307
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2018 Google
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+
#import <XCTest/XCTest.h>
18+
#include <leveldb/db.h>
19+
20+
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
21+
#import "Firestore/Source/Local/FSTLevelDBMigrations.h"
22+
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
23+
24+
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
25+
26+
NS_ASSUME_NONNULL_BEGIN
27+
28+
using leveldb::DB;
29+
using leveldb::Options;
30+
using leveldb::Status;
31+
32+
@interface FSTLevelDBMigrationsTests : XCTestCase
33+
@end
34+
35+
@implementation FSTLevelDBMigrationsTests {
36+
std::shared_ptr<DB> _db;
37+
}
38+
39+
- (void)setUp {
40+
Options options;
41+
options.error_if_exists = true;
42+
options.create_if_missing = true;
43+
44+
NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
45+
DB *db;
46+
Status status = DB::Open(options, [dir UTF8String], &db);
47+
XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
48+
_db.reset(db);
49+
}
50+
51+
- (void)tearDown {
52+
_db.reset();
53+
}
54+
55+
- (void)testAddsTargetGlobal {
56+
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
57+
XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
58+
[FSTLevelDBMigrations runMigrationsOnDB:_db];
59+
metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
60+
XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
61+
}
62+
63+
- (void)testSetsVersionNumber {
64+
FSTLevelDBSchemaVersion initial = [FSTLevelDBMigrations schemaVersionForDB:_db];
65+
XCTAssertEqual(0, initial, "No version should be equivalent to 0");
66+
67+
// Pick an arbitrary high migration number and migrate to it.
68+
[FSTLevelDBMigrations runMigrationsOnDB:_db];
69+
FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionForDB:_db];
70+
XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
71+
}
72+
73+
@end
74+
75+
NS_ASSUME_NONNULL_END

Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ NS_ASSUME_NONNULL_BEGIN
2323

2424
@interface FSTPersistenceTestHelpers : NSObject
2525

26+
/**
27+
* @return The directory where a leveldb instance can store data files. Any files that existed
28+
* there will be deleted first.
29+
*/
30+
+ (NSString *)levelDBDir;
31+
2632
/**
2733
* Creates and starts a new FSTLevelDB instance for testing, destroying any previous contents
2834
* if they existed.

Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626

2727
@implementation FSTPersistenceTestHelpers
2828

29-
+ (FSTLevelDB *)levelDBPersistence {
29+
+ (NSString *)levelDBDir {
3030
NSError *error;
3131
NSFileManager *files = [NSFileManager defaultManager];
32-
3332
NSString *dir =
3433
[NSTemporaryDirectory() stringByAppendingPathComponent:@"FSTPersistenceTestHelpers"];
3534
if ([files fileExistsAtPath:dir]) {
@@ -40,12 +39,18 @@ + (FSTLevelDB *)levelDBPersistence {
4039
format:@"Failed to clean up leveldb path %@: %@", dir, error];
4140
}
4241
}
42+
return dir;
43+
}
44+
45+
+ (FSTLevelDB *)levelDBPersistence {
46+
NSString *dir = [self levelDBDir];
4347

4448
FSTDatabaseID *databaseID = [FSTDatabaseID databaseIDWithProject:@"p" database:@"d"];
4549
FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:databaseID];
4650
FSTLocalSerializer *serializer =
4751
[[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
4852
FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
53+
NSError *error;
4954
BOOL success = [db start:&error];
5055
if (!success) {
5156
[NSException raise:NSInternalInconsistencyException

Firestore/Source/Local/FSTLevelDB.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace leveldb {
2525
class DB;
26+
class ReadOptions;
2627
class Status;
2728
}
2829
#endif
@@ -70,6 +71,10 @@ NS_ASSUME_NONNULL_BEGIN
7071

7172
#ifdef __cplusplus
7273
// What follows is the Objective-C++ extension to the API.
74+
/**
75+
* @return A standard set of read options
76+
*/
77+
+ (const leveldb::ReadOptions)standardReadOptions;
7378

7479
/**
7580
* Creates an NSError based on the given status if the status is not ok.

Firestore/Source/Local/FSTLevelDB.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#import "FIRFirestoreErrors.h"
2222
#import "Firestore/Source/Core/FSTDatabaseInfo.h"
23+
#import "Firestore/Source/Local/FSTLevelDBMigrations.h"
2324
#import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
2425
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
2526
#import "Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.h"
@@ -36,6 +37,7 @@
3637

3738
using leveldb::DB;
3839
using leveldb::Options;
40+
using leveldb::ReadOptions;
3941
using leveldb::Status;
4042
using leveldb::WriteOptions;
4143

@@ -50,6 +52,15 @@ @interface FSTLevelDB ()
5052

5153
@implementation FSTLevelDB
5254

55+
/**
56+
* For now this is paranoid, but perhaps disable that in production builds.
57+
*/
58+
+ (const ReadOptions)standardReadOptions {
59+
ReadOptions options;
60+
options.verify_checksums = true;
61+
return options;
62+
}
63+
5364
- (instancetype)initWithDirectory:(NSString *)directory
5465
serializer:(FSTLocalSerializer *)serializer {
5566
if (self = [super init]) {
@@ -115,8 +126,8 @@ - (BOOL)start:(NSError **)error {
115126
if (!database) {
116127
return NO;
117128
}
118-
119129
_ptr.reset(database);
130+
[FSTLevelDBMigrations runMigrationsOnDB:_ptr];
120131
return YES;
121132
}
122133

Firestore/Source/Local/FSTLevelDBKey.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ NS_ASSUME_NONNULL_BEGIN
8282

8383
@end
8484

85+
/** A key to a singleton row storing the version of the schema. */
86+
@interface FSTLevelDBVersionKey : NSObject
87+
88+
/** Returns the key pointing to the singleton row storing the schema version. */
89+
+ (std::string)key;
90+
91+
@end
92+
8593
/** A key in the mutations table. */
8694
@interface FSTLevelDBMutationKey : NSObject
8795

Firestore/Source/Local/FSTLevelDBKey.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Firestore::StringView;
3030
using leveldb::Slice;
3131

32+
static const char *kVersionGlobalTable = "version";
3233
static const char *kMutationsTable = "mutation";
3334
static const char *kDocumentMutationsTable = "document_mutation";
3435
static const char *kMutationQueuesTable = "mutation_queue";
@@ -448,6 +449,17 @@ + (NSString *)descriptionForKey:(StringView)key {
448449

449450
@end
450451

452+
@implementation FSTLevelDBVersionKey
453+
454+
+ (std::string)key {
455+
std::string result;
456+
WriteTableName(&result, kVersionGlobalTable);
457+
WriteTerminator(&result);
458+
return result;
459+
}
460+
461+
@end
462+
451463
@implementation FSTLevelDBMutationKey {
452464
std::string _userID;
453465
}

0 commit comments

Comments
 (0)