forked from firebase/firebase-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port Firestore Document to C++ (firebase#777)
* implement SnapshotVersion and test * update project * implement MaybeDocument and test * move snapshot-version from core to model * fix a bug * implement Document and test * implement NoDocument * adding type tag and fix style * fix a few bugs, discovered after merging and test run. * add assert to check FieldValue type and more test for comparision. * address changes * allow moving FieldValue to construct Document. * address changes * add document tests to project * use std::less convention * make Type::Unknown static initializer
- Loading branch information
Showing
12 changed files
with
545 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/document.h" | ||
|
||
#include <utility> | ||
|
||
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
Document::Document(FieldValue&& data, | ||
DocumentKey key, | ||
SnapshotVersion version, | ||
bool has_local_mutations) | ||
: MaybeDocument(std::move(key), std::move(version)), | ||
data_(std::move(data)), | ||
has_local_mutations_(has_local_mutations) { | ||
set_type(Type::Document); | ||
FIREBASE_ASSERT(FieldValue::Type::Object == data.type()); | ||
} | ||
|
||
bool Document::Equals(const MaybeDocument& other) const { | ||
if (other.type() != Type::Document) { | ||
return false; | ||
} | ||
const Document& other_doc = static_cast<const Document&>(other); | ||
return MaybeDocument::Equals(other) && | ||
has_local_mutations_ == other_doc.has_local_mutations_ && | ||
data_ == other_doc.data_; | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_ | ||
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/field_value.h" | ||
#include "Firestore/core/src/firebase/firestore/model/maybe_document.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
/** | ||
* Represents a document in Firestore with a key, version, data and whether the | ||
* data has local mutations applied to it. | ||
*/ | ||
class Document : public MaybeDocument { | ||
public: | ||
/** | ||
* Construct a document. FieldValue must be passed by rvalue. | ||
*/ | ||
Document(FieldValue&& data, | ||
DocumentKey key, | ||
SnapshotVersion version, | ||
bool has_local_mutations); | ||
|
||
const FieldValue& data() const { | ||
return data_; | ||
} | ||
|
||
bool has_local_mutations() const { | ||
return has_local_mutations_; | ||
} | ||
|
||
protected: | ||
bool Equals(const MaybeDocument& other) const override; | ||
|
||
private: | ||
FieldValue data_; // This is of type Object. | ||
bool has_local_mutations_; | ||
}; | ||
|
||
/** Compares against another Document. */ | ||
inline bool operator==(const Document& lhs, const Document& rhs) { | ||
return lhs.version() == rhs.version() && lhs.key() == rhs.key() && | ||
lhs.has_local_mutations() == rhs.has_local_mutations() && | ||
lhs.data() == rhs.data(); | ||
} | ||
|
||
inline bool operator!=(const Document& lhs, const Document& rhs) { | ||
return !(lhs == rhs); | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_ |
38 changes: 38 additions & 0 deletions
38
Firestore/core/src/firebase/firestore/model/maybe_document.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/maybe_document.h" | ||
|
||
#include <utility> | ||
|
||
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
MaybeDocument::MaybeDocument(DocumentKey key, SnapshotVersion version) | ||
: key_(std::move(key)), version_(std::move(version)) { | ||
} | ||
|
||
bool MaybeDocument::Equals(const MaybeDocument& other) const { | ||
return type_ == other.type_ && version_ == other.version_ && | ||
key_ == other.key_; | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
100 changes: 100 additions & 0 deletions
100
Firestore/core/src/firebase/firestore/model/maybe_document.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_ | ||
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_ | ||
|
||
#include <functional> | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/document_key.h" | ||
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
/** | ||
* The result of a lookup for a given path may be an existing document or a | ||
* tombstone that marks the path deleted. | ||
*/ | ||
class MaybeDocument { | ||
public: | ||
/** | ||
* All the different kinds of documents, including MaybeDocument and its | ||
* subclasses. This is used to provide RTTI for documents. | ||
*/ | ||
enum class Type { | ||
Unknown, | ||
Document, | ||
NoDocument, | ||
}; | ||
|
||
MaybeDocument(DocumentKey key, SnapshotVersion version); | ||
|
||
/** The runtime type of this document. */ | ||
Type type() const { | ||
return type_; | ||
} | ||
|
||
/** The key for this document. */ | ||
const DocumentKey& key() const { | ||
return key_; | ||
} | ||
|
||
/** | ||
* Returns the version of this document if it exists or a version at which | ||
* this document was guaranteed to not exist. | ||
*/ | ||
const SnapshotVersion& version() const { | ||
return version_; | ||
} | ||
|
||
protected: | ||
// Only allow subclass to set their types. | ||
void set_type(Type type) { | ||
type_ = type; | ||
} | ||
|
||
virtual bool Equals(const MaybeDocument& other) const; | ||
|
||
friend bool operator==(const MaybeDocument& lhs, const MaybeDocument& rhs); | ||
|
||
private: | ||
Type type_ = Type::Unknown; | ||
DocumentKey key_; | ||
SnapshotVersion version_; | ||
}; | ||
|
||
inline bool operator==(const MaybeDocument& lhs, const MaybeDocument& rhs) { | ||
return lhs.Equals(rhs); | ||
} | ||
|
||
inline bool operator!=(const MaybeDocument& lhs, const MaybeDocument& rhs) { | ||
return !(lhs == rhs); | ||
} | ||
|
||
/** Compares against another MaybeDocument by keys only. */ | ||
struct DocumentKeyComparator : public std::less<MaybeDocument> { | ||
bool operator()(const MaybeDocument& lhs, const MaybeDocument& rhs) const { | ||
return lhs.key() < rhs.key(); | ||
} | ||
}; | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_ |
32 changes: 32 additions & 0 deletions
32
Firestore/core/src/firebase/firestore/model/no_document.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/no_document.h" | ||
|
||
#include <utility> | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
NoDocument::NoDocument(DocumentKey key, SnapshotVersion version) | ||
: MaybeDocument(std::move(key), std::move(version)) { | ||
set_type(Type::NoDocument); | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
Oops, something went wrong.