dendrite/internal/cosmosdbutil/document_seq.go
alexfca 5d68daef80
Implement Cosmos DB for the RoomServer Service (#5)
* - Implement Cosmos for the devices_table
- Use the ConnectionString in the YAML to include the Tenant
- Revert all other non implemented tables back to use SQLLite3

* - Change the Config to use "test.criticicalarc.com" Container
- Add generic function GetDocumentOrNil to standardize GetDocument
- Add func to return CrossPartition queries for Aggregates
- Add func GetNextSequence() as generic seq generator for AutoIncrement
- Add cosmosdbutil.ErrNoRows to return (emulate) sql.ErrNoRows
- Add a "fake" ExclusiveWriterFake
- Add standard "getXX", "setXX" and "queryXX" to all TABLE class files
- Add specific Table SEQ for the Events table
- Add specific Table SEQ for the Rooms table
- Add specific Table SEQ for the StateSnapshot table
2021-05-20 14:42:33 +10:00

77 lines
1.6 KiB
Go

package cosmosdbutil
import (
"context"
"fmt"
"github.com/matrix-org/dendrite/internal/cosmosdbapi"
)
type SequenceCosmosData struct {
Id string `json:"id"`
Pk string `json:"_pk"`
Cn string `json:"_cn"`
ETag string `json:"_etag"`
Timestamp int64 `json:"_ts"`
Value int64 `json:"_value"`
}
func GetNextSequence(
ctx context.Context,
connection cosmosdbapi.CosmosConnection,
config cosmosdbapi.CosmosConfig,
serviceName string,
tableName string,
seqId string,
initial int64,
) (int64, error) {
collName := fmt.Sprintf("%s_%s", tableName, seqId)
dbCollectionName := cosmosdbapi.GetCollectionName(serviceName, collName)
cosmosDocId := cosmosdbapi.GetDocumentId(config.ContainerName, dbCollectionName, seqId)
pk := cosmosDocId
dbData := SequenceCosmosData{}
cosmosdbapi.GetDocumentOrNil(
connection,
config,
ctx,
pk,
cosmosDocId,
&dbData,
)
if dbData.Id == "" {
dbData = SequenceCosmosData{}
dbData.Id = cosmosDocId
dbData.Pk = pk
dbData.Cn = dbCollectionName
dbData.Value = initial
var optionsCreate = cosmosdbapi.GetCreateDocumentOptions(dbData.Pk)
var _, _, err = cosmosdbapi.GetClient(connection).CreateDocument(
ctx,
config.DatabaseName,
config.ContainerName,
dbData,
optionsCreate,
)
if err != nil {
return -1, err
}
} else {
dbData.Value++
var optionsReplace = cosmosdbapi.GetReplaceDocumentOptions(dbData.Pk, dbData.ETag)
var _, _, err = cosmosdbapi.GetClient(connection).ReplaceDocument(
ctx,
config.DatabaseName,
config.ContainerName,
cosmosDocId,
dbData,
optionsReplace,
)
if err != nil {
return -1, err
}
}
return dbData.Value, nil
}