- Add $$NULL$$ string to be used when a part of the DocId is known to be NULL (#23)

- Use the $$NULL$$ for the known nullable use cases

Co-authored-by: alexf@example.com <alexf@example.com>
This commit is contained in:
alexfca 2021-10-06 10:07:40 +11:00 committed by GitHub
parent db34c0950e
commit 49f8c7fe38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 4 deletions

View file

@ -16,6 +16,8 @@ type CosmosDocument struct {
Timestamp int64 `json:"_ts"` Timestamp int64 `json:"_ts"`
} }
var DocumentIdPartNullString string = "$$NULL$$"
func removeSpecialChars(docId string) string { func removeSpecialChars(docId string) string {
// The following characters are restricted and cannot be used in the Id property: '/', '\', '?', '#' // The following characters are restricted and cannot be used in the Id property: '/', '\', '?', '#'
invalidChars := [4]string{"/", "\\", "?", "#"} invalidChars := [4]string{"/", "\\", "?", "#"}
@ -27,6 +29,13 @@ func removeSpecialChars(docId string) string {
return result return result
} }
func EnsureIdPart(idPart string) string {
if len(idPart) == 0 {
return DocumentIdPartNullString
}
return idPart
}
func GetDocumentId(tenantName string, collectionName string, id string) string { func GetDocumentId(tenantName string, collectionName string, id string) string {
safeId := removeSpecialChars(id) safeId := removeSpecialChars(id)
return fmt.Sprintf("%s,%s,%s", collectionName, tenantName, safeId) return fmt.Sprintf("%s,%s,%s", collectionName, tenantName, safeId)

View file

@ -132,7 +132,7 @@ func ensureEventStateKeys(s *eventStateKeyStatements, ctx context.Context) {
// ON CONFLICT DO NOTHING; // ON CONFLICT DO NOTHING;
// event_state_key TEXT NOT NULL UNIQUE // event_state_key TEXT NOT NULL UNIQUE
docId := "" docId := cosmosdbapi.EnsureIdPart("")
cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId) cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId)
data := eventStateKeysCosmos{ data := eventStateKeysCosmos{

View file

@ -137,7 +137,8 @@ func (s *accountDataStatements) InsertAccountData(
} }
// UNIQUE (user_id, room_id, type) // UNIQUE (user_id, room_id, type)
docId := fmt.Sprintf("%s,%s,%s", userID, roomID, dataType) // roomId can be NULL
docId := fmt.Sprintf("%s,%s,%s", userID, cosmosdbapi.EnsureIdPart(roomID), dataType)
cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId) cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId)
dbData, _ := getAccountDataType(s, ctx, s.getPartitionKey(), cosmosDocId) dbData, _ := getAccountDataType(s, ctx, s.getPartitionKey(), cosmosDocId)

View file

@ -391,7 +391,8 @@ func (s *currentRoomStateStatements) UpsertRoomState(
// ) // )
// " ON CONFLICT (room_id, type, state_key)" + // " ON CONFLICT (room_id, type, state_key)" +
docId := fmt.Sprintf("%s,%s,%s", event.RoomID(), event.Type(), *event.StateKey()) //StateKey can be NULL
docId := fmt.Sprintf("%s,%s,%s", event.RoomID(), event.Type(), cosmosdbapi.EnsureIdPart(*event.StateKey()))
cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId) cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId)
membershipData := "" membershipData := ""
@ -556,7 +557,7 @@ func (s *currentRoomStateStatements) SelectStateEvent(
var res []byte var res []byte
// " ON CONFLICT (room_id, type, state_key)" + // " ON CONFLICT (room_id, type, state_key)" +
docId := fmt.Sprintf("%s,%s,%s", roomID, evType, stateKey) docId := fmt.Sprintf("%s,%s,%s", roomID, evType, cosmosdbapi.EnsureIdPart(stateKey))
cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId) cosmosDocId := cosmosdbapi.GetDocumentId(s.db.cosmosConfig.TenantName, s.getCollectionName(), docId)
var response, err = getEvent(s, ctx, s.getPartitionKey(), cosmosDocId) var response, err = getEvent(s, ctx, s.getPartitionKey(), cosmosDocId)