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

Method QueryAttributes

internal/storage/memory/data_reader.go:201–271  ·  view source on GitHub ↗

QueryAttributes queries the database for attributes based on the provided filter.

(_ context.Context, tenantID string, filter *base.AttributeFilter, _ string, pagination database.CursorPagination)

Source from the content-addressed store, hash-verified

199
200// QueryAttributes queries the database for attributes based on the provided filter.
201func (r *DataReader) QueryAttributes(_ context.Context, tenantID string, filter *base.AttributeFilter, _ string, pagination database.CursorPagination) (iterator *database.AttributeIterator, err error) {
202 txn := r.database.DB.Txn(false)
203 defer txn.Abort()
204
205 var lowerBound string
206
207 if pagination.Cursor() != "" {
208 var t database.ContinuousToken
209 t, err = utils.EncodedContinuousToken{Value: pagination.Cursor()}.Decode()
210 if err != nil {
211 return nil, err
212 }
213 lowerBound = t.(utils.ContinuousToken).Value
214 }
215
216 // Get the index and arguments based on the filter.
217 index, args := utils.GetAttributesIndexNameAndArgsByFilters(tenantID, filter)
218
219 // Get the result iterator based on the index and arguments.
220 var result memdb.ResultIterator
221 result, err = txn.Get(constants.AttributesTable, index, args...) // Query attributes table
222 if err != nil {
223 return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
224 }
225
226 // Filter the result iterator and add the attributes to the collection.
227 attr := make([]storage.Attribute, 0, 10)
228 fit := memdb.NewFilterIterator(result, utils.FilterAttributesQuery(tenantID, filter))
229 for obj := fit.Next(); obj != nil; obj = fit.Next() {
230 t, ok := obj.(storage.Attribute)
231 if !ok {
232 return nil, errors.New(base.ErrorCode_ERROR_CODE_TYPE_CONVERSATION.String())
233 }
234 attr = append(attr, t)
235 }
236
237 // Sort attributes based on the provided order field
238 sort.Slice(attr, func(i, j int) bool {
239 switch pagination.Sort() {
240 case "entity_id":
241 return attr[i].EntityID < attr[j].EntityID
242 default:
243 return false // No sorting if order field is invalid
244 }
245 })
246
247 var attrs []*base.Attribute
248 count := uint32(0)
249 limit := pagination.Limit()
250
251 for _, t := range attr {
252 // Skip attributes below the lower bound
253 switch pagination.Sort() {
254 case "entity_id":
255 if t.EntityID < lowerBound {
256 continue
257 }
258 }

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 11

FilterAttributesQueryFunction · 0.92
NewAttributeCollectionFunction · 0.92
CursorMethod · 0.80
SortMethod · 0.80
ToAttributeMethod · 0.80
DecodeMethod · 0.65
GetMethod · 0.65
StringMethod · 0.65
LimitMethod · 0.45

Tested by

no test coverage detected