Skip to content
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

normalize and port the rest of Firebase/Port code #713

Merged
merged 5 commits into from
Jan 27, 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
34 changes: 17 additions & 17 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB356EF6200EA5EB0089B766 /* field_value_test.cc */; };
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CF82019382300D97691 /* target_id_generator_test.cc */; };
AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CFC201A2EE200D97691 /* string_util_test.cc */; };
AB380D02201BC69F00D97691 /* bits_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D01201BC69F00D97691 /* bits_test.cc */; };
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D03201BC6E400D97691 /* ordered_code_test.cc */; };
AB382F7C1FE02A1F007CA955 /* FIRDocumentReferenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */; };
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */; };
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
Expand Down Expand Up @@ -251,6 +253,8 @@
AB356EF6200EA5EB0089B766 /* field_value_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = field_value_test.cc; sourceTree = "<group>"; };
AB380CF82019382300D97691 /* target_id_generator_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = target_id_generator_test.cc; sourceTree = "<group>"; };
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>"; };
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>"; };
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>"; };
AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; };
AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -404,8 +408,10 @@
children = (
548DB926200D590300E00ABC /* assert_test.cc */,
54740A521FC913E500713A1A /* autoid_test.cc */,
AB380D01201BC69F00D97691 /* bits_test.cc */,
548DB928200D59F600E00ABC /* comparison_test.cc */,
54C2294E1FECABAE007D065B /* log_test.cc */,
AB380D03201BC6E400D97691 /* ordered_code_test.cc */,
54740A531FC913E500713A1A /* secure_random_test.cc */,
5436F32320008FAD006E51E3 /* string_printf_test.cc */,
AB380CFC201A2EE200D97691 /* string_util_test.cc */,
Expand All @@ -418,21 +424,13 @@
children = (
AB380CF7201937B800D97691 /* core */,
AB356EF5200E9D1A0089B766 /* model */,
54764FAD1FAA0C650085E60A /* Port */,
54740A561FC913EB00713A1A /* util */,
54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */,
AB7BAB332012B519001E0872 /* geo_point_test.cc */,
);
name = GoogleTests;
sourceTree = "<group>";
};
54764FAD1FAA0C650085E60A /* Port */ = {
isa = PBXGroup;
children = (
);
name = Port;
sourceTree = "<group>";
};
6003F581195388D10070C39A = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -594,31 +592,31 @@
DE51B1621F0D48AC0013853F /* Local */ = {
isa = PBXGroup;
children = (
61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */,
DE51B16A1F0D48AC0013853F /* FSTLocalStoreTests.h */,
DE51B1701F0D48AC0013853F /* FSTMutationQueueTests.h */,
DE51B1721F0D48AC0013853F /* FSTPersistenceTestHelpers.h */,
DE51B1741F0D48AC0013853F /* FSTQueryCacheTests.h */,
DE51B1771F0D48AC0013853F /* FSTRemoteDocumentCacheTests.h */,
DE51B1631F0D48AC0013853F /* FSTEagerGarbageCollectorTests.m */,
DE51B1641F0D48AC0013853F /* FSTLevelDBKeyTests.mm */,
DE51B1651F0D48AC0013853F /* FSTLevelDBLocalStoreTests.m */,
DE51B1661F0D48AC0013853F /* FSTLevelDBMutationQueueTests.mm */,
DE51B1671F0D48AC0013853F /* FSTLevelDBQueryCacheTests.m */,
DE51B1681F0D48AC0013853F /* FSTLevelDBRemoteDocumentCacheTests.mm */,
DE51B1691F0D48AC0013853F /* FSTLocalSerializerTests.m */,
DE51B16A1F0D48AC0013853F /* FSTLocalStoreTests.h */,
DE51B16B1F0D48AC0013853F /* FSTLocalStoreTests.m */,
DE51B16C1F0D48AC0013853F /* FSTMemoryLocalStoreTests.m */,
DE51B16D1F0D48AC0013853F /* FSTMemoryMutationQueueTests.m */,
DE51B16E1F0D48AC0013853F /* FSTMemoryQueryCacheTests.m */,
DE51B16F1F0D48AC0013853F /* FSTMemoryRemoteDocumentCacheTests.m */,
DE51B1701F0D48AC0013853F /* FSTMutationQueueTests.h */,
DE51B1711F0D48AC0013853F /* FSTMutationQueueTests.m */,
DE51B1721F0D48AC0013853F /* FSTPersistenceTestHelpers.h */,
DE51B1731F0D48AC0013853F /* FSTPersistenceTestHelpers.m */,
DE51B1741F0D48AC0013853F /* FSTQueryCacheTests.h */,
DE51B1751F0D48AC0013853F /* FSTQueryCacheTests.m */,
DE51B1761F0D48AC0013853F /* FSTReferenceSetTests.m */,
DE51B1771F0D48AC0013853F /* FSTRemoteDocumentCacheTests.h */,
DE51B1781F0D48AC0013853F /* FSTRemoteDocumentCacheTests.m */,
DE51B1791F0D48AC0013853F /* FSTRemoteDocumentChangeBufferTests.m */,
DE51B1641F0D48AC0013853F /* FSTLevelDBKeyTests.mm */,
DE51B1661F0D48AC0013853F /* FSTLevelDBMutationQueueTests.mm */,
DE51B1681F0D48AC0013853F /* FSTLevelDBRemoteDocumentCacheTests.mm */,
DE51B17A1F0D48AC0013853F /* FSTWriteGroupTests.mm */,
61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */,
);
path = Local;
sourceTree = "<group>";
Expand Down Expand Up @@ -1274,6 +1272,7 @@
DE51B1E01F0D490D0013853F /* FSTMemoryQueryCacheTests.m in Sources */,
DE51B1E91F0D490D0013853F /* FSTLevelDBMutationQueueTests.mm in Sources */,
54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */,
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */,
DE51B1E61F0D490D0013853F /* FSTRemoteDocumentCacheTests.m in Sources */,
61E1D8B11FCF6C5700753285 /* StringViewTests.mm in Sources */,
DE51B1D91F0D490D0013853F /* FSTEagerGarbageCollectorTests.m in Sources */,
Expand All @@ -1290,6 +1289,7 @@
DE51B1FC1F0D492C0013853F /* FSTMockDatastore.m in Sources */,
DE51B1CE1F0D48CD0013853F /* FSTEventManagerTests.m in Sources */,
DE51B1E41F0D490D0013853F /* FSTQueryCacheTests.m in Sources */,
AB380D02201BC69F00D97691 /* bits_test.cc in Sources */,
DE51B1CD1F0D48CD0013853F /* FSTDatabaseInfoTests.m in Sources */,
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */,
548DB929200D59F600E00ABC /* comparison_test.cc in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions Firestore/Example/Tests/Local/FSTLevelDBMutationQueueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#import <XCTest/XCTest.h>
#include <leveldb/db.h>

#include "Firestore/Port/ordered_code.h"
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
Expand All @@ -29,14 +28,16 @@
#import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"

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

NS_ASSUME_NONNULL_BEGIN

using leveldb::DB;
using leveldb::Slice;
using leveldb::Status;
using leveldb::WriteOptions;
using Firestore::StringView;
using Firestore::OrderedCode;
using firebase::firestore::util::OrderedCode;

// A dummy mutation value, useful for testing code that's known to examine only mutation keys.
static const char *kDummy = "1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

#include <leveldb/db.h>

#include "Firestore/Port/ordered_code.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"

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

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

NS_ASSUME_NONNULL_BEGIN

using leveldb::WriteOptions;
using Firestore::OrderedCode;
using firebase::firestore::util::OrderedCode;

// A dummy document value, useful for testing code that's known to examine only document keys.
static const char *kDummy = "1";
Expand Down
37 changes: 21 additions & 16 deletions Firestore/Source/Local/FSTLevelDBKey.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

#include <string>

#include "Firestore/Port/ordered_code.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTPath.h"

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

NS_ASSUME_NONNULL_BEGIN

using Firestore::OrderedCode;
using firebase::firestore::util::OrderedCode;
using Firestore::StringView;
using leveldb::Slice;

Expand Down Expand Up @@ -109,11 +110,11 @@ void WriteComponentLabel(std::string *dest, FSTComponentLabel label) {
*/
BOOL ReadComponentLabel(leveldb::Slice *contents, FSTComponentLabel *label) {
int64_t rawResult = 0;
Slice tmp = *contents;
absl::string_view tmp(contents->data(), contents->size());
if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
if (rawResult >= FSTComponentLabelTerminator && rawResult <= FSTComponentLabelUnknown) {
*label = static_cast<FSTComponentLabel>(rawResult);
*contents = tmp;
*contents = leveldb::Slice(tmp.data(), tmp.size());
return YES;
}
}
Expand All @@ -128,9 +129,9 @@ BOOL ReadComponentLabel(leveldb::Slice *contents, FSTComponentLabel *label) {
*
* If the read is successful, returns YES and contents will be updated to the next unread byte.
*/
BOOL ReadComponentLabelMatching(Slice *contents, FSTComponentLabel expectedLabel) {
BOOL ReadComponentLabelMatching(absl::string_view *contents, FSTComponentLabel expectedLabel) {
int64_t rawResult = 0;
Slice tmp = *contents;
absl::string_view tmp = *contents;
if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
if (rawResult == expectedLabel) {
*contents = tmp;
Expand All @@ -152,10 +153,10 @@ BOOL ReadComponentLabelMatching(Slice *contents, FSTComponentLabel expectedLabel
*/
BOOL ReadInt32(Slice *contents, int32_t *result) {
int64_t rawResult = 0;
Slice tmp = *contents;
absl::string_view tmp(contents->data(), contents->size());
if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
if (rawResult >= INT32_MIN && rawResult <= INT32_MAX) {
*contents = tmp;
*contents = leveldb::Slice(tmp.data(), tmp.size());
*result = static_cast<int32_t>(rawResult);
return YES;
}
Expand All @@ -180,10 +181,11 @@ void WriteLabeledInt32(std::string *dest, FSTComponentLabel label, int32_t value
* value will be set to the decoded integer value.
*/
BOOL ReadLabeledInt32(Slice *contents, FSTComponentLabel expectedLabel, int32_t *value) {
Slice tmp = *contents;
absl::string_view tmp(contents->data(), contents->size());
if (ReadComponentLabelMatching(&tmp, expectedLabel)) {
if (ReadInt32(&tmp, value)) {
*contents = tmp;
Slice tmpSlice = leveldb::Slice(tmp.data(), tmp.size());
if (ReadInt32(&tmpSlice, value)) {
*contents = tmpSlice;
return YES;
}
}
Expand All @@ -207,10 +209,10 @@ void WriteLabeledString(std::string *dest, FSTComponentLabel label, StringView v
* value will be set to the decoded string value.
*/
BOOL ReadLabeledString(Slice *contents, FSTComponentLabel expectedLabel, std::string *value) {
Slice tmp = *contents;
absl::string_view tmp(contents->data(), contents->size());
if (ReadComponentLabelMatching(&tmp, expectedLabel)) {
if (OrderedCode::ReadString(&tmp, value)) {
*contents = tmp;
*contents = leveldb::Slice(tmp.data(), tmp.size());
return YES;
}
}
Expand Down Expand Up @@ -272,7 +274,7 @@ BOOL ReadDocumentKey(Slice *contents, FSTDocumentKey *__strong *result) {
for (;;) {
// Advance a temporary slice to avoid advancing contents into the next key component which may
// not be a path segment.
Slice readPosition = completeSegments;
absl::string_view readPosition(completeSegments.data(), completeSegments.size());
if (!ReadComponentLabelMatching(&readPosition, FSTComponentLabelPathSegment)) {
break;
}
Expand All @@ -284,7 +286,7 @@ BOOL ReadDocumentKey(Slice *contents, FSTDocumentKey *__strong *result) {
[pathSegments addObject:pathSegment];
segment.clear();

completeSegments = readPosition;
completeSegments = leveldb::Slice(readPosition.data(), readPosition.size());
}

FSTResourcePath *path = [FSTResourcePath pathWithSegments:pathSegments];
Expand All @@ -304,7 +306,10 @@ inline void WriteTerminator(std::string *dest) {
}

inline BOOL ReadTerminator(Slice *contents) {
return ReadComponentLabelMatching(contents, FSTComponentLabelTerminator);
absl::string_view tmp(contents->data(), contents->size());
BOOL result = ReadComponentLabelMatching(&tmp, FSTComponentLabelTerminator);
*contents = leveldb::Slice(tmp.data(), tmp.size());
return result;
}

inline void WriteTableName(std::string *dest, const char *tableName) {
Expand Down
2 changes: 0 additions & 2 deletions Firestore/Source/Local/FSTLevelDBMutationQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@
#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Util/FSTAssert.h"

#include "Firestore/Port/ordered_code.h"
#include "Firestore/core/src/firebase/firestore/util/string_util.h"

NS_ASSUME_NONNULL_BEGIN

using Firestore::OrderedCode;
using Firestore::StringView;
using leveldb::DB;
using leveldb::Iterator;
Expand Down
3 changes: 0 additions & 3 deletions Firestore/Source/Local/FSTLevelDBQueryCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Util/FSTAssert.h"

#include "Firestore/Port/ordered_code.h"

NS_ASSUME_NONNULL_BEGIN

using Firestore::OrderedCode;
using Firestore::StringView;
using leveldb::DB;
using leveldb::Iterator;
Expand Down
3 changes: 0 additions & 3 deletions Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@
#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Util/FSTAssert.h"

#include "Firestore/Port/ordered_code.h"

NS_ASSUME_NONNULL_BEGIN

using Firestore::OrderedCode;
using leveldb::DB;
using leveldb::Iterator;
using leveldb::ReadOptions;
Expand Down
3 changes: 0 additions & 3 deletions Firestore/Source/Local/FSTWriteGroup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Util/FSTAssert.h"

#include "Firestore/Port/ordered_code.h"

using Firestore::OrderedCode;
using Firestore::StringView;
using leveldb::DB;
using leveldb::Slice;
Expand Down
12 changes: 12 additions & 0 deletions Firestore/Source/Local/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <leveldb/slice.h>
#include <string>
#include "absl/strings/string_view.h"

namespace Firestore {

Expand Down Expand Up @@ -64,6 +65,10 @@ class StringView {
StringView(leveldb::Slice slice) : data_(slice.data()), size_(slice.size()) {
}

// Creates a StringView from the absl::string_view.
StringView(absl::string_view s) : data_(s.data()), size_(s.size()) {
}

// Creates a StringView from the given std::string. The string must be an
// lvalue for the lifetime requirements to be satisfied.
StringView(const std::string &str) : data_(str.data()), size_(str.size()) {
Expand All @@ -76,6 +81,13 @@ class StringView {
return leveldb::Slice(data_, size_);
}

// Converts this StringView to a absl::string_view, which is an equivalent (and more
// functional) type. The returned string_view has the same lifetime as this
// StringView.
operator absl::string_view() {
return absl::string_view(data_, size_);
}

private:
const char *data_;
const size_t size_;
Expand Down
4 changes: 4 additions & 0 deletions Firestore/core/src/firebase/firestore/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ cc_library(
SOURCES
autoid.cc
autoid.h
bits.cc
bits.h
comparison.cc
comparison.h
config.h
firebase_assert.h
log.h
ordered_code.cc
ordered_code.h
secure_random.h
string_util.cc
string_util.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

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

#include <assert.h>
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h"

namespace Firestore {
namespace firebase {
namespace firestore {
namespace util {

int Bits::Log2Floor_Portable(uint32_t n) {
if (n == 0) return -1;
Expand All @@ -32,8 +34,10 @@ int Bits::Log2Floor_Portable(uint32_t n) {
log += shift;
}
}
assert(value == 1);
FIREBASE_ASSERT(value == 1);
return log;
}

} // namespace Firestore
} // namespace util
} // namespace firestore
} // namespace firebase
Loading