Skip to content

Commit 55acc03

Browse files
authored
Firestore project: Import of API interfaces. (#1938)
- 216873604 fix a style (clang-format) for GitHub sync. by zxu <zxu> - 216859139 Implement the header of SetOptions, which will be of non-... by zxu <zxu> - 216760495 Implement the JNI part of FieldPath: by zxu <zxu> - 216580771 Implement the header of FieldPath, which will be of non-w... by zxu <zxu> - 215276303 Implement the JNI call part of Transaction Android wrapper. by zxu <zxu> - 215271876 Implement the header and common part of Transaction Andro... by zxu <zxu> - 214817327 Implement the JNI call part of WriteBatch Android wrapper. by zxu <zxu> - 214809271 Implement the header and common part of WriteBatch Androi... by zxu <zxu> - 214660417 Implement the JNI call part of the DocumentChange Android... by zxu <zxu> - 214648904 Implement the header and common part of DocumentChange An... by zxu <zxu> ORIGINAL_AUTHOR=Firebase <firebase-noreply> PiperOrigin-RevId: 216873604
1 parent 8225588 commit 55acc03

File tree

7 files changed

+461
-12
lines changed

7 files changed

+461
-12
lines changed

Firestore/core/include/firebase/firestore/document_change.h

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,88 @@
1717
#ifndef FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_CHANGE_H_
1818
#define FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_CHANGE_H_
1919

20+
#include <cstddef>
21+
22+
#include "firebase/firestore/document_snapshot.h"
23+
2024
namespace firebase {
2125
namespace firestore {
2226

27+
class DocumentChangeInternal;
28+
2329
/**
2430
* A DocumentChange represents a change to the documents matching a query. It
2531
* contains the document affected and the type of change that occurred (added,
2632
* modified, or removed).
2733
*/
28-
// TODO(zxu123): add more methods to complete the class and make it useful.
29-
class DocumentChange {};
34+
class DocumentChange {
35+
public:
36+
enum class Type {
37+
kAdded,
38+
kModified,
39+
kRemoved,
40+
};
41+
42+
static const constexpr std::size_t npos = static_cast<std::size_t>(-1);
43+
44+
/**
45+
* @brief Default constructor. This creates an invalid DocumentChange.
46+
* Attempting to perform any operations on this instance will fail (and cause
47+
* a crash) unless a valid DocumentChange has been assigned to it.
48+
*/
49+
DocumentChange();
50+
51+
/** @brief Copy constructor. */
52+
DocumentChange(const DocumentChange& value);
53+
54+
/** @brief Move constructor. */
55+
DocumentChange(DocumentChange&& value);
56+
57+
virtual ~DocumentChange();
58+
59+
/** @brief Copy assignment operator. */
60+
DocumentChange& operator=(const DocumentChange& value);
61+
62+
/** @brief Move assignment operator. */
63+
DocumentChange& operator=(DocumentChange&& value);
64+
65+
/**
66+
* Returns the type of change that occurred (added, modified, or removed).
67+
*/
68+
virtual Type type() const;
69+
70+
/**
71+
* @brief The document affected by this change.
72+
*
73+
* Returns the newly added or modified document if this DocumentChange is for
74+
* an updated document. Returns the deleted document if this document change
75+
* represents a removal.
76+
*/
77+
virtual DocumentSnapshot document() const;
78+
79+
/**
80+
* The index of the changed document in the result set immediately prior to
81+
* this DocumentChange (i.e. supposing that all prior DocumentChange objects
82+
* have been applied). Returns npos for 'added' events.
83+
*/
84+
virtual std::size_t old_index() const;
85+
86+
/**
87+
* The index of the changed document in the result set immediately after this
88+
* DocumentChange (i.e. supposing that all prior DocumentChange objects and
89+
* the current DocumentChange object have been applied). Returns npos for
90+
* 'removed' events.
91+
*/
92+
virtual std::size_t new_index() const;
93+
94+
protected:
95+
explicit DocumentChange(DocumentChangeInternal* internal);
96+
97+
private:
98+
friend class FirestoreInternal;
99+
100+
DocumentChangeInternal* internal_ = nullptr;
101+
};
30102

31103
} // namespace firestore
32104
} // namespace firebase

Firestore/core/include/firebase/firestore/document_reference.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ class DocumentReference {
387387
friend class DocumentSnapshotInternal;
388388
friend class FieldValueInternal;
389389
friend class FirestoreInternal;
390+
friend class TransactionInternal;
391+
friend class WriteBatchInternal;
390392

391393
// TODO(zxu123): investigate possibility to use std::unique_ptr or
392394
// firebase::UniquePtr.

Firestore/core/include/firebase/firestore/document_snapshot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ class DocumentSnapshot {
223223
explicit DocumentSnapshot(DocumentSnapshotInternal* internal);
224224

225225
private:
226+
friend class DocumentChangeInternal;
226227
friend class EventListenerInternal;
227228
friend class FirestoreInternal;
228229
friend class QueryInternal;
230+
friend class TransactionInternal;
229231
friend class Wrapper;
230232

231233
DocumentSnapshotInternal* internal_ = nullptr;

Firestore/core/include/firebase/firestore/field_path.h

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,71 @@
1919

2020
#include <string>
2121

22+
#if !defined(_STLPORT_VERSION)
23+
#include <initializer_list>
24+
#endif // !defined(_STLPORT_VERSION)
25+
2226
namespace firebase {
2327
namespace firestore {
2428

25-
class FieldPathInternal;
29+
namespace model {
30+
class FieldPath;
31+
} // namespace model
2632

2733
/**
2834
* A FieldPath refers to a field in a document. The path may consist of a single
2935
* field name (referring to a top level field in the document), or a list of
3036
* field names (referring to a nested field in the document).
3137
*/
32-
// TODO(zxu123): add more methods to complete the class and make it useful.
3338
class FieldPath {
34-
private:
39+
public:
40+
using FieldPathInternal = ::firebase::firestore::model::FieldPath;
41+
42+
/**
43+
* Default constructor. This creates an invalid FieldPath. Attempting to
44+
* perform any operations on this path will fail (and cause a crash) unless a
45+
* valid FieldPath has been assigned to it.
46+
*/
47+
FieldPath();
48+
49+
#if !defined(_STLPORT_VERSION)
50+
/**
51+
* Creates a FieldPath from the provided field names. If more than one field
52+
* name is provided, the path will point to a nested field in a document.
53+
*
54+
* @param field_names A list of field names.
55+
*/
56+
FieldPath(std::initializer_list<std::string> field_names);
57+
#endif // !defined(_STLPORT_VERSION)
58+
59+
/**
60+
* Copy constructor.
61+
*/
62+
FieldPath(const FieldPath& path);
63+
64+
/**
65+
* Move constructor.
66+
*/
67+
FieldPath(FieldPath&& path);
68+
69+
virtual ~FieldPath();
70+
71+
/**
72+
* Copy assignment operator.
73+
*/
74+
FieldPath& operator=(const FieldPath& path);
75+
76+
/**
77+
* Move assignment operator.
78+
*/
79+
FieldPath& operator=(FieldPath&& path);
80+
81+
/**
82+
* A special sentinel FieldPath to refer to the ID of a document. It can be
83+
* used in queries to sort or filter by the document ID.
84+
*/
85+
static FieldPath DocumentId();
86+
3587
/**
3688
* Parses a field path string into a FieldPath, treating dots as separators.
3789
*
@@ -40,8 +92,25 @@ class FieldPath {
4092
*/
4193
static FieldPath FromDotSeparatedString(const std::string& path);
4294

95+
/**
96+
* Returns a string representation of this FieldPath.
97+
*/
98+
virtual std::string ToString() const;
99+
100+
protected:
101+
explicit FieldPath(FieldPathInternal* internal);
102+
103+
private:
104+
friend bool operator==(const FieldPath& lhs, const FieldPath& rhs);
105+
friend bool operator!=(const FieldPath& lhs, const FieldPath& rhs);
106+
friend bool operator<(const FieldPath& lhs, const FieldPath& rhs);
107+
friend bool operator>(const FieldPath& lhs, const FieldPath& rhs);
108+
friend bool operator<=(const FieldPath& lhs, const FieldPath& rhs);
109+
friend bool operator>=(const FieldPath& lhs, const FieldPath& rhs);
110+
43111
friend class Query;
44112
friend class QueryInternal;
113+
friend class FieldPathConverter;
45114

46115
FieldPathInternal* internal_ = nullptr;
47116
};

Firestore/core/include/firebase/firestore/set_options.h

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#ifndef FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_SET_OPTIONS_H_
1818
#define FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_SET_OPTIONS_H_
1919

20+
#include <string>
21+
#include <vector>
22+
23+
#include "firebase/firestore/field_path.h"
24+
2025
namespace firebase {
2126
namespace firestore {
2227

@@ -27,11 +32,74 @@ namespace firestore {
2732
* granular merges instead of overwriting the target documents in their
2833
* entirety.
2934
*/
30-
// TODO(zxu123): add more methods to complete the class and make it useful.
3135
class SetOptions {
3236
public:
33-
SetOptions() {
34-
}
37+
enum class Type {
38+
kOverwrite,
39+
kMergeAll,
40+
kMergeSpecific,
41+
};
42+
43+
/**
44+
* Default constructor. This creates an invalid SetOptions. Attempting
45+
* to perform any operations on this instance will fail (and cause a crash)
46+
* unless a valid SetOptions has been assigned to it.
47+
*/
48+
SetOptions() = default;
49+
50+
/** Copy constructor. */
51+
SetOptions(const SetOptions& value) = default;
52+
53+
/** Move constructor. */
54+
SetOptions(SetOptions&& value) = default;
55+
56+
virtual ~SetOptions();
57+
58+
/** Copy assignment operator. */
59+
SetOptions& operator=(const SetOptions& value) = default;
60+
61+
/** Move assignment operator. */
62+
SetOptions& operator=(SetOptions&& value) = default;
63+
64+
/**
65+
* Returns an instance that can be used to change the behavior of set() calls
66+
* to only replace the values specified in its data argument. Fields omitted
67+
* from the set() call will remain untouched.
68+
*/
69+
static SetOptions Merge();
70+
71+
/**
72+
* Returns an instance that can be used to change the behavior of set() calls
73+
* to only replace the fields under fieldPaths. Any field that is not
74+
* specified in fieldPaths is ignored and remains untouched.
75+
*
76+
* It is an error to pass a SetOptions object to a set() call that is missing
77+
* a value for any of the fields specified here.
78+
*
79+
* @param fields The list of fields to merge. Fields can contain dots to
80+
* reference nested fields within the document.
81+
*/
82+
static SetOptions MergeField(const std::vector<std::string>& fields);
83+
84+
/**
85+
* Returns an instance that can be used to change the behavior of set() calls
86+
* to only replace the fields under fieldPaths. Any field that is not
87+
* specified in fieldPaths is ignored and remains untouched.
88+
*
89+
* It is an error to pass a SetOptions object to a set() call that is missing
90+
* a value for any of the fields specified here in its to data argument.
91+
*
92+
* @param fields The list of fields to merge.
93+
*/
94+
static SetOptions MergeField(const std::vector<FieldPath>& fields);
95+
96+
private:
97+
friend class SetOptionsInternal;
98+
99+
SetOptions(Type type, std::vector<FieldPath> fields);
100+
101+
Type type_ = Type::kOverwrite;
102+
std::vector<FieldPath> fields_;
35103
};
36104

37105
} // namespace firestore

0 commit comments

Comments
 (0)