From 95045820a72d6d7de2274c0bd7632f11dee53ffa Mon Sep 17 00:00:00 2001 From: zxu Date: Mon, 12 Feb 2018 20:39:31 -0500 Subject: Port Firestore Document to C++ (#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 --- .../src/firebase/firestore/model/CMakeLists.txt | 10 ++- .../core/src/firebase/firestore/model/document.cc | 50 +++++++++++ .../core/src/firebase/firestore/model/document.h | 72 +++++++++++++++ .../src/firebase/firestore/model/maybe_document.cc | 38 ++++++++ .../src/firebase/firestore/model/maybe_document.h | 100 +++++++++++++++++++++ .../src/firebase/firestore/model/no_document.cc | 32 +++++++ .../src/firebase/firestore/model/no_document.h | 36 ++++++++ 7 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 Firestore/core/src/firebase/firestore/model/document.cc create mode 100644 Firestore/core/src/firebase/firestore/model/document.h create mode 100644 Firestore/core/src/firebase/firestore/model/maybe_document.cc create mode 100644 Firestore/core/src/firebase/firestore/model/maybe_document.h create mode 100644 Firestore/core/src/firebase/firestore/model/no_document.cc create mode 100644 Firestore/core/src/firebase/firestore/model/no_document.h (limited to 'Firestore/core/src/firebase/firestore/model') diff --git a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt index 95310ec..1b0e6a4 100644 --- a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt @@ -18,16 +18,22 @@ cc_library( base_path.h database_id.cc database_id.h + document.cc + document.h document_key.cc document_key.h field_path.cc field_path.h field_value.cc field_value.h - snapshot_version.cc - snapshot_version.h + maybe_document.cc + maybe_document.h + no_document.cc + no_document.h resource_path.cc resource_path.h + snapshot_version.cc + snapshot_version.h timestamp.cc timestamp.h DEPENDS diff --git a/Firestore/core/src/firebase/firestore/model/document.cc b/Firestore/core/src/firebase/firestore/model/document.cc new file mode 100644 index 0000000..16548cd --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/document.cc @@ -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 + +#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(other); + return MaybeDocument::Equals(other) && + has_local_mutations_ == other_doc.has_local_mutations_ && + data_ == other_doc.data_; +} + +} // namespace model +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/model/document.h b/Firestore/core/src/firebase/firestore/model/document.h new file mode 100644 index 0000000..50a7b90 --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/document.h @@ -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_ diff --git a/Firestore/core/src/firebase/firestore/model/maybe_document.cc b/Firestore/core/src/firebase/firestore/model/maybe_document.cc new file mode 100644 index 0000000..4f3be1d --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/maybe_document.cc @@ -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 + +#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 diff --git a/Firestore/core/src/firebase/firestore/model/maybe_document.h b/Firestore/core/src/firebase/firestore/model/maybe_document.h new file mode 100644 index 0000000..71bd3ef --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/maybe_document.h @@ -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 + +#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 { + 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_ diff --git a/Firestore/core/src/firebase/firestore/model/no_document.cc b/Firestore/core/src/firebase/firestore/model/no_document.cc new file mode 100644 index 0000000..98cb428 --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/no_document.cc @@ -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 + +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 diff --git a/Firestore/core/src/firebase/firestore/model/no_document.h b/Firestore/core/src/firebase/firestore/model/no_document.h new file mode 100644 index 0000000..7cfd47c --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/no_document.h @@ -0,0 +1,36 @@ +/* + * 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_NO_DOCUMENT_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_NO_DOCUMENT_H_ + +#include "Firestore/core/src/firebase/firestore/model/maybe_document.h" + +namespace firebase { +namespace firestore { +namespace model { + +/** Represents that no documents exists for the key at the given version. */ +class NoDocument : public MaybeDocument { + public: + NoDocument(DocumentKey key, SnapshotVersion version); +}; + +} // namespace model +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_NO_DOCUMENT_H_ -- cgit v1.2.3