Files
firebase-ios-sdk/Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
T
Greg Soltis f77ec68a88 Switch LevelDB MutationQueue and RemoteDocumentCache to use transactions (#968)
* Start work on leveldb transactions

* Style

* Working API. Not plumbed in yet

* Move files into correct place

* Wrangling file locations and associations

* Tests pass

* Add some comments

* style

* Fix copyright

* Rewrite iterator internals to handle deletion-while-iterating. Also add tests for same

* Switch to strings instead of slices

* Style

* More style fixes

* Start switching writegroup over

* Swap out write group tracking for transaction usage

* Style

* Response to feedback before updating docs

* Style

* Add comment

* Initialize version_

* Satisfy the linter

* Start switching writegroup over

* Swap out write group tracking for transaction usage

* Style

* Checkpoint before implementing BatchDescription

* Style

* Initial plumbing for leveldb local parts

* Add model::BatchId

* Port leveldb_key.{h,cc}

* Add string StartsWith

* Add leveldb_key_test.cc to the project

* Revert back to using leveldb::Slice for read/describe

These operations universally operate on keys obtained from leveldb so
it's actually unhelpful to force all the callers to make
absl::string_views from them.

* Everything passing

* Drop unused function

* Style

* STart work on reads

* Swap reads in queryCache to use transactions

* Fix up tests of querycache

* Drop commented out code

* Cleanup

* Style

* Fix up for passing tests

* style

* Renaming

* Style

* Start work on ToString for transactions

* Add ToString() method to LevelDbTransaction

* Style

* lint

* Fix includes, drop runTransaction

* current_transaction -> currentTransaction

* LevelDbTransaction::NewIterator now returns a unique_ptr

* Style

* Revert addition of util::StartsWith

* Add log line

* Style

* Add log line

* Style

* Add debug log line for commits, drop unused BatchDescription

* STart work on reads

* Swap reads in queryCache to use transactions

* Start on remote documents

* Transition mutation queue and remote documents to use transactions

* Style

* Make everything pass

* Make everything pass

* Make it compile

* Style

* Style

* Revert name change, use DefaultReadOptions()

* Style

* Handle iterators returning bad statuses
2018-03-26 11:57:34 -07:00

114 lines
3.9 KiB
Plaintext

/*
* 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 <XCTest/XCTest.h>
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Local/FSTLevelDBMigrations.h"
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
#include "leveldb/db.h"
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
NS_ASSUME_NONNULL_BEGIN
using firebase::firestore::local::LevelDbTransaction;
using firebase::firestore::util::OrderedCode;
using leveldb::DB;
using leveldb::Options;
using leveldb::Status;
@interface FSTLevelDBMigrationsTests : XCTestCase
@end
@implementation FSTLevelDBMigrationsTests {
std::shared_ptr<DB> _db;
}
- (void)setUp {
Options options;
options.error_if_exists = true;
options.create_if_missing = true;
NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
DB *db;
Status status = DB::Open(options, [dir UTF8String], &db);
XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
_db.reset(db);
}
- (void)tearDown {
_db.reset();
}
- (void)testAddsTargetGlobal {
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
LevelDbTransaction transaction(_db.get());
[FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
transaction.Commit();
metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
}
- (void)testSetsVersionNumber {
LevelDbTransaction transaction(_db.get());
FSTLevelDBSchemaVersion initial =
[FSTLevelDBMigrations schemaVersionWithTransaction:&transaction];
XCTAssertEqual(0, initial, "No version should be equivalent to 0");
// Pick an arbitrary high migration number and migrate to it.
[FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionWithTransaction:&transaction];
XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
}
- (void)testCountsQueries {
NSUInteger expected = 50;
{
// Setup some targets to be counted in the migration.
LevelDbTransaction transaction(_db.get());
for (int i = 0; i < expected; i++) {
std::string key = [FSTLevelDBTargetKey keyWithTargetID:i];
transaction.Put(key, "dummy");
}
// Add a dummy entry after the targets to make sure the iteration is correctly bounded.
// Use a table that would sort logically right after that table 'target'.
std::string dummyKey;
// Magic number that indicates a table name follows. Needed to mimic the prefix to the target
// table.
OrderedCode::WriteSignedNumIncreasing(&dummyKey, 5);
OrderedCode::WriteString(&dummyKey, "targetA");
transaction.Put(dummyKey, "dummy");
transaction.Commit();
}
{
LevelDbTransaction transaction(_db.get());
[FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
transaction.Commit();
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
XCTAssertEqual(expected, metadata.targetCount, @"Failed to count all of the targets we added");
}
}
@end
NS_ASSUME_NONNULL_END