ListSchemas - List all Schemas
(_ context.Context, tenantID string, pagination database.Pagination)
| 137 | |
| 138 | // ListSchemas - List all Schemas |
| 139 | func (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 | } |
nothing calls this directly
no test coverage detected