MCPcopy Create free account
hub / github.com/Permify/permify / HeadSnapshot

Method HeadSnapshot

internal/storage/postgres/data_reader.go:553–587  ·  view source on GitHub ↗

HeadSnapshot retrieves the latest snapshot token associated with the tenant.

(ctx context.Context, tenantID string)

Source from the content-addressed store, hash-verified

551
552// HeadSnapshot retrieves the latest snapshot token associated with the tenant.
553func (r *DataReader) HeadSnapshot(ctx context.Context, tenantID string) (token.SnapToken, error) {
554 // Start a new trace span and end it when the function exits.
555 ctx, span := internal.Tracer.Start(ctx, "data-reader.head-snapshot")
556 defer span.End()
557 // Log snapshot operation
558 slog.DebugContext(ctx, "getting head snapshot for tenant_id", slog.String("tenant_id", tenantID))
559 // Declare transaction ID and snapshot variables
560 var xid db.XID8
561 var snapshotValue string
562
563 // Build the query to find the highest transaction ID and snapshot associated with the tenant.
564 builder := r.database.Builder.Select("id", "snapshot").From(TransactionsTable).Where(squirrel.Eq{"tenant_id": tenantID}).OrderBy("id DESC").Limit(1)
565 query, args, err := builder.ToSql()
566 if err != nil {
567 return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SQL_BUILDER)
568 }
569
570 // TODO: To optimize this query, create the following index concurrently to avoid table locks:
571 // CREATE INDEX CONCURRENTLY idx_transactions_tenant_id_id ON transactions(tenant_id, id DESC);
572
573 // Execute the query and retrieve the highest transaction ID and snapshot.
574 err = r.database.ReadPool.QueryRow(ctx, query, args...).Scan(&xid, &snapshotValue)
575 if err != nil {
576 // If no rows are found, return a snapshot token with a value of 0.
577 if errors.Is(err, pgx.ErrNoRows) {
578 return snapshot.NewToken(db.XID8{Uint: 0}, ""), nil
579 }
580 return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SCAN)
581 }
582
583 slog.DebugContext(ctx, "successfully retrieved latest snapshot token")
584 // Return snapshot token
585 // Return the latest snapshot token associated with the tenant.
586 return snapshot.NewToken(xid, snapshotValue), nil
587}

Callers

nothing calls this directly

Implementers 5

NoopDataReaderinternal/storage/storage.go
DataReaderinternal/storage/proxies/circuitbreake
DataReaderinternal/storage/proxies/singleflight/
DataReaderinternal/storage/postgres/data_reader.
DataReaderinternal/storage/memory/data_reader.go

Calls 6

HandleErrorFunction · 0.92
NewTokenFunction · 0.92
StartMethod · 0.80
StringMethod · 0.65
LimitMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected