forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit c8bde64 Author: Islam Aliev <aliev.islam@gmail.com> Date: Sat Oct 14 00:26:22 2023 +0200 feat: Make queries utilise secondary indexes (sourcenetwork#1925) ## Relevant issue(s) Resolves sourcenetwork#1555 ## Description With this change the the secondary indexes are utilised during querying data. A dedicated `Indexer` fetcher is implemented to perform fetching of values of indexed fields. Now there is a separate `filter` package that houses a lot of methods for working with filters. A new metric `indexesFetched` is introduced into `@explain` to provide information about how many indexes has been fetched. It also includes an update to the testing framework to allow adding custom asserters. The new ExplainResultsAsserter is used with this new feature.
- Loading branch information
Showing
68 changed files
with
4,218 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package datastore | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
ds "github.com/ipfs/go-datastore" | ||
|
||
"github.com/ipfs/go-datastore/query" | ||
) | ||
|
||
// DeserializePrefix deserializes all elements with the given prefix from the given storage. | ||
// It returns the keys and their corresponding elements. | ||
func DeserializePrefix[T any]( | ||
ctx context.Context, | ||
prefix string, | ||
store ds.Read, | ||
) ([]string, []T, error) { | ||
q, err := store.Query(ctx, query.Query{Prefix: prefix}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
keys := make([]string, 0) | ||
elements := make([]T, 0) | ||
for res := range q.Next() { | ||
if res.Error != nil { | ||
_ = q.Close() | ||
return nil, nil, res.Error | ||
} | ||
|
||
var element T | ||
err = json.Unmarshal(res.Value, &element) | ||
if err != nil { | ||
_ = q.Close() | ||
return nil, nil, NewErrInvalidStoredValue(err) | ||
} | ||
keys = append(keys, res.Key) | ||
elements = append(elements, element) | ||
} | ||
if err := q.Close(); err != nil { | ||
return nil, nil, err | ||
} | ||
return keys, elements, nil | ||
} | ||
|
||
// FetchKeysForPrefix fetches all keys with the given prefix from the given storage. | ||
func FetchKeysForPrefix( | ||
ctx context.Context, | ||
prefix string, | ||
store ds.Read, | ||
) ([]ds.Key, error) { | ||
q, err := store.Query(ctx, query.Query{Prefix: prefix}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keys := make([]ds.Key, 0) | ||
for res := range q.Next() { | ||
if res.Error != nil { | ||
_ = q.Close() | ||
return nil, res.Error | ||
} | ||
keys = append(keys, ds.NewKey(res.Key)) | ||
} | ||
if err = q.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return keys, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.