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

Method ListSchemas

internal/storage/memory/schema_reader.go:139–195  ·  view source on GitHub ↗

ListSchemas - List all Schemas

(_ context.Context, tenantID string, pagination database.Pagination)

Source from the content-addressed store, hash-verified

137
138// ListSchemas - List all Schemas
139func (r *SchemaReader) ListSchemas(_ context.Context, tenantID string, pagination database.Pagination) (schemas []*base.SchemaList, ct database.EncodedContinuousToken, err error) {
140 txn := r.database.DB.Txn(false)
141 defer txn.Abort()
142
143 var result memdb.ResultIterator
144 result, err = txn.Get(constants.SchemaDefinitionsTable, "tenant", tenantID)
145 if err != nil {
146 return nil, nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
147 }
148 distinctVersions := make(map[string]bool)
149 filterFunc := func(schemaRaw interface{}) bool {
150 schema := schemaRaw.(storage.SchemaDefinition)
151 _, ok := distinctVersions[schema.Version]
152 if !ok {
153 distinctVersions[schema.Version] = true
154 return false
155 }
156 return true
157 }
158 filtered := memdb.NewFilterIterator(result, filterFunc)
159
160 startPage := false
161 var lowerBound string
162 schemas = make([]*base.SchemaList, 0, pagination.PageSize()+1)
163
164 if pagination.Token() != "" {
165 var t database.ContinuousToken
166 t, err = utils.EncodedContinuousToken{Value: pagination.Token()}.Decode()
167 if err != nil {
168 return nil, nil, err
169 }
170 lowerBound = t.(utils.ContinuousToken).Value
171 }
172
173 for obj := filtered.Next(); obj != nil; obj = filtered.Next() {
174 s, ok := obj.(storage.SchemaDefinition)
175 if !ok {
176 return nil, nil, errors.New(base.ErrorCode_ERROR_CODE_TYPE_CONVERSATION.String())
177 }
178 if s.Version == lowerBound {
179 startPage = true
180 }
181 if pagination.Token() == "" || startPage {
182 id, err := xid.FromString(s.Version)
183 if err != nil {
184 return nil, nil, errors.New(base.ErrorCode_ERROR_CODE_INTERNAL.String())
185 }
186 createdAt := id.Time().String()
187 schemas = append(schemas, &base.SchemaList{Version: s.Version, CreatedAt: createdAt})
188 }
189 if len(schemas) > int(pagination.PageSize()) {
190 return schemas[:pagination.PageSize()], utils.NewContinuousToken(s.Version).Encode(), nil
191 }
192 }
193
194 return schemas, database.NewNoopContinuousToken().Encode(), err
195}

Callers

nothing calls this directly

Implementers 7

NoopSchemaReaderinternal/storage/storage.go
SchemaReaderinternal/storage/proxies/cache/schema_
SchemaReaderinternal/storage/proxies/circuitbreake
SchemaReaderinternal/storage/proxies/singleflight/
SchemaReaderinternal/storage/postgres/schema_reade
SchemaReaderinternal/storage/memory/schema_reader.
mockSchemaReaderinternal/engines/lookup_test.go

Calls 8

NewContinuousTokenFunction · 0.92
NewNoopContinuousTokenFunction · 0.92
PageSizeMethod · 0.80
TokenMethod · 0.80
GetMethod · 0.65
StringMethod · 0.65
DecodeMethod · 0.65
EncodeMethod · 0.65

Tested by

no test coverage detected