Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Sep 11, 2024
1 parent 1b24a43 commit b7632c8
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 94 deletions.
2 changes: 2 additions & 0 deletions internal/core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ func NewViewCacheKey(rootID uint32, itemID uint) ViewCacheKey {
}

func (k ViewCacheKey) ToString() string {
return k.PrettyPrint()
return string(k.Bytes())

Check failure on line 565 in internal/core/key.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unreachable: unreachable code (govet)
}

Expand All @@ -569,6 +570,7 @@ func (k ViewCacheKey) Bytes() []byte {

if k.CollectionRootID != 0 {
result = append(result, '/')
//result = append(result, []byte(fmt.Sprint(k.CollectionRootID))...)
result = encoding.EncodeUvarintAscending(result, uint64(k.CollectionRootID))
}

Expand Down
4 changes: 3 additions & 1 deletion internal/db/collection_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func (db *db) patchCollection(
// If the collection is being de-materialized - delete any cached values.
// Leaving them around will not break anything, but it would be a waste of
// storage space.
err := db.clearViewCache(ctx, col)
err := db.clearViewCache(ctx, client.CollectionDefinition{
Description: col,
})
if err != nil {
return err
}
Expand Down
102 changes: 84 additions & 18 deletions internal/db/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,29 @@ func (db *db) refreshViews(ctx context.Context, opts client.CollectionFetchOptio
return err
}

//txn := mustGetContextTxn(ctx)
//err = txn.Commit(ctx)
//if err != nil {
// return err
//}

err = db.buildViewCache(ctx, col)
if err != nil {
return err
}
}

println("*************************")
return nil
}

func (db *db) getViews(ctx context.Context, opts client.CollectionFetchOptions) ([]client.CollectionDescription, error) {
func (db *db) getViews(ctx context.Context, opts client.CollectionFetchOptions) ([]client.CollectionDefinition, error) {
cols, err := db.getCollections(ctx, opts)
if err != nil {
return nil, err
}

var views []client.CollectionDescription
var views []client.CollectionDefinition
for _, col := range cols {
if !col.Description().IsMaterialized {
continue
Expand All @@ -189,19 +196,18 @@ func (db *db) getViews(ctx context.Context, opts client.CollectionFetchOptions)
continue
}

views = append(views, col.Description())
views = append(views, col.Definition())
}

return views, nil
}

func (db *db) buildViewCache(ctx context.Context, col client.CollectionDescription) error {
func (db *db) buildViewCache(ctx context.Context, col client.CollectionDefinition) error {
//queries := col.QuerySources()

// todo:acp stuff (can execute query as node)?
// todo: need to query the view (as if it was uncached), then store the results with `ViewCacheKey`s
txn := mustGetContextTxn(ctx)
// todo: get node identity, not user
identity := GetContextIdentity(ctx)

p := planner.New(ctx, identity, db.acp, db, txn)
Expand All @@ -211,24 +217,27 @@ func (db *db) buildViewCache(ctx context.Context, col client.CollectionDescripti
instead of manually orchastrating stuff
*/
// temp disable to allow cacheless querying
col.IsMaterialized = false
col, err := description.SaveCollection(ctx, txn, col)
col.Description.IsMaterialized = false
desc, err := description.SaveCollection(ctx, txn, col.Description)
col.Description = desc
if err != nil {
return err
}
defer func() {
col.IsMaterialized = true
col, err = description.SaveCollection(ctx, txn, col)
col.Description.IsMaterialized = true
desc, err := description.SaveCollection(ctx, txn, col.Description)
col.Description = desc
if err != nil {
panic(err.Error())
}
}()

source, err := p.MakeSelectionPlan(&request.Select{
Field: request.Field{
Name: col.Name.Value(),
},
})
request, err := db.generateMaximalSelectFromCollection(ctx, col, immutable.None[string]())
if err != nil {
return err
}

source, err := p.MakeSelectionPlan(request)
if err != nil {
return err
}
Expand All @@ -250,20 +259,29 @@ func (db *db) buildViewCache(ctx context.Context, col client.CollectionDescripti
}
println("cache map: ")
println(fmt.Sprint(source.DocumentMap().IndexesByName))
println("hasNext")
println(hasNext)

var itemID uint = 1
for hasNext {
doc := source.Value()
println("fetched item:")
println(fmt.Sprint(doc.Fields))

serializedItem, err := core.MarshalViewItem(doc)
if err != nil {
return err
}
println("caching item:")
println(string(serializedItem))

// todo - serialize and store results
itemKey := core.NewViewCacheKey(col.RootID, itemID)
itemKey := core.NewViewCacheKey(col.Description.RootID, itemID)

db.multistore.Datastore().Put(ctx, itemKey.ToDS(), serializedItem)
err = txn.Datastore().Put(ctx, itemKey.ToDS(), serializedItem)
if err != nil {
return err
}

hasNext, err = source.Next()
if err != nil {
Expand Down Expand Up @@ -352,9 +370,9 @@ func (db *db) buildViewCache(ctx context.Context, col client.CollectionDescripti
return nil
}

func (db *db) clearViewCache(ctx context.Context, col client.CollectionDescription) error {
func (db *db) clearViewCache(ctx context.Context, col client.CollectionDefinition) error {
txn := mustGetContextTxn(ctx)
prefix := core.NewViewCacheColPrefix(col.RootID)
prefix := core.NewViewCacheColPrefix(col.Description.RootID)

q, err := txn.Datastore().Query(ctx, query.Query{
Prefix: prefix.ToString(),
Expand All @@ -378,3 +396,51 @@ func (db *db) clearViewCache(ctx context.Context, col client.CollectionDescripti

return q.Close()
}

func (db *db) generateMaximalSelectFromCollection(ctx context.Context, col client.CollectionDefinition, fieldName immutable.Option[string]) (*request.Select, error) {
childRequests := []request.Selection{}

for _, field := range col.GetFields() {
switch kind := field.Kind.(type) {
case *client.SchemaKind:
schemas, err := db.GetSchemas(ctx, client.SchemaFetchOptions{
Root: immutable.Some(kind.Root),
})
if err != nil {
return nil, err
}

innerSelect, err := db.generateMaximalSelectFromCollection(
ctx,
client.CollectionDefinition{
Schema: schemas[0],
},
immutable.Some(field.Name),
)
if err != nil {
return nil, err
}

childRequests = append(childRequests, innerSelect)
default:
println(fmt.Sprintf("%T", field.Kind))
}

}

var name string
if fieldName.HasValue() {
name = fieldName.Value()
} else {
name = col.GetName()
}

return &request.Select{
Field: request.Field{
Name: name,
},
ChildSelect: request.ChildSelect{
Fields: childRequests,
},
}, nil
}
2 changes: 2 additions & 0 deletions internal/planner/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (n *lensNode) toDoc(mapDoc map[string]any) (core.Doc, error) {
// we do not know about. In which case we have to skip it.
continue
}

if n.documentMapping.
// Take the last index of this name, this is in order to be consistent with other
// similar logic, for example when converting a core.Doc to a map before passing it
// into a lens transform.
Expand Down
Loading

0 comments on commit b7632c8

Please sign in to comment.