Skip to content

Use new filesystem routines in FSTLevelDB #1707

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 4 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
B6FB468E208F9BAB00554BA2 /* executor_libdispatch_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = B6FB4689208F9B9100554BA2 /* executor_libdispatch_test.mm */; };
B6FB468F208F9BAE00554BA2 /* executor_std_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6FB4687208F9B9100554BA2 /* executor_std_test.cc */; };
B6FB4690208F9BB300554BA2 /* executor_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6FB4688208F9B9100554BA2 /* executor_test.cc */; };
BEE0294A23AB993E5DE0E946 /* leveldb_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 332485C4DCC6BA0DBB5E31B7 /* leveldb_util_test.cc */; };
C1AA536F90A0A576CA2816EB /* Pods_Firestore_Example_iOS_Firestore_SwiftTests_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB92EB03E3F92485023F64ED /* Pods_Firestore_Example_iOS_Firestore_SwiftTests_iOS.framework */; };
C482E724F4B10968417C3F78 /* Pods_Firestore_FuzzTests_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B79CA87A1A01FC5329031C9B /* Pods_Firestore_FuzzTests_iOS.framework */; };
C80B10E79CDD7EF7843C321E /* type_traits_apple_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2A0CF41BA5AED6049B0BEB2C /* type_traits_apple_test.mm */; };
Expand Down Expand Up @@ -293,6 +294,7 @@
132E3BB3D5C42282B4ACFB20 /* FSTLevelDBBenchmarkTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTLevelDBBenchmarkTests.mm; sourceTree = "<group>"; };
2A0CF41BA5AED6049B0BEB2C /* type_traits_apple_test.mm */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.objcpp; path = type_traits_apple_test.mm; sourceTree = "<group>"; };
2B50B3A0DF77100EEE887891 /* Pods_Firestore_Tests_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_Tests_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
332485C4DCC6BA0DBB5E31B7 /* leveldb_util_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; path = leveldb_util_test.cc; sourceTree = "<group>"; };
358C3B5FE573B1D60A4F7592 /* strerror_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = strerror_test.cc; sourceTree = "<group>"; };
3B843E4A1F3930A400548890 /* remote_store_spec_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = remote_store_spec_test.json; sourceTree = "<group>"; };
3C81DE3772628FE297055662 /* Pods-Firestore_Example_iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example_iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example_iOS/Pods-Firestore_Example_iOS.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -712,6 +714,7 @@
isa = PBXGroup;
children = (
54995F6E205B6E12004EFFA0 /* leveldb_key_test.cc */,
332485C4DCC6BA0DBB5E31B7 /* leveldb_util_test.cc */,
F8043813A5D16963EC02B182 /* local_serializer_test.cc */,
);
path = local;
Expand Down Expand Up @@ -1669,10 +1672,14 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-Firestore_IntegrationTests_iOS/Pods-Firestore_IntegrationTests_iOS-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/leveldb-library/leveldb.framework",
"${BUILT_PRODUCTS_DIR}/GoogleTest/GoogleTest.framework",
"${BUILT_PRODUCTS_DIR}/OCMock/OCMock.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/leveldb.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleTest.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/OCMock.framework",
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1884,6 +1891,7 @@
54A0353520A3D8CB003E0143 /* iterator_adaptors_test.cc in Sources */,
618BBEAE20B89AAC00B5BCE7 /* latlng.pb.cc in Sources */,
54995F6F205B6E12004EFFA0 /* leveldb_key_test.cc in Sources */,
BEE0294A23AB993E5DE0E946 /* leveldb_util_test.cc in Sources */,
020AFD89BB40E5175838BB76 /* local_serializer_test.cc in Sources */,
54C2294F1FECABAE007D065B /* log_test.cc in Sources */,
618BBEA720B89AAC00B5BCE7 /* maybe_document.pb.cc in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions Firestore/Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ target 'Firestore_Example_iOS' do
target 'Firestore_Tests_iOS' do
inherit! :search_paths

pod 'leveldb-library'
pod 'OCMock'
pod 'GoogleTest', :podspec => 'GoogleTest.podspec'
pod 'ProtobufCpp', :podspec => 'ProtobufCpp.podspec'

pod 'OCMock'
pod 'leveldb-library'
end

target 'Firestore_Benchmarks_iOS' do
Expand All @@ -36,7 +37,10 @@ target 'Firestore_Example_iOS' do
target 'Firestore_IntegrationTests_iOS' do
inherit! :search_paths

pod 'GoogleTest', :podspec => 'GoogleTest.podspec'

pod 'OCMock'
pod 'leveldb-library'
end

target 'Firestore_SwiftTests_iOS' do
Expand Down
5 changes: 3 additions & 2 deletions Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
using firebase::firestore::model::TargetId;
using firebase::firestore::testutil::Key;
using firebase::firestore::util::OrderedCode;
using firebase::firestore::util::Path;
using leveldb::DB;
using leveldb::Options;
using leveldb::Status;
Expand All @@ -65,9 +66,9 @@ - (void)setUp {
options.error_if_exists = true;
options.create_if_missing = true;

NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
Path dir = [FSTPersistenceTestHelpers levelDBDir];
DB *db;
Status status = DB::Open(options, [dir UTF8String], &db);
Status status = DB::Open(options, dir.ToUtf8String(), &db);
XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
_db.reset(db);
}
Expand Down
3 changes: 2 additions & 1 deletion Firestore/Example/Tests/Local/FSTLevelDBQueryCacheTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using firebase::firestore::model::ResourcePath;
using firebase::firestore::model::SnapshotVersion;
using firebase::firestore::model::TargetId;
using firebase::firestore::util::Path;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -66,7 +67,7 @@ - (void)testMetadataPersistedAcrossRestarts {
[self.persistence shutdown];
self.persistence = nil;

NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
Path dir = [FSTPersistenceTestHelpers levelDBDir];

FSTLevelDB *db1 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
FSTLevelDBQueryCache *queryCache = [db1 queryCache];
Expand Down
11 changes: 6 additions & 5 deletions Firestore/Example/Tests/Local/FSTLevelDBTransactionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@

NS_ASSUME_NONNULL_BEGIN

using firebase::firestore::local::LevelDbMutationKey;
using firebase::firestore::local::LevelDbTransaction;
using firebase::firestore::util::Path;
using leveldb::DB;
using leveldb::Options;
using leveldb::ReadOptions;
using leveldb::WriteOptions;
using leveldb::Status;
using firebase::firestore::local::LevelDbMutationKey;
using firebase::firestore::local::LevelDbTransaction;
using leveldb::WriteOptions;

@interface FSTLevelDBTransactionTests : XCTestCase
@end
Expand All @@ -54,9 +55,9 @@ - (void)setUp {
options.error_if_exists = true;
options.create_if_missing = true;

NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
Path dir = [FSTPersistenceTestHelpers levelDBDir];
DB *db;
Status status = DB::Open(options, [dir UTF8String], &db);
Status status = DB::Open(options, dir.ToUtf8String(), &db);
XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
_db.reset(db);
}
Expand Down
6 changes: 4 additions & 2 deletions Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <Foundation/Foundation.h>

#include "Firestore/core/src/firebase/firestore/util/path.h"

@class FSTLevelDB;
@class FSTMemoryPersistence;

Expand All @@ -27,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
* @return The directory where a leveldb instance can store data files. Any files that existed
* there will be deleted first.
*/
+ (NSString *)levelDBDir;
+ (firebase::firestore::util::Path)levelDBDir;

/**
* Creates and starts a new FSTLevelDB instance for testing, destroying any previous contents
Expand All @@ -44,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
* present in the given directory. As a consequence, the resulting databse is not guaranteed
* to be empty.
*/
+ (FSTLevelDB *)levelDBPersistenceWithDir:(NSString *)dir;
+ (FSTLevelDB *)levelDBPersistenceWithDir:(firebase::firestore::util::Path)dir;

/** Creates and starts a new FSTMemoryPersistence instance for testing. */
+ (FSTMemoryPersistence *)eagerGCMemoryPersistence;
Expand Down
32 changes: 16 additions & 16 deletions Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"

#include <utility>

#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLocalSerializer.h"
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
Expand All @@ -30,12 +32,13 @@
namespace util = firebase::firestore::util;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::util::Path;
using firebase::firestore::util::Status;

NS_ASSUME_NONNULL_BEGIN

@implementation FSTPersistenceTestHelpers

+ (NSString *)levelDBDir {
+ (Path)levelDBDir {
Path dir = util::TempDir().AppendUtf8("FSTPersistenceTestHelpers");

// Delete the directory first to ensure isolation between runs.
Expand All @@ -46,22 +49,21 @@ + (NSString *)levelDBDir {
format:@"Failed to clean up leveldb path %s: %s", dir.c_str(), status.ToString().c_str()];
}

return dir.ToNSString();
return dir;
}

+ (FSTLevelDB *)levelDBPersistenceWithDir:(NSString *)dir {
+ (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir {
// This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
static DatabaseId database_id{"p", "d"};

FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&database_id];
FSTLocalSerializer *serializer =
[[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
NSError *error;
BOOL success = [db start:&error];
if (!success) {
FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:std::move(dir) serializer:serializer];
Status status = [db start];
if (!status.ok()) {
[NSException raise:NSInternalInconsistencyException
format:@"Failed to create leveldb path %@: %@", dir, error];
format:@"Failed to start leveldb persistence: %s", status.ToString().c_str()];
}

return db;
Expand All @@ -72,24 +74,22 @@ + (FSTLevelDB *)levelDBPersistence {
}

+ (FSTMemoryPersistence *)eagerGCMemoryPersistence {
NSError *error;
FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistenceWithEagerGC];
BOOL success = [persistence start:&error];
if (!success) {
Status status = [persistence start];
if (!status.ok()) {
[NSException raise:NSInternalInconsistencyException
format:@"Failed to start memory persistence: %@", error];
format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
}

return persistence;
}

+ (FSTMemoryPersistence *)lruMemoryPersistence {
NSError *error;
FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistenceWithLRUGC];
BOOL success = [persistence start:&error];
if (!success) {
Status status = [persistence start];
if (!status.ok()) {
[NSException raise:NSInternalInconsistencyException
format:@"Failed to start memory persistence: %@", error];
format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
}

return persistence;
Expand Down
16 changes: 8 additions & 8 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
#include "Firestore/core/src/firebase/firestore/util/filesystem.h"
#include "Firestore/core/src/firebase/firestore/util/path.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "Firestore/core/test/firebase/firestore/util/status_test_util.h"
#include "absl/memory/memory.h"

#import "Firestore/Source/API/FIRFirestore+Internal.h"
Expand All @@ -49,6 +52,8 @@
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::util::CreateAutoId;
using firebase::firestore::util::Path;
using firebase::firestore::util::Status;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -86,14 +91,9 @@ - (void)tearDown {
}

- (void)clearPersistence {
NSString *levelDBDir = [FSTLevelDB documentsDirectory];
NSError *error;
if (![[NSFileManager defaultManager] removeItemAtPath:levelDBDir error:&error]) {
// file not found is okay.
XCTAssertTrue(
[error.domain isEqualToString:NSCocoaErrorDomain] && error.code == NSFileNoSuchFileError,
@"Failed to clear LevelDB Persistence: %@", error);
}
Path levelDBDir = [FSTLevelDB documentsDirectory];
Status status = util::RecursivelyDelete(levelDBDir);
ASSERT_OK(status);
}

- (FIRFirestore *)firestore {
Expand Down
15 changes: 9 additions & 6 deletions Firestore/Source/Core/FSTFirestoreClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::OnlineState;
using firebase::firestore::util::Path;
using firebase::firestore::util::Status;
using firebase::firestore::util::internal::Executor;

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -165,26 +167,27 @@ - (void)initializeWithUser:(const User &)user usePersistence:(BOOL)usePersistenc
// external write/listen operations could get queued to run before that subsequent work
// completes.
if (usePersistence) {
NSString *dir = [FSTLevelDB storageDirectoryForDatabaseInfo:*self.databaseInfo
documentsDirectory:[FSTLevelDB documentsDirectory]];
Path dir = [FSTLevelDB storageDirectoryForDatabaseInfo:*self.databaseInfo
documentsDirectory:[FSTLevelDB documentsDirectory]];

FSTSerializerBeta *remoteSerializer =
[[FSTSerializerBeta alloc] initWithDatabaseID:&self.databaseInfo->database_id()];
FSTLocalSerializer *serializer =
[[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];

_persistence = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
_persistence = [[FSTLevelDB alloc] initWithDirectory:std::move(dir) serializer:serializer];
} else {
_persistence = [FSTMemoryPersistence persistenceWithEagerGC];
}

NSError *error;
if (![_persistence start:&error]) {
Status status = [_persistence start];
if (!status.ok()) {
// If local storage fails to start then just throw up our hands: the error is unrecoverable.
// There's nothing an end-user can do and nearly all failures indicate the developer is doing
// something grossly wrong so we should stop them cold in their tracks with a failure they
// can't ignore.
[NSException raise:NSInternalInconsistencyException format:@"Failed to open DB: %@", error];
[NSException raise:NSInternalInconsistencyException
format:@"Failed to open DB: %s", status.ToString().c_str()];
}

_localStore = [[FSTLocalStore alloc] initWithPersistence:_persistence initialUser:user];
Expand Down
Loading