Skip to content

Commit 17e5ae8

Browse files
authored
Add IndexEntry (#9287)
* Add IndexEntry * Tidy up includes. * Add missing include for Hash
1 parent 779f35f commit 17e5ae8

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/index/index_entry.h"
18+
19+
#include "Firestore/core/src/util/hashing.h"
20+
#include "absl/strings/str_cat.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace index {
25+
26+
util::ComparisonResult IndexEntry::CompareTo(const IndexEntry& rhs) const {
27+
util::ComparisonResult cmp = util::Compare(index_id(), rhs.index_id());
28+
if (!util::Same(cmp)) return cmp;
29+
30+
cmp = util::Compare(document_key(), rhs.document_key());
31+
if (!util::Same(cmp)) return cmp;
32+
33+
cmp = util::Compare(directional_value(), rhs.directional_value());
34+
if (!util::Same(cmp)) return cmp;
35+
36+
return util::Compare(array_value(), rhs.array_value());
37+
}
38+
39+
std::string IndexEntry::ToString() const {
40+
return absl::StrCat("IndexEntry(", index_id(), ":", document_key().ToString(),
41+
" dir_val:", directional_value(),
42+
" array_val:", array_value(), ")");
43+
}
44+
45+
std::ostream& operator<<(std::ostream& out, const IndexEntry& entry) {
46+
return out << entry.ToString();
47+
}
48+
49+
size_t IndexEntry::Hash() const {
50+
return util::Hash(index_id(), document_key(), directional_value(),
51+
array_value());
52+
}
53+
54+
} // namespace index
55+
} // namespace firestore
56+
} // namespace firebase
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_INDEX_INDEX_ENTRY_H_
18+
#define FIRESTORE_CORE_SRC_INDEX_INDEX_ENTRY_H_
19+
20+
#include <iostream>
21+
#include <memory>
22+
#include <string>
23+
#include <utility>
24+
25+
#include "Firestore/core/src/model/document_key.h"
26+
#include "Firestore/core/src/util/comparison.h"
27+
28+
namespace firebase {
29+
namespace firestore {
30+
namespace index {
31+
32+
/** Represents an index entry saved by the SDK in its local storage. */
33+
class IndexEntry : public util::Comparable<IndexEntry> {
34+
public:
35+
IndexEntry(int32_t index_id,
36+
model::DocumentKey key,
37+
std::string array_value,
38+
std::string directional_value)
39+
: index_id_(index_id),
40+
key_(std::move(key)),
41+
array_value_(std::move(array_value)),
42+
directional_value_(std::move(directional_value)) {
43+
}
44+
45+
int32_t index_id() const {
46+
return index_id_;
47+
}
48+
49+
const model::DocumentKey& document_key() const {
50+
return key_;
51+
}
52+
53+
const std::string& array_value() const {
54+
return array_value_;
55+
}
56+
57+
const std::string& directional_value() const {
58+
return directional_value_;
59+
}
60+
61+
util::ComparisonResult CompareTo(const IndexEntry& rhs) const;
62+
size_t Hash() const;
63+
64+
std::string ToString() const;
65+
friend std::ostream& operator<<(std::ostream& out,
66+
const IndexEntry& database_id);
67+
68+
private:
69+
int32_t index_id_;
70+
model::DocumentKey key_;
71+
std::string array_value_;
72+
std::string directional_value_;
73+
};
74+
75+
} // namespace index
76+
} // namespace firestore
77+
} // namespace firebase
78+
79+
#endif // FIRESTORE_CORE_SRC_INDEX_INDEX_ENTRY_H_

0 commit comments

Comments
 (0)