Skip to content

Commit 3097102

Browse files
authored
Merge 254a394 into a026e3d
2 parents a026e3d + 254a394 commit 3097102

File tree

3 files changed

+437
-0
lines changed

3 files changed

+437
-0
lines changed

Firestore/core/src/local/leveldb_key.cc

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const char* kCollectionParentsTable = "collection_parent";
5050
const char* kRemoteDocumentReadTimeTable = "remote_document_read_time";
5151
const char* kBundlesTable = "bundles";
5252
const char* kNamedQueriesTable = "named_queries";
53+
const char* kIndexConfigurationTable = "index_configuration";
54+
const char* kIndexStateTable = "index_state";
55+
const char* kIndexEntriesTable = "index_entries";
5356

5457
/**
5558
* Labels for the components of keys. These serve to make keys self-describing.
@@ -115,6 +118,11 @@ enum ComponentLabel {
115118
/** A component containing the name of a named query. */
116119
QueryName = 18,
117120

121+
/** A component containing a index id. */
122+
IndexId = 19,
123+
IndexArrayValue = 20,
124+
IndexDirectionalValue = 21,
125+
118126
/**
119127
* A path segment describes just a single segment in a resource path. Path
120128
* segments that occur sequentially in a key represent successive segments in
@@ -201,6 +209,18 @@ class Reader {
201209
return ReadLabeledString(ComponentLabel::QueryName);
202210
}
203211

212+
int32_t ReadIndexId() {
213+
return ReadLabeledInt32(ComponentLabel::IndexId);
214+
}
215+
216+
std::string ReadIndexArrayValue() {
217+
return ReadLabeledString(ComponentLabel::IndexArrayValue);
218+
}
219+
220+
std::string ReadIndexDirectionalValue() {
221+
return ReadLabeledString(ComponentLabel::IndexDirectionalValue);
222+
}
223+
204224
/**
205225
* Reads a snapshot version, encoded as a component label and a pair of
206226
* seconds (int64) and nanoseconds (int32).
@@ -567,6 +587,21 @@ std::string Reader::Describe() {
567587
if (ok_) {
568588
absl::StrAppend(&description, " query_name=", query_name);
569589
}
590+
} else if (label == ComponentLabel::IndexId) {
591+
int32_t index_id = ReadIndexId();
592+
if (ok_) {
593+
absl::StrAppend(&description, " index_id=", index_id);
594+
}
595+
} else if (label == ComponentLabel::IndexArrayValue) {
596+
const std::string& value = ReadIndexArrayValue();
597+
if (ok_) {
598+
absl::StrAppend(&description, " array_value=", value);
599+
}
600+
} else if (label == ComponentLabel::IndexDirectionalValue) {
601+
const std::string& value = ReadIndexDirectionalValue();
602+
if (ok_) {
603+
absl::StrAppend(&description, " directional_value=", value);
604+
}
570605
} else {
571606
absl::StrAppend(&description, " unknown label=", static_cast<int>(label));
572607
Fail();
@@ -650,6 +685,18 @@ class Writer {
650685
}
651686
}
652687

688+
void WriteIndexId(int32_t id) {
689+
WriteLabeledInt32(ComponentLabel::IndexId, id);
690+
}
691+
692+
void WriteIndexArrayValue(absl::string_view value) {
693+
WriteLabeledString(ComponentLabel::IndexArrayValue, value);
694+
}
695+
696+
void WriteIndexDirectionalValue(absl::string_view value) {
697+
WriteLabeledString(ComponentLabel::IndexDirectionalValue, value);
698+
}
699+
653700
private:
654701
/** Writes a component label to the given key destination. */
655702
void WriteComponentLabel(ComponentLabel label) {
@@ -1094,6 +1141,87 @@ bool LevelDbNamedQueryKey::Decode(absl::string_view key) {
10941141
return reader.ok();
10951142
}
10961143

1144+
std::string LevelDbIndexConfigurationKey::KeyPrefix() {
1145+
Writer writer;
1146+
writer.WriteTableName(kIndexConfigurationTable);
1147+
return writer.result();
1148+
}
1149+
1150+
std::string LevelDbIndexConfigurationKey::Key(int32_t id) {
1151+
Writer writer;
1152+
writer.WriteTableName(kIndexConfigurationTable);
1153+
writer.WriteIndexId(id);
1154+
writer.WriteTerminator();
1155+
return writer.result();
1156+
}
1157+
1158+
bool LevelDbIndexConfigurationKey::Decode(absl::string_view key) {
1159+
Reader reader{key};
1160+
reader.ReadTableNameMatching(kIndexConfigurationTable);
1161+
index_id_ = reader.ReadIndexId();
1162+
reader.ReadTerminator();
1163+
return reader.ok();
1164+
}
1165+
1166+
std::string LevelDbIndexStateKey::KeyPrefix() {
1167+
Writer writer;
1168+
writer.WriteTableName(kIndexStateTable);
1169+
return writer.result();
1170+
}
1171+
1172+
std::string LevelDbIndexStateKey::Key(int32_t index_id,
1173+
absl::string_view user_id) {
1174+
Writer writer;
1175+
writer.WriteTableName(kIndexStateTable);
1176+
writer.WriteIndexId(index_id);
1177+
writer.WriteUserId(user_id);
1178+
writer.WriteTerminator();
1179+
return writer.result();
1180+
}
1181+
1182+
bool LevelDbIndexStateKey::Decode(absl::string_view key) {
1183+
Reader reader{key};
1184+
reader.ReadTableNameMatching(kIndexStateTable);
1185+
index_id_ = reader.ReadIndexId();
1186+
user_id_ = reader.ReadUserId();
1187+
reader.ReadTerminator();
1188+
return reader.ok();
1189+
}
1190+
1191+
std::string LevelDbIndexEntryKey::KeyPrefix() {
1192+
Writer writer;
1193+
writer.WriteTableName(kIndexEntriesTable);
1194+
return writer.result();
1195+
}
1196+
1197+
std::string LevelDbIndexEntryKey::Key(int32_t index_id,
1198+
absl::string_view user_id,
1199+
absl::string_view array_value,
1200+
absl::string_view dicrectional_value,
1201+
absl::string_view document_name) {
1202+
Writer writer;
1203+
writer.WriteTableName(kIndexEntriesTable);
1204+
writer.WriteIndexId(index_id);
1205+
writer.WriteUserId(user_id);
1206+
writer.WriteIndexArrayValue(array_value);
1207+
writer.WriteIndexDirectionalValue(dicrectional_value);
1208+
writer.WriteDocumentId(document_name);
1209+
writer.WriteTerminator();
1210+
return writer.result();
1211+
}
1212+
1213+
bool LevelDbIndexEntryKey::Decode(absl::string_view key) {
1214+
Reader reader{key};
1215+
reader.ReadTableNameMatching(kIndexEntriesTable);
1216+
index_id_ = reader.ReadIndexId();
1217+
user_id_ = reader.ReadUserId();
1218+
array_value_ = reader.ReadIndexArrayValue();
1219+
directional_value_ = reader.ReadIndexDirectionalValue();
1220+
document_name_ = reader.ReadDocumentId();
1221+
reader.ReadTerminator();
1222+
return reader.ok();
1223+
}
1224+
10971225
} // namespace local
10981226
} // namespace firestore
10991227
} // namespace firebase

Firestore/core/src/local/leveldb_key.h

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ namespace local {
9797
// named_queries:
9898
// - table_name: string = "named_queries"
9999
// - name: string
100+
//
101+
// index_configuration:
102+
// - table_name: string = "index_configuration"
103+
// - index_id: int32_t
104+
//
105+
// index_state:
106+
// - table_name: string = "index_state"
107+
// - index_id: int32_t
108+
// - user_id: string
109+
//
110+
// index_entries:
111+
// - table_name: string = "index_entries"
112+
// - index_id: int32_t
113+
// - user_id: string
114+
// - array_value: string
115+
// - directional_value: string
116+
// - document_name: string
100117

101118
/**
102119
* Parses the given key and returns a human readable description of its
@@ -713,6 +730,150 @@ class LevelDbNamedQueryKey {
713730
std::string name_;
714731
};
715732

733+
/**
734+
* A key in the index_configuration table, storing the index definition proto,
735+
* and the collection (group) it applies to.
736+
*/
737+
class LevelDbIndexConfigurationKey {
738+
public:
739+
/**
740+
* Creates a key prefix that points just before the first key of the table.
741+
*/
742+
static std::string KeyPrefix();
743+
744+
/**
745+
* Creates a key that points to the key for the given index id.
746+
*/
747+
static std::string Key(int32_t id);
748+
749+
/**
750+
* Decodes the given complete key, storing the decoded values in this
751+
* instance.
752+
*
753+
* @return true if the key successfully decoded, false otherwise. If false is
754+
* returned, this instance is in an undefined state until the next call to
755+
* `Decode()`.
756+
*/
757+
ABSL_MUST_USE_RESULT
758+
bool Decode(absl::string_view key);
759+
760+
/** The index id for this entry. */
761+
int32_t index_id() const {
762+
return index_id_;
763+
}
764+
765+
private:
766+
int32_t index_id_;
767+
};
768+
769+
/**
770+
* A key in the index_state table, storing the per index per user state tracking
771+
* backfilling state for each index.
772+
*/
773+
class LevelDbIndexStateKey {
774+
public:
775+
/**
776+
* Creates a key prefix that points just before the first key of the table.
777+
*/
778+
static std::string KeyPrefix();
779+
780+
/**
781+
* Creates a key that points to the key for the given index id and user id.
782+
*/
783+
static std::string Key(int32_t index_id, absl::string_view user_id);
784+
785+
/**
786+
* Decodes the given complete key, storing the decoded values in this
787+
* instance.
788+
*
789+
* @return true if the key successfully decoded, false otherwise. If false is
790+
* returned, this instance is in an undefined state until the next call to
791+
* `Decode()`.
792+
*/
793+
ABSL_MUST_USE_RESULT
794+
bool Decode(absl::string_view key);
795+
796+
/** The index id for this entry. */
797+
int32_t index_id() const {
798+
return index_id_;
799+
}
800+
801+
/** The user id for this entry. */
802+
const std::string& user_id() const {
803+
return user_id_;
804+
}
805+
806+
private:
807+
int32_t index_id_;
808+
std::string user_id_;
809+
};
810+
811+
/**
812+
* A key in the index_entries table, storing the the encoded entries for all
813+
* fields used by a given index.
814+
*
815+
* Note: `array_value` is expected to be set for all queries.
816+
*/
817+
class LevelDbIndexEntryKey {
818+
public:
819+
/**
820+
* Creates a key prefix that points just before the first key of the table.
821+
*/
822+
static std::string KeyPrefix();
823+
824+
/**
825+
* Creates a key that points to the key for the given index entry fields.
826+
*/
827+
static std::string Key(int32_t index_id,
828+
absl::string_view user_id,
829+
absl::string_view array_value,
830+
absl::string_view directional_value,
831+
absl::string_view document_name);
832+
833+
/**
834+
* Decodes the given complete key, storing the decoded values in this
835+
* instance.
836+
*
837+
* @return true if the key successfully decoded, false otherwise. If false is
838+
* returned, this instance is in an undefined state until the next call to
839+
* `Decode()`.
840+
*/
841+
ABSL_MUST_USE_RESULT
842+
bool Decode(absl::string_view key);
843+
844+
/** The index id for this entry. */
845+
int32_t index_id() const {
846+
return index_id_;
847+
}
848+
849+
/** The user id for this entry. */
850+
const std::string& user_id() const {
851+
return user_id_;
852+
}
853+
854+
/** The array index value encoded for this entry. */
855+
const std::string& array_value() const {
856+
return array_value_;
857+
}
858+
859+
/** The directional index value encoded for this entry. */
860+
const std::string& directional_value() const {
861+
return directional_value_;
862+
}
863+
864+
/** The document name this entry points to. */
865+
const std::string& document_name() const {
866+
return document_name_;
867+
}
868+
869+
private:
870+
int32_t index_id_;
871+
std::string user_id_;
872+
std::string array_value_;
873+
std::string directional_value_;
874+
std::string document_name_;
875+
};
876+
716877
} // namespace local
717878
} // namespace firestore
718879
} // namespace firebase

0 commit comments

Comments
 (0)