QueryAttributes queries the database for attributes based on the provided filter.
(_ context.Context, tenantID string, filter *base.AttributeFilter, _ string, pagination database.CursorPagination)
| 199 | |
| 200 | // QueryAttributes queries the database for attributes based on the provided filter. |
| 201 | func (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 | } |
nothing calls this directly
no test coverage detected