mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-29 01:33:10 -06:00
* - Create CosmosDocument as a base class - Add CT and UT - Refactor all tables to use the CosmosDocument * - Add UpsertDocument method to perform updates in a generic way - Add SetUpdateTime() to update the UT for updates - Refactor it all Co-authored-by: alexf@example.com <alexf@example.com>
38 lines
1,013 B
Go
38 lines
1,013 B
Go
package cosmosdbapi
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type CosmosDocument struct {
|
|
Id string `json:"id"`
|
|
Pk string `json:"_pk"`
|
|
Tn string `json:"_sid"`
|
|
Cn string `json:"_cn"`
|
|
Ct string `json:"_ct"`
|
|
Ut string `json:"_ut"`
|
|
ETag string `json:"_etag"`
|
|
Timestamp int64 `json:"_ts"`
|
|
}
|
|
|
|
func removeSpecialChars(docId string) string {
|
|
// The following characters are restricted and cannot be used in the Id property: '/', '\', '?', '#'
|
|
invalidChars := [4]string{"/", "\\", "?", "#"}
|
|
replaceChar := ","
|
|
result := docId
|
|
for _, invalidChar := range invalidChars {
|
|
result = strings.ReplaceAll(result, invalidChar, replaceChar)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func GetDocumentId(tenantName string, collectionName string, id string) string {
|
|
safeId := removeSpecialChars(id)
|
|
return fmt.Sprintf("%s,%s,%s", collectionName, tenantName, safeId)
|
|
}
|
|
|
|
func GetPartitionKey(tenantName string, collectionName string) string {
|
|
return fmt.Sprintf("%s,%s", collectionName, tenantName)
|
|
}
|