droboticflow/internal/store/object_sync.go
2026-07-28 20:43:46 +08:00

1455 lines
53 KiB
Go

package store
import (
"context"
"encoding/base64"
"errors"
"sort"
"strconv"
"strings"
"time"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
ErrObjectSyncLeaseHeld = errors.New("object sync resource lease is held by another item")
)
type ObjectSyncStore interface {
ListObjectSyncSpaces(context.Context, string) ([]domain.ObjectSyncSpaceRecord, error)
GetObjectSyncSpace(context.Context, string) (*domain.ObjectSyncSpaceRecord, error)
UpsertObjectSyncSpace(context.Context, domain.ObjectSyncSpaceRecord) error
ListObjectSyncDevices(context.Context, string) ([]domain.ObjectSyncDeviceRecord, error)
GetObjectSyncDevice(context.Context, string) (*domain.ObjectSyncDeviceRecord, error)
UpsertObjectSyncDevice(context.Context, domain.ObjectSyncDeviceRecord) error
ListObjectSyncAuthorizations(context.Context, string, string) ([]domain.ObjectSyncDeviceAuthorizationRecord, error)
GetObjectSyncAuthorization(context.Context, string) (*domain.ObjectSyncDeviceAuthorizationRecord, error)
GetObjectSyncAuthorizationByDeviceCodeHash(context.Context, string) (*domain.ObjectSyncDeviceAuthorizationRecord, error)
GetObjectSyncAuthorizationByUserCode(context.Context, string) (*domain.ObjectSyncDeviceAuthorizationRecord, error)
UpsertObjectSyncAuthorization(context.Context, domain.ObjectSyncDeviceAuthorizationRecord) error
ListObjectSyncCredentials(context.Context, string) ([]domain.ObjectSyncCredentialRecord, error)
GetObjectSyncCredential(context.Context, string) (*domain.ObjectSyncCredentialRecord, error)
GetObjectSyncCredentialByTokenHash(context.Context, string) (*domain.ObjectSyncCredentialRecord, error)
UpsertObjectSyncCredential(context.Context, domain.ObjectSyncCredentialRecord) error
ListObjectSyncSessions(context.Context, domain.ObjectSyncListQuery) (domain.ObjectSyncSessionPage, error)
GetObjectSyncSession(context.Context, string) (*domain.ObjectSyncSessionRecord, error)
UpsertObjectSyncSession(context.Context, domain.ObjectSyncSessionRecord) error
ClaimObjectSyncSession(context.Context, domain.ObjectSyncSessionStatus, domain.ObjectSyncSessionStatus, string) (*domain.ObjectSyncSessionRecord, error)
GetObjectSyncItem(context.Context, string) (*domain.ObjectSyncItemRecord, error)
UpsertObjectSyncItems(context.Context, []domain.ObjectSyncItemRecord) error
ListObjectSyncItems(context.Context, domain.ObjectSyncListQuery) (domain.ObjectSyncItemPage, error)
ClaimObjectSyncItem(context.Context, string, time.Duration) (*domain.ObjectSyncItemRecord, error)
ClaimObjectSyncCleanupItem(context.Context, string, time.Time, time.Duration) (*domain.ObjectSyncItemRecord, error)
AcquireObjectSyncLease(context.Context, domain.ObjectSyncLeaseRecord, time.Duration) (*domain.ObjectSyncLeaseRecord, bool, error)
RenewObjectSyncLease(context.Context, string, string, string, int64, time.Duration) (bool, error)
ReleaseObjectSyncLease(context.Context, string, string, string, int64) error
ForceReleaseObjectSyncLease(context.Context, string) (*domain.ObjectSyncLeaseRecord, bool, error)
GetObjectSyncHead(context.Context, string, string, string) (*domain.ObjectSyncObjectHeadRecord, error)
UpsertObjectSyncHead(context.Context, domain.ObjectSyncObjectHeadRecord) error
ListObjectSyncHeads(context.Context, string, string, string, int) (domain.ObjectSyncHeadPage, error)
InsertObjectSyncVersion(context.Context, domain.ObjectSyncVersionRecord) error
AppendObjectSyncAudit(context.Context, domain.ObjectSyncAuditEvent) error
ListObjectSyncAudit(context.Context, domain.ObjectSyncListQuery) ([]domain.ObjectSyncAuditEvent, error)
ListObjectSyncAuditRange(context.Context, string, string, string, string, int) (domain.ObjectSyncAuditPage, error)
GetLatestObjectSyncAudit(context.Context, string) (*domain.ObjectSyncAuditEvent, error)
GetObjectSyncAuditSeal(context.Context, string, string) (*domain.ObjectSyncAuditSealRecord, error)
InsertObjectSyncAuditSeal(context.Context, domain.ObjectSyncAuditSealRecord) error
ListObjectSyncReleases(context.Context, string) ([]domain.ObjectSyncReleaseRecord, error)
UpsertObjectSyncRelease(context.Context, domain.ObjectSyncReleaseRecord) error
ClaimObjectSyncRelease(context.Context, string, time.Duration) (*domain.ObjectSyncReleaseRecord, error)
ListObjectSyncClientReleases(context.Context) ([]domain.ObjectSyncClientReleaseRecord, error)
UpsertObjectSyncClientRelease(context.Context, domain.ObjectSyncClientReleaseRecord) error
UpsertObjectSyncReconcile(context.Context, domain.ObjectSyncReconcileRecord) error
ClaimObjectSyncReconcile(context.Context, string, time.Duration) (*domain.ObjectSyncReconcileRecord, error)
BeginObjectSyncIdempotency(context.Context, domain.ObjectSyncIdempotencyRecord) (*domain.ObjectSyncIdempotencyRecord, bool, error)
CompleteObjectSyncIdempotency(context.Context, domain.ObjectSyncIdempotencyRecord) error
DeleteObjectSyncIdempotency(context.Context, string, string) error
ConsumeObjectSyncRateLimit(context.Context, domain.ObjectSyncRateLimitRecord, int64) (domain.ObjectSyncRateLimitRecord, bool, error)
}
func AsObjectSyncStore(value Store) (ObjectSyncStore, bool) {
target, ok := value.(ObjectSyncStore)
return target, ok
}
func objectSyncLimit(value int) int {
if value <= 0 {
return 100
}
if value > 1000 {
return 1000
}
return value
}
func decodeObjectSyncCursor(value string) string {
if value == "" {
return ""
}
decoded, err := base64.RawURLEncoding.DecodeString(value)
if err != nil {
return value
}
return string(decoded)
}
func encodeObjectSyncCursor(value string) string {
if value == "" {
return ""
}
return base64.RawURLEncoding.EncodeToString([]byte(value))
}
func objectSyncSessionCursor(item domain.ObjectSyncSessionRecord) string {
return item.CreatedAt + "\x00" + item.ID
}
func (store *MemoryStore) BeginObjectSyncIdempotency(_ context.Context, requested domain.ObjectSyncIdempotencyRecord) (*domain.ObjectSyncIdempotencyRecord, bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
if item, ok := store.objectSyncIdempotency[requested.ID]; ok {
copy := item
return &copy, false, nil
}
store.objectSyncIdempotency[requested.ID] = requested
copy := requested
return &copy, true, nil
}
func (store *MemoryStore) ConsumeObjectSyncRateLimit(_ context.Context, requested domain.ObjectSyncRateLimitRecord, limit int64) (domain.ObjectSyncRateLimitRecord, bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, exists := store.objectSyncRateLimits[requested.ID]
if !exists || (!item.ExpiresAt.IsZero() && item.ExpiresAt.Before(time.Now().UTC())) {
item = requested
item.Count = 0
}
item.Count++
store.objectSyncRateLimits[requested.ID] = item
return item, limit <= 0 || item.Count <= limit, nil
}
func (store *MemoryStore) UpsertObjectSyncReconcile(_ context.Context, item domain.ObjectSyncReconcileRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncReconciles[item.ID] = item
return nil
}
func (store *MemoryStore) ClaimObjectSyncReconcile(_ context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncReconcileRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
now := time.Now().UTC()
for id, item := range store.objectSyncReconciles {
if item.Status != "pending" || (item.CoordinatorOwner != "" && item.CoordinatorLeaseUntil.After(now)) {
continue
}
item.Status = "running"
item.CoordinatorOwner = owner
item.CoordinatorLeaseUntil = now.Add(ttl)
item.UpdatedAt = now.Format(time.RFC3339Nano)
store.objectSyncReconciles[id] = item
copy := item
return &copy, nil
}
return nil, nil
}
func (store *MemoryStore) CompleteObjectSyncIdempotency(_ context.Context, item domain.ObjectSyncIdempotencyRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
existing, ok := store.objectSyncIdempotency[item.ID]
if !ok || existing.RequestDigest != item.RequestDigest {
return mongo.ErrNoDocuments
}
store.objectSyncIdempotency[item.ID] = item
return nil
}
func (store *MemoryStore) DeleteObjectSyncIdempotency(_ context.Context, id, requestDigest string) error {
store.mu.Lock()
defer store.mu.Unlock()
if item, ok := store.objectSyncIdempotency[id]; ok && item.RequestDigest == requestDigest {
delete(store.objectSyncIdempotency, id)
}
return nil
}
func (store *MemoryStore) ListObjectSyncSpaces(_ context.Context, organizationID string) ([]domain.ObjectSyncSpaceRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncSpaceRecord, 0)
for _, item := range store.objectSyncSpaces {
if item.OrganizationID == normalizedOrganizationID(organizationID) {
items = append(items, item)
}
}
sort.Slice(items, func(i, j int) bool { return items[i].UpdatedAt > items[j].UpdatedAt })
return items, nil
}
func (store *MemoryStore) GetObjectSyncSpace(_ context.Context, id string) (*domain.ObjectSyncSpaceRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncSpaces[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertObjectSyncSpace(_ context.Context, item domain.ObjectSyncSpaceRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncSpaces[item.ID] = item
return nil
}
func (store *MemoryStore) ListObjectSyncDevices(_ context.Context, organizationID string) ([]domain.ObjectSyncDeviceRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncDeviceRecord, 0)
for _, item := range store.objectSyncDevices {
if item.OrganizationID == normalizedOrganizationID(organizationID) {
items = append(items, item)
}
}
sort.Slice(items, func(i, j int) bool { return items[i].UpdatedAt > items[j].UpdatedAt })
return items, nil
}
func (store *MemoryStore) GetObjectSyncDevice(_ context.Context, id string) (*domain.ObjectSyncDeviceRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncDevices[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertObjectSyncDevice(_ context.Context, item domain.ObjectSyncDeviceRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncDevices[item.ID] = item
return nil
}
func (store *MemoryStore) ListObjectSyncAuthorizations(_ context.Context, organizationID, status string) ([]domain.ObjectSyncDeviceAuthorizationRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncDeviceAuthorizationRecord, 0)
for _, item := range store.objectSyncAuthorizations {
if organizationID != "" && item.OrganizationID != normalizedOrganizationID(organizationID) {
continue
}
if status != "" && item.Status != status {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt > items[j].CreatedAt })
return items, nil
}
func (store *MemoryStore) GetObjectSyncAuthorization(_ context.Context, id string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncAuthorizations[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) GetObjectSyncAuthorizationByDeviceCodeHash(_ context.Context, hash string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
for _, item := range store.objectSyncAuthorizations {
if item.DeviceCodeHash == hash {
copy := item
return &copy, nil
}
}
return nil, nil
}
func (store *MemoryStore) GetObjectSyncAuthorizationByUserCode(_ context.Context, code string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
for _, item := range store.objectSyncAuthorizations {
if item.UserCode == code {
copy := item
return &copy, nil
}
}
return nil, nil
}
func (store *MemoryStore) UpsertObjectSyncAuthorization(_ context.Context, item domain.ObjectSyncDeviceAuthorizationRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncAuthorizations[item.ID] = item
return nil
}
func (store *MemoryStore) ListObjectSyncCredentials(_ context.Context, organizationID string) ([]domain.ObjectSyncCredentialRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncCredentialRecord, 0)
for _, item := range store.objectSyncCredentials {
if item.OrganizationID == normalizedOrganizationID(organizationID) {
items = append(items, item)
}
}
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt > items[j].CreatedAt })
return items, nil
}
func (store *MemoryStore) GetObjectSyncCredential(_ context.Context, id string) (*domain.ObjectSyncCredentialRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncCredentials[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) GetObjectSyncCredentialByTokenHash(_ context.Context, hash string) (*domain.ObjectSyncCredentialRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
for _, item := range store.objectSyncCredentials {
if item.TokenHash == hash {
copy := item
return &copy, nil
}
}
return nil, nil
}
func (store *MemoryStore) UpsertObjectSyncCredential(_ context.Context, item domain.ObjectSyncCredentialRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncCredentials[item.ID] = item
return nil
}
func (store *MemoryStore) ListObjectSyncSessions(_ context.Context, query domain.ObjectSyncListQuery) (domain.ObjectSyncSessionPage, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncSessionRecord, 0)
decodedCursor := decodeObjectSyncCursor(query.Cursor)
for _, item := range store.objectSyncSessions {
if query.OrganizationID != "" && item.OrganizationID != normalizedOrganizationID(query.OrganizationID) {
continue
}
if query.Status != "" && string(item.Status) != query.Status {
continue
}
if decodedCursor != "" && objectSyncSessionCursor(item) >= decodedCursor {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool {
return objectSyncSessionCursor(items[i]) > objectSyncSessionCursor(items[j])
})
limit := objectSyncLimit(query.Limit)
page := domain.ObjectSyncSessionPage{Items: items}
if len(items) > limit {
page.Items = items[:limit]
page.NextCursor = encodeObjectSyncCursor(objectSyncSessionCursor(page.Items[len(page.Items)-1]))
}
return page, nil
}
func (store *MemoryStore) GetObjectSyncSession(_ context.Context, id string) (*domain.ObjectSyncSessionRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncSessions[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertObjectSyncSession(_ context.Context, item domain.ObjectSyncSessionRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncSessions[item.ID] = item
return nil
}
func (store *MemoryStore) ClaimObjectSyncSession(_ context.Context, from, to domain.ObjectSyncSessionStatus, operationID string) (*domain.ObjectSyncSessionRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
for id, item := range store.objectSyncSessions {
if item.Status != from {
continue
}
item.Status = to
item.OperationID = operationID
item.UpdatedAt = nowISO()
store.objectSyncSessions[id] = item
copy := item
return &copy, nil
}
return nil, nil
}
func (store *MemoryStore) GetObjectSyncItem(_ context.Context, id string) (*domain.ObjectSyncItemRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncItems[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertObjectSyncItems(_ context.Context, items []domain.ObjectSyncItemRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
for _, item := range items {
store.objectSyncItems[item.ID] = item
}
return nil
}
func (store *MemoryStore) ListObjectSyncItems(_ context.Context, query domain.ObjectSyncListQuery) (domain.ObjectSyncItemPage, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncItemRecord, 0)
for _, item := range store.objectSyncItems {
if query.SessionID != "" && item.SessionID != query.SessionID {
continue
}
if query.OrganizationID != "" && item.OrganizationID != normalizedOrganizationID(query.OrganizationID) {
continue
}
if query.Status != "" && string(item.Status) != query.Status {
continue
}
if cursor := decodeObjectSyncCursor(query.Cursor); cursor != "" && item.ID <= cursor {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return items[i].ID < items[j].ID })
limit := objectSyncLimit(query.Limit)
page := domain.ObjectSyncItemPage{Items: items}
if len(items) > limit {
page.Items = items[:limit]
page.NextCursor = encodeObjectSyncCursor(page.Items[len(page.Items)-1].ID)
}
return page, nil
}
func (store *MemoryStore) ClaimObjectSyncItem(_ context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncItemRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
now := time.Now().UTC()
for id, item := range store.objectSyncItems {
if item.Status != domain.ObjectSyncItemCommitting || (item.CoordinatorOwner != "" && item.CoordinatorLeaseUntil.After(now)) {
continue
}
item.CoordinatorOwner = owner
item.CoordinatorLeaseUntil = now.Add(ttl)
item.UpdatedAt = now.Format(time.RFC3339Nano)
store.objectSyncItems[id] = item
copy := item
return &copy, nil
}
return nil, nil
}
func (store *MemoryStore) ClaimObjectSyncCleanupItem(_ context.Context, owner string, before time.Time, ttl time.Duration) (*domain.ObjectSyncItemRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
now := time.Now().UTC()
for id, item := range store.objectSyncItems {
if item.StagingKey == "" || (item.CoordinatorOwner != "" && item.CoordinatorLeaseUntil.After(now)) {
continue
}
switch item.Status {
case domain.ObjectSyncItemUploading, domain.ObjectSyncItemUploaded, domain.ObjectSyncItemFailed, domain.ObjectSyncItemAborted, domain.ObjectSyncItemExpired, domain.ObjectSyncItemCommitted, domain.ObjectSyncItemKeptBoth:
default:
continue
}
updatedAt, err := time.Parse(time.RFC3339Nano, item.UpdatedAt)
if err != nil || updatedAt.After(before) {
continue
}
item.CoordinatorOwner = owner
item.CoordinatorLeaseUntil = now.Add(ttl)
store.objectSyncItems[id] = item
copy := item
return &copy, nil
}
return nil, nil
}
func (store *MemoryStore) AcquireObjectSyncLease(_ context.Context, requested domain.ObjectSyncLeaseRecord, ttl time.Duration) (*domain.ObjectSyncLeaseRecord, bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
now := time.Now().UTC()
item, exists := store.objectSyncLeases[requested.ResourceHash]
if exists && item.OwnerItemID != "" && item.ExpiresAtTime.After(now) && item.OwnerItemID != requested.OwnerItemID {
copy := item
return &copy, false, nil
}
requested.FencingToken = item.FencingToken + 1
requested.ID = item.ID
if requested.ID == "" {
requested.ID = domain.NewID("synclease")
}
requested.ExpiresAtTime = now.Add(ttl)
requested.ExpiresAt = requested.ExpiresAtTime.Format(time.RFC3339Nano)
requested.LastHeartbeatAt = now.Format(time.RFC3339Nano)
requested.UpdatedAt = requested.LastHeartbeatAt
store.objectSyncLeases[requested.ResourceHash] = requested
copy := requested
return &copy, true, nil
}
func (store *MemoryStore) RenewObjectSyncLease(_ context.Context, resourceHash, sessionID, itemID string, fencingToken int64, ttl time.Duration) (bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncLeases[resourceHash]
resolvedHash := resourceHash
if !ok {
for hash, candidate := range store.objectSyncLeases {
if candidate.ID == resourceHash {
item = candidate
resolvedHash = hash
ok = true
break
}
}
}
if !ok || item.OwnerSessionID != sessionID || item.OwnerItemID != itemID || item.FencingToken != fencingToken {
return false, nil
}
now := time.Now().UTC()
item.ExpiresAtTime = now.Add(ttl)
item.ExpiresAt = item.ExpiresAtTime.Format(time.RFC3339Nano)
item.LastHeartbeatAt = now.Format(time.RFC3339Nano)
item.UpdatedAt = item.LastHeartbeatAt
store.objectSyncLeases[resolvedHash] = item
return true, nil
}
func (store *MemoryStore) ReleaseObjectSyncLease(_ context.Context, resourceHash, sessionID, itemID string, fencingToken int64) error {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncLeases[resourceHash]
resolvedHash := resourceHash
if !ok {
for hash, candidate := range store.objectSyncLeases {
if candidate.ID == resourceHash {
item = candidate
resolvedHash = hash
ok = true
break
}
}
}
if !ok || item.OwnerSessionID != sessionID || item.OwnerItemID != itemID || item.FencingToken != fencingToken {
return nil
}
item.OwnerSessionID = ""
item.OwnerItemID = ""
item.ExpiresAt = ""
item.ExpiresAtTime = time.Time{}
item.UpdatedAt = nowISO()
store.objectSyncLeases[resolvedHash] = item
return nil
}
func (store *MemoryStore) ForceReleaseObjectSyncLease(_ context.Context, id string) (*domain.ObjectSyncLeaseRecord, bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
for hash, item := range store.objectSyncLeases {
if item.ID != id {
continue
}
item.OwnerSessionID = ""
item.OwnerItemID = ""
item.ExpiresAt = ""
item.ExpiresAtTime = time.Time{}
item.UpdatedAt = nowISO()
store.objectSyncLeases[hash] = item
copy := item
return &copy, true, nil
}
return nil, false, nil
}
func objectSyncHeadKey(organizationID, syncSpaceID, key string) string {
return normalizedOrganizationID(organizationID) + "|" + syncSpaceID + "|" + key
}
func (store *MemoryStore) GetObjectSyncHead(_ context.Context, organizationID, syncSpaceID, key string) (*domain.ObjectSyncObjectHeadRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncHeads[objectSyncHeadKey(organizationID, syncSpaceID, key)]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertObjectSyncHead(_ context.Context, item domain.ObjectSyncObjectHeadRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncHeads[objectSyncHeadKey(item.OrganizationID, item.SyncSpaceID, item.Key)] = item
return nil
}
func (store *MemoryStore) ListObjectSyncHeads(_ context.Context, organizationID, syncSpaceID, cursor string, limit int) (domain.ObjectSyncHeadPage, error) {
store.mu.Lock()
defer store.mu.Unlock()
decodedCursor := decodeObjectSyncCursor(cursor)
items := make([]domain.ObjectSyncObjectHeadRecord, 0)
for _, item := range store.objectSyncHeads {
if item.OrganizationID != normalizedOrganizationID(organizationID) || item.SyncSpaceID != syncSpaceID || (decodedCursor != "" && item.Key <= decodedCursor) {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return items[i].Key < items[j].Key })
page := domain.ObjectSyncHeadPage{Items: items}
pageLimit := objectSyncLimit(limit)
if len(items) > pageLimit {
page.Items = items[:pageLimit]
page.NextCursor = encodeObjectSyncCursor(page.Items[len(page.Items)-1].Key)
}
return page, nil
}
func (store *MemoryStore) InsertObjectSyncVersion(_ context.Context, item domain.ObjectSyncVersionRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
key := item.OrganizationID + "|" + item.SyncSpaceID + "|" + item.Key + "|" + strconv.FormatInt(item.PlatformVersion, 10)
store.objectSyncVersions[key] = item
return nil
}
func (store *MemoryStore) AppendObjectSyncAudit(_ context.Context, item domain.ObjectSyncAuditEvent) error {
store.mu.Lock()
defer store.mu.Unlock()
for _, existing := range store.objectSyncAuditEvents {
if existing.EventKey == item.EventKey {
return nil
}
}
store.objectSyncAuditEvents[item.ID] = item
return nil
}
func (store *MemoryStore) ListObjectSyncAudit(_ context.Context, query domain.ObjectSyncListQuery) ([]domain.ObjectSyncAuditEvent, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncAuditEvent, 0)
for _, item := range store.objectSyncAuditEvents {
if query.OrganizationID != "" && item.OrganizationID != normalizedOrganizationID(query.OrganizationID) {
continue
}
if query.SessionID != "" && item.SessionID != query.SessionID {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return items[i].OccurredAt > items[j].OccurredAt })
if len(items) > objectSyncLimit(query.Limit) {
items = items[:objectSyncLimit(query.Limit)]
}
return items, nil
}
func (store *MemoryStore) GetLatestObjectSyncAudit(_ context.Context, organizationID string) (*domain.ObjectSyncAuditEvent, error) {
items, err := store.ListObjectSyncAudit(context.Background(), domain.ObjectSyncListQuery{OrganizationID: organizationID, Limit: 1})
if err != nil || len(items) == 0 {
return nil, err
}
return &items[0], nil
}
func (store *MemoryStore) ListObjectSyncReleases(_ context.Context, organizationID string) ([]domain.ObjectSyncReleaseRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncReleaseRecord, 0)
for _, item := range store.objectSyncReleases {
if item.OrganizationID == normalizedOrganizationID(organizationID) {
items = append(items, item)
}
}
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt > items[j].CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertObjectSyncRelease(_ context.Context, item domain.ObjectSyncReleaseRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncReleases[item.ID] = item
return nil
}
func (store *MemoryStore) ClaimObjectSyncRelease(_ context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncReleaseRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
now := time.Now().UTC()
for id, item := range store.objectSyncReleases {
if item.Status != "pending" || (item.CoordinatorOwner != "" && item.CoordinatorLeaseUntil.After(now)) {
continue
}
item.CoordinatorOwner = owner
item.CoordinatorLeaseUntil = now.Add(ttl)
store.objectSyncReleases[id] = item
copy := item
return &copy, nil
}
return nil, nil
}
func objectSyncAuditCursor(item domain.ObjectSyncAuditEvent) string {
return item.OccurredAt + "\x00" + item.ID
}
func (store *MemoryStore) ListObjectSyncAuditRange(_ context.Context, organizationID, start, end, cursor string, limit int) (domain.ObjectSyncAuditPage, error) {
store.mu.Lock()
defer store.mu.Unlock()
decodedCursor := decodeObjectSyncCursor(cursor)
items := make([]domain.ObjectSyncAuditEvent, 0)
for _, item := range store.objectSyncAuditEvents {
if item.OrganizationID != normalizedOrganizationID(organizationID) || item.OccurredAt < start || item.OccurredAt >= end || (decodedCursor != "" && objectSyncAuditCursor(item) <= decodedCursor) {
continue
}
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return objectSyncAuditCursor(items[i]) < objectSyncAuditCursor(items[j]) })
pageLimit := objectSyncLimit(limit)
page := domain.ObjectSyncAuditPage{Items: items}
if len(items) > pageLimit {
page.Items = items[:pageLimit]
page.NextCursor = encodeObjectSyncCursor(objectSyncAuditCursor(page.Items[len(page.Items)-1]))
}
return page, nil
}
func (store *MemoryStore) GetObjectSyncAuditSeal(_ context.Context, organizationID, dayUTC string) (*domain.ObjectSyncAuditSealRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.objectSyncAuditSeals[normalizedOrganizationID(organizationID)+"\x00"+dayUTC]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) InsertObjectSyncAuditSeal(_ context.Context, item domain.ObjectSyncAuditSealRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
key := normalizedOrganizationID(item.OrganizationID) + "\x00" + item.DayUTC
if _, exists := store.objectSyncAuditSeals[key]; exists {
return nil
}
store.objectSyncAuditSeals[key] = item
return nil
}
func (store *MemoryStore) ListObjectSyncClientReleases(_ context.Context) ([]domain.ObjectSyncClientReleaseRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ObjectSyncClientReleaseRecord, 0, len(store.objectSyncClientReleases))
for _, item := range store.objectSyncClientReleases {
items = append(items, item)
}
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt > items[j].CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertObjectSyncClientRelease(_ context.Context, item domain.ObjectSyncClientReleaseRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
store.objectSyncClientReleases[item.ID] = item
return nil
}
func (store *MongoStore) ListObjectSyncSpaces(ctx context.Context, organizationID string) ([]domain.ObjectSyncSpaceRecord, error) {
cursor, err := store.objectSyncSpaces.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncSpaceRecord](ctx, cursor)
}
func mongoObjectSyncGet[T any](ctx context.Context, collection *mongo.Collection, filter bson.M) (*T, error) {
var item T
err := collection.FindOne(ctx, filter).Decode(&item)
if err == mongo.ErrNoDocuments {
return nil, nil
}
if err != nil {
return nil, err
}
return &item, nil
}
func mongoObjectSyncUpsert(ctx context.Context, collection *mongo.Collection, id string, item any) error {
_, err := collection.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": item}, options.Update().SetUpsert(true))
return err
}
func (store *MongoStore) GetObjectSyncSpace(ctx context.Context, id string) (*domain.ObjectSyncSpaceRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncSpaceRecord](ctx, store.objectSyncSpaces, bson.M{"id": id})
}
func (store *MongoStore) UpsertObjectSyncSpace(ctx context.Context, item domain.ObjectSyncSpaceRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncSpaces, item.ID, item)
}
func (store *MongoStore) ListObjectSyncDevices(ctx context.Context, organizationID string) ([]domain.ObjectSyncDeviceRecord, error) {
cursor, err := store.objectSyncDevices.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "updatedAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncDeviceRecord](ctx, cursor)
}
func (store *MongoStore) GetObjectSyncDevice(ctx context.Context, id string) (*domain.ObjectSyncDeviceRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncDeviceRecord](ctx, store.objectSyncDevices, bson.M{"id": id})
}
func (store *MongoStore) UpsertObjectSyncDevice(ctx context.Context, item domain.ObjectSyncDeviceRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncDevices, item.ID, item)
}
func (store *MongoStore) ListObjectSyncAuthorizations(ctx context.Context, organizationID, status string) ([]domain.ObjectSyncDeviceAuthorizationRecord, error) {
filter := bson.M{}
if organizationID != "" {
// Public short-code requests are intentionally unowned until an
// administrator resolves the code. Never broadcast those records to
// the default organization through the legacy compatibility filter.
filter["organizationId"] = normalizedOrganizationID(organizationID)
}
if status != "" {
filter["status"] = status
}
cursor, err := store.objectSyncAuthorizations.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncDeviceAuthorizationRecord](ctx, cursor)
}
func (store *MongoStore) GetObjectSyncAuthorization(ctx context.Context, id string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncDeviceAuthorizationRecord](ctx, store.objectSyncAuthorizations, bson.M{"id": id})
}
func (store *MongoStore) GetObjectSyncAuthorizationByDeviceCodeHash(ctx context.Context, hash string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncDeviceAuthorizationRecord](ctx, store.objectSyncAuthorizations, bson.M{"deviceCodeHash": hash})
}
func (store *MongoStore) GetObjectSyncAuthorizationByUserCode(ctx context.Context, code string) (*domain.ObjectSyncDeviceAuthorizationRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncDeviceAuthorizationRecord](ctx, store.objectSyncAuthorizations, bson.M{"userCode": code})
}
func (store *MongoStore) UpsertObjectSyncAuthorization(ctx context.Context, item domain.ObjectSyncDeviceAuthorizationRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncAuthorizations, item.ID, item)
}
func (store *MongoStore) ListObjectSyncCredentials(ctx context.Context, organizationID string) ([]domain.ObjectSyncCredentialRecord, error) {
cursor, err := store.objectSyncCredentials.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncCredentialRecord](ctx, cursor)
}
func (store *MongoStore) GetObjectSyncCredential(ctx context.Context, id string) (*domain.ObjectSyncCredentialRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncCredentialRecord](ctx, store.objectSyncCredentials, bson.M{"id": id})
}
func (store *MongoStore) GetObjectSyncCredentialByTokenHash(ctx context.Context, hash string) (*domain.ObjectSyncCredentialRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncCredentialRecord](ctx, store.objectSyncCredentials, bson.M{"tokenHash": hash})
}
func (store *MongoStore) UpsertObjectSyncCredential(ctx context.Context, item domain.ObjectSyncCredentialRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncCredentials, item.ID, item)
}
func (store *MongoStore) ListObjectSyncSessions(ctx context.Context, query domain.ObjectSyncListQuery) (domain.ObjectSyncSessionPage, error) {
filter := bson.M{}
if query.OrganizationID != "" {
filter = orgCompatibilityFilter(query.OrganizationID)
}
if query.Status != "" {
filter = organizationScopedFilter(query.OrganizationID, bson.M{"status": query.Status})
}
if decoded := decodeObjectSyncCursor(query.Cursor); decoded != "" {
if createdAt, id, ok := strings.Cut(decoded, "\x00"); ok && createdAt != "" && id != "" {
filter["$or"] = bson.A{
bson.M{"createdAt": bson.M{"$lt": createdAt}},
bson.M{"createdAt": createdAt, "id": bson.M{"$lt": id}},
}
}
}
limit := objectSyncLimit(query.Limit)
cursor, err := store.objectSyncSessions.Find(
ctx,
filter,
options.Find().
SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}).
SetLimit(int64(limit+1)),
)
if err != nil {
return domain.ObjectSyncSessionPage{}, err
}
items, err := decodeMany[domain.ObjectSyncSessionRecord](ctx, cursor)
if err != nil {
return domain.ObjectSyncSessionPage{}, err
}
page := domain.ObjectSyncSessionPage{Items: items}
if len(items) > limit {
page.Items = items[:limit]
page.NextCursor = encodeObjectSyncCursor(objectSyncSessionCursor(page.Items[len(page.Items)-1]))
}
return page, nil
}
func (store *MongoStore) GetObjectSyncSession(ctx context.Context, id string) (*domain.ObjectSyncSessionRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncSessionRecord](ctx, store.objectSyncSessions, bson.M{"id": id})
}
func (store *MongoStore) UpsertObjectSyncSession(ctx context.Context, item domain.ObjectSyncSessionRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncSessions, item.ID, item)
}
func (store *MongoStore) ClaimObjectSyncSession(ctx context.Context, from, to domain.ObjectSyncSessionStatus, operationID string) (*domain.ObjectSyncSessionRecord, error) {
result := store.objectSyncSessions.FindOneAndUpdate(
ctx,
bson.M{"status": string(from)},
bson.M{"$set": bson.M{"status": string(to), "operationId": operationID, "updatedAt": nowISO()}},
options.FindOneAndUpdate().SetSort(bson.D{{Key: "updatedAt", Value: 1}}).SetReturnDocument(options.After),
)
var item domain.ObjectSyncSessionRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &item, nil
}
func (store *MongoStore) GetObjectSyncItem(ctx context.Context, id string) (*domain.ObjectSyncItemRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncItemRecord](ctx, store.objectSyncItems, bson.M{"id": id})
}
func (store *MongoStore) UpsertObjectSyncItems(ctx context.Context, items []domain.ObjectSyncItemRecord) error {
if len(items) == 0 {
return nil
}
models := make([]mongo.WriteModel, 0, len(items))
for _, item := range items {
models = append(models, mongo.NewUpdateOneModel().SetFilter(bson.M{"id": item.ID}).SetUpdate(bson.M{"$set": item}).SetUpsert(true))
}
_, err := store.objectSyncItems.BulkWrite(ctx, models, options.BulkWrite().SetOrdered(false))
return err
}
func (store *MongoStore) ListObjectSyncItems(ctx context.Context, query domain.ObjectSyncListQuery) (domain.ObjectSyncItemPage, error) {
filter := bson.M{}
if query.SessionID != "" {
filter["sessionId"] = query.SessionID
}
if query.OrganizationID != "" {
filter["organizationId"] = normalizedOrganizationID(query.OrganizationID)
}
if query.Status != "" {
filter["status"] = query.Status
}
if cursor := decodeObjectSyncCursor(query.Cursor); cursor != "" {
filter["id"] = bson.M{"$gt": cursor}
}
limit := objectSyncLimit(query.Limit)
cursor, err := store.objectSyncItems.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "id", Value: 1}}).SetLimit(int64(limit+1)))
if err != nil {
return domain.ObjectSyncItemPage{}, err
}
items, err := decodeMany[domain.ObjectSyncItemRecord](ctx, cursor)
if err != nil {
return domain.ObjectSyncItemPage{}, err
}
page := domain.ObjectSyncItemPage{Items: items}
if len(items) > limit {
page.Items = items[:limit]
page.NextCursor = encodeObjectSyncCursor(page.Items[len(page.Items)-1].ID)
}
return page, nil
}
func (store *MongoStore) ClaimObjectSyncItem(ctx context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncItemRecord, error) {
now := time.Now().UTC()
result := store.objectSyncItems.FindOneAndUpdate(
ctx,
bson.M{
"status": string(domain.ObjectSyncItemCommitting),
"$or": bson.A{
bson.M{"coordinatorOwner": ""},
bson.M{"coordinatorOwner": bson.M{"$exists": false}},
bson.M{"coordinatorLeaseUntil": bson.M{"$lte": now}},
},
},
bson.M{"$set": bson.M{
"coordinatorOwner": owner,
"coordinatorLeaseUntil": now.Add(ttl),
"updatedAt": now.Format(time.RFC3339Nano),
}},
options.FindOneAndUpdate().SetSort(bson.D{{Key: "updatedAt", Value: 1}}).SetReturnDocument(options.After),
)
var item domain.ObjectSyncItemRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &item, nil
}
func (store *MongoStore) ClaimObjectSyncCleanupItem(ctx context.Context, owner string, before time.Time, ttl time.Duration) (*domain.ObjectSyncItemRecord, error) {
now := time.Now().UTC()
result := store.objectSyncItems.FindOneAndUpdate(
ctx,
bson.M{
"stagingKey": bson.M{"$ne": ""},
"status": bson.M{"$in": bson.A{
domain.ObjectSyncItemUploading,
domain.ObjectSyncItemUploaded,
domain.ObjectSyncItemFailed,
domain.ObjectSyncItemAborted,
domain.ObjectSyncItemExpired,
domain.ObjectSyncItemCommitted,
domain.ObjectSyncItemKeptBoth,
}},
"updatedAt": bson.M{"$lte": before.UTC().Format(time.RFC3339Nano)},
"$or": bson.A{
bson.M{"coordinatorOwner": ""},
bson.M{"coordinatorOwner": bson.M{"$exists": false}},
bson.M{"coordinatorLeaseUntil": bson.M{"$lte": now}},
},
},
bson.M{"$set": bson.M{
"coordinatorOwner": owner,
"coordinatorLeaseUntil": now.Add(ttl),
}},
options.FindOneAndUpdate().SetSort(bson.D{{Key: "updatedAt", Value: 1}}).SetReturnDocument(options.After),
)
var item domain.ObjectSyncItemRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &item, nil
}
func (store *MongoStore) AcquireObjectSyncLease(ctx context.Context, requested domain.ObjectSyncLeaseRecord, ttl time.Duration) (*domain.ObjectSyncLeaseRecord, bool, error) {
now := time.Now().UTC()
expiresAt := now.Add(ttl)
set := bson.M{
"id": requested.ID,
"organizationId": requested.OrganizationID,
"resourceHash": requested.ResourceHash,
"resourceType": requested.ResourceType,
"connectionId": requested.ConnectionID,
"bucket": requested.Bucket,
"keyOrPrefix": requested.KeyOrPrefix,
"ownerSessionId": requested.OwnerSessionID,
"ownerItemId": requested.OwnerItemID,
"expiresAt": expiresAt.Format(time.RFC3339Nano),
"expiresAtTime": expiresAt,
"lastHeartbeatAt": now.Format(time.RFC3339Nano),
"updatedAt": now.Format(time.RFC3339Nano),
}
result := store.objectSyncLeases.FindOneAndUpdate(
ctx,
bson.M{
"organizationId": requested.OrganizationID,
"resourceHash": requested.ResourceHash,
"$or": bson.A{
bson.M{"ownerItemId": requested.OwnerItemID},
bson.M{"ownerItemId": ""},
bson.M{"ownerItemId": bson.M{"$exists": false}},
bson.M{"expiresAtTime": bson.M{"$lte": now}},
},
},
bson.M{"$set": set, "$inc": bson.M{"fencingToken": 1}},
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After),
)
var item domain.ObjectSyncLeaseRecord
err := result.Decode(&item)
if mongo.IsDuplicateKeyError(err) || err == mongo.ErrNoDocuments {
current, getErr := mongoObjectSyncGet[domain.ObjectSyncLeaseRecord](ctx, store.objectSyncLeases, bson.M{
"organizationId": requested.OrganizationID,
"resourceHash": requested.ResourceHash,
})
return current, false, getErr
}
if err != nil {
return nil, false, err
}
return &item, true, nil
}
func (store *MongoStore) RenewObjectSyncLease(ctx context.Context, resourceHash, sessionID, itemID string, fencingToken int64, ttl time.Duration) (bool, error) {
now := time.Now().UTC()
expiresAt := now.Add(ttl)
result, err := store.objectSyncLeases.UpdateOne(ctx, bson.M{
"$or": bson.A{bson.M{"resourceHash": resourceHash}, bson.M{"id": resourceHash}},
"ownerSessionId": sessionID,
"ownerItemId": itemID,
"fencingToken": fencingToken,
}, bson.M{"$set": bson.M{
"expiresAt": expiresAt.Format(time.RFC3339Nano),
"expiresAtTime": expiresAt,
"lastHeartbeatAt": now.Format(time.RFC3339Nano),
"updatedAt": now.Format(time.RFC3339Nano),
}})
return err == nil && result.MatchedCount > 0, err
}
func (store *MongoStore) ReleaseObjectSyncLease(ctx context.Context, resourceHash, sessionID, itemID string, fencingToken int64) error {
_, err := store.objectSyncLeases.UpdateOne(ctx, bson.M{
"$or": bson.A{bson.M{"resourceHash": resourceHash}, bson.M{"id": resourceHash}},
"ownerSessionId": sessionID,
"ownerItemId": itemID,
"fencingToken": fencingToken,
}, bson.M{"$set": bson.M{
"ownerSessionId": "",
"ownerItemId": "",
"expiresAt": "",
"updatedAt": nowISO(),
}, "$unset": bson.M{"expiresAtTime": ""}})
return err
}
func (store *MongoStore) ForceReleaseObjectSyncLease(ctx context.Context, id string) (*domain.ObjectSyncLeaseRecord, bool, error) {
result := store.objectSyncLeases.FindOneAndUpdate(ctx, bson.M{"id": id}, bson.M{
"$set": bson.M{"ownerSessionId": "", "ownerItemId": "", "expiresAt": "", "updatedAt": nowISO()},
"$unset": bson.M{"expiresAtTime": ""},
}, options.FindOneAndUpdate().SetReturnDocument(options.After))
var item domain.ObjectSyncLeaseRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return &item, true, nil
}
func (store *MongoStore) GetObjectSyncHead(ctx context.Context, organizationID, syncSpaceID, key string) (*domain.ObjectSyncObjectHeadRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncObjectHeadRecord](ctx, store.objectSyncHeads, bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"syncSpaceId": syncSpaceID,
"key": key,
})
}
func (store *MongoStore) UpsertObjectSyncHead(ctx context.Context, item domain.ObjectSyncObjectHeadRecord) error {
_, err := store.objectSyncHeads.UpdateOne(ctx, bson.M{
"organizationId": item.OrganizationID,
"syncSpaceId": item.SyncSpaceID,
"key": item.Key,
}, bson.M{"$set": item}, options.Update().SetUpsert(true))
return err
}
func (store *MongoStore) ListObjectSyncHeads(ctx context.Context, organizationID, syncSpaceID, cursor string, limit int) (domain.ObjectSyncHeadPage, error) {
filter := bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"syncSpaceId": syncSpaceID,
}
if decodedCursor := decodeObjectSyncCursor(cursor); decodedCursor != "" {
filter["key"] = bson.M{"$gt": decodedCursor}
}
pageLimit := objectSyncLimit(limit)
mongoCursor, err := store.objectSyncHeads.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "key", Value: 1}}).SetLimit(int64(pageLimit+1)))
if err != nil {
return domain.ObjectSyncHeadPage{}, err
}
items, err := decodeMany[domain.ObjectSyncObjectHeadRecord](ctx, mongoCursor)
if err != nil {
return domain.ObjectSyncHeadPage{}, err
}
page := domain.ObjectSyncHeadPage{Items: items}
if len(items) > pageLimit {
page.Items = items[:pageLimit]
page.NextCursor = encodeObjectSyncCursor(page.Items[len(page.Items)-1].Key)
}
return page, nil
}
func (store *MongoStore) InsertObjectSyncVersion(ctx context.Context, item domain.ObjectSyncVersionRecord) error {
_, err := store.objectSyncVersions.InsertOne(ctx, item)
if mongo.IsDuplicateKeyError(err) {
return nil
}
return err
}
func (store *MongoStore) AppendObjectSyncAudit(ctx context.Context, item domain.ObjectSyncAuditEvent) error {
_, err := store.objectSyncAuditEvents.InsertOne(ctx, item)
if mongo.IsDuplicateKeyError(err) {
return nil
}
return err
}
func (store *MongoStore) ListObjectSyncAudit(ctx context.Context, query domain.ObjectSyncListQuery) ([]domain.ObjectSyncAuditEvent, error) {
filter := bson.M{}
if query.OrganizationID != "" {
filter["organizationId"] = normalizedOrganizationID(query.OrganizationID)
}
if query.SessionID != "" {
filter["sessionId"] = query.SessionID
}
cursor, err := store.objectSyncAuditEvents.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "occurredAt", Value: -1}}).SetLimit(int64(objectSyncLimit(query.Limit))))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncAuditEvent](ctx, cursor)
}
func (store *MongoStore) GetLatestObjectSyncAudit(ctx context.Context, organizationID string) (*domain.ObjectSyncAuditEvent, error) {
var result domain.ObjectSyncAuditEvent
err := store.objectSyncAuditEvents.FindOne(
ctx,
bson.M{"organizationId": normalizedOrganizationID(organizationID)},
options.FindOne().SetSort(bson.D{{Key: "occurredAt", Value: -1}}),
).Decode(&result)
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
}
return &result, err
}
func (store *MongoStore) ListObjectSyncAuditRange(ctx context.Context, organizationID, start, end, cursor string, limit int) (domain.ObjectSyncAuditPage, error) {
filter := bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"occurredAt": bson.M{"$gte": start, "$lt": end},
}
if decoded := decodeObjectSyncCursor(cursor); decoded != "" {
parts := strings.SplitN(decoded, "\x00", 2)
if len(parts) == 2 {
filter["$or"] = bson.A{
bson.M{"occurredAt": bson.M{"$gt": parts[0]}},
bson.M{"occurredAt": parts[0], "id": bson.M{"$gt": parts[1]}},
}
}
}
pageLimit := objectSyncLimit(limit)
mongoCursor, err := store.objectSyncAuditEvents.Find(
ctx,
filter,
options.Find().SetSort(bson.D{{Key: "occurredAt", Value: 1}, {Key: "id", Value: 1}}).SetLimit(int64(pageLimit+1)),
)
if err != nil {
return domain.ObjectSyncAuditPage{}, err
}
items, err := decodeMany[domain.ObjectSyncAuditEvent](ctx, mongoCursor)
if err != nil {
return domain.ObjectSyncAuditPage{}, err
}
page := domain.ObjectSyncAuditPage{Items: items}
if len(items) > pageLimit {
page.Items = items[:pageLimit]
page.NextCursor = encodeObjectSyncCursor(objectSyncAuditCursor(page.Items[len(page.Items)-1]))
}
return page, nil
}
func (store *MongoStore) GetObjectSyncAuditSeal(ctx context.Context, organizationID, dayUTC string) (*domain.ObjectSyncAuditSealRecord, error) {
return mongoObjectSyncGet[domain.ObjectSyncAuditSealRecord](ctx, store.objectSyncAuditSeals, bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"dayUtc": dayUTC,
})
}
func (store *MongoStore) InsertObjectSyncAuditSeal(ctx context.Context, item domain.ObjectSyncAuditSealRecord) error {
_, err := store.objectSyncAuditSeals.InsertOne(ctx, item)
if mongo.IsDuplicateKeyError(err) {
return nil
}
return err
}
func (store *MongoStore) ListObjectSyncReleases(ctx context.Context, organizationID string) ([]domain.ObjectSyncReleaseRecord, error) {
cursor, err := store.objectSyncReleases.Find(ctx, orgCompatibilityFilter(organizationID), options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncReleaseRecord](ctx, cursor)
}
func (store *MongoStore) UpsertObjectSyncRelease(ctx context.Context, item domain.ObjectSyncReleaseRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncReleases, item.ID, item)
}
func (store *MongoStore) ClaimObjectSyncRelease(ctx context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncReleaseRecord, error) {
now := time.Now().UTC()
result := store.objectSyncReleases.FindOneAndUpdate(
ctx,
bson.M{
"status": "pending",
"$or": bson.A{
bson.M{"coordinatorOwner": ""},
bson.M{"coordinatorOwner": bson.M{"$exists": false}},
bson.M{"coordinatorLeaseUntil": bson.M{"$lte": now}},
},
},
bson.M{"$set": bson.M{
"coordinatorOwner": owner,
"coordinatorLeaseUntil": now.Add(ttl),
}},
options.FindOneAndUpdate().SetSort(bson.D{{Key: "createdAt", Value: 1}}).SetReturnDocument(options.After),
)
var item domain.ObjectSyncReleaseRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &item, nil
}
func (store *MongoStore) ListObjectSyncClientReleases(ctx context.Context) ([]domain.ObjectSyncClientReleaseRecord, error) {
cursor, err := store.objectSyncClientReleases.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
if err != nil {
return nil, err
}
return decodeMany[domain.ObjectSyncClientReleaseRecord](ctx, cursor)
}
func (store *MongoStore) UpsertObjectSyncClientRelease(ctx context.Context, item domain.ObjectSyncClientReleaseRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncClientReleases, item.ID, item)
}
func (store *MongoStore) UpsertObjectSyncReconcile(ctx context.Context, item domain.ObjectSyncReconcileRecord) error {
return mongoObjectSyncUpsert(ctx, store.objectSyncReconciles, item.ID, item)
}
func (store *MongoStore) ClaimObjectSyncReconcile(ctx context.Context, owner string, ttl time.Duration) (*domain.ObjectSyncReconcileRecord, error) {
now := time.Now().UTC()
result := store.objectSyncReconciles.FindOneAndUpdate(
ctx,
bson.M{
"status": "pending",
"$or": bson.A{
bson.M{"coordinatorOwner": ""},
bson.M{"coordinatorOwner": bson.M{"$exists": false}},
bson.M{"coordinatorLeaseUntil": bson.M{"$lte": now}},
},
},
bson.M{"$set": bson.M{
"status": "running",
"updatedAt": now.Format(time.RFC3339Nano),
"coordinatorOwner": owner,
"coordinatorLeaseUntil": now.Add(ttl),
}},
options.FindOneAndUpdate().SetSort(bson.D{{Key: "createdAt", Value: 1}}).SetReturnDocument(options.After),
)
var item domain.ObjectSyncReconcileRecord
if err := result.Decode(&item); err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
return &item, nil
}
func (store *MongoStore) BeginObjectSyncIdempotency(ctx context.Context, requested domain.ObjectSyncIdempotencyRecord) (*domain.ObjectSyncIdempotencyRecord, bool, error) {
if _, err := store.objectSyncIdempotency.InsertOne(ctx, requested); err == nil {
copy := requested
return &copy, true, nil
} else if !mongo.IsDuplicateKeyError(err) {
return nil, false, err
}
returned, err := mongoObjectSyncGet[domain.ObjectSyncIdempotencyRecord](ctx, store.objectSyncIdempotency, bson.M{"id": requested.ID})
return returned, false, err
}
func (store *MongoStore) ConsumeObjectSyncRateLimit(ctx context.Context, requested domain.ObjectSyncRateLimitRecord, limit int64) (domain.ObjectSyncRateLimitRecord, bool, error) {
var result domain.ObjectSyncRateLimitRecord
err := store.objectSyncRateLimits.FindOneAndUpdate(
ctx,
bson.M{"id": requested.ID},
bson.M{
"$inc": bson.M{"count": 1},
"$setOnInsert": bson.M{
"id": requested.ID,
"subject": requested.Subject,
"window": requested.Window,
"expiresAt": requested.ExpiresAt,
},
},
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After),
).Decode(&result)
if err != nil {
return domain.ObjectSyncRateLimitRecord{}, false, err
}
return result, limit <= 0 || result.Count <= limit, nil
}
func (store *MongoStore) CompleteObjectSyncIdempotency(ctx context.Context, item domain.ObjectSyncIdempotencyRecord) error {
result, err := store.objectSyncIdempotency.ReplaceOne(ctx, bson.M{
"id": item.ID,
"requestDigest": item.RequestDigest,
}, item)
if err != nil {
return err
}
if result.MatchedCount == 0 {
return mongo.ErrNoDocuments
}
return nil
}
func (store *MongoStore) DeleteObjectSyncIdempotency(ctx context.Context, id, requestDigest string) error {
_, err := store.objectSyncIdempotency.DeleteOne(ctx, bson.M{"id": id, "requestDigest": requestDigest})
return err
}
func objectSyncStringIn(items []string, target string) bool {
for _, item := range items {
if strings.TrimSpace(item) == target {
return true
}
}
return false
}