Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache into the cmd #14

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cmd/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cache

import (
"fmt"

"github.com/bit-bom/bitbom/pkg"
"github.com/spf13/cobra"
)

type options struct{}

func (o *options) AddFlags(_ *cobra.Command) {}

func (o *options) Run(_ *cobra.Command, _ []string) error {
// Get the storage instance (assuming a function GetStorageInstance exists)
storage := pkg.GetStorageInstance("localhost:6379")

if err := pkg.Cache(storage); err != nil {
return fmt.Errorf("failed to cache: %w", err)
}

fmt.Println("Finished Caching")
return nil
}

func New() *cobra.Command {
o := &options{}
cmd := &cobra.Command{
Use: "cache",
Short: "Cache all nodes",
RunE: o.Run,
DisableAutoGenTag: true,
}
o.AddFlags(cmd)

return cmd
}
4 changes: 3 additions & 1 deletion cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package root

import (
"github.com/spf13/cobra"
"github.com/bit-bom/bitbom/cmd/allKeys"
"github.com/bit-bom/bitbom/cmd/cache"
"github.com/bit-bom/bitbom/cmd/ingest"
"github.com/bit-bom/bitbom/cmd/query"
"github.com/spf13/cobra"
)

type options struct{}
Expand All @@ -26,6 +27,7 @@ func New() *cobra.Command {
cmd.AddCommand(query.New())
cmd.AddCommand(ingest.New())
cmd.AddCommand(allKeys.New())
cmd.AddCommand(cache.New())

return cmd
}
9 changes: 2 additions & 7 deletions pkg/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ import (
"github.com/RoaringBitmap/roaring"
)

type stackElm struct {
id uint32
todoIndex int
}

func Cache[T any](storage Storage[T]) error {

uncachedNodes, err := storage.ToBeCached()
if err != nil {
return err
}

keys, err := storage.GetAllKeys()
if err != nil {
return fmt.Errorf("error getting keys")
return fmt.Errorf("error getting keys: %w", err)
}

childSCC, err := findCycles(storage, ChildDirection, len(keys))
Expand Down
7 changes: 0 additions & 7 deletions pkg/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ func (n *Node[T]) SetDependency(storage Storage[T], neighbor *Node[T]) error {
if err := storage.SaveNode(neighbor); err != nil {
return fmt.Errorf("failed to save neighbor node: %w", err)
}
if err := storage.AddNodeToCachedStack(n.Id); err != nil {
return err
}
if err := storage.AddNodeToCachedStack(neighbor.Id); err != nil {
return err
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/mockGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (m *MockStorage[T]) SaveCache(cache *NodeCache) error {
m.cache = map[uint32]*NodeCache{}
}
m.cache[cache.nodeID] = cache
if err := m.AddNodeToCachedStack(cache.nodeID); err != nil {
return err
}
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/redis_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ func (r *RedisStorage[T]) SaveCache(cache *NodeCache) error {
if err != nil {
return err
}
if err := r.AddNodeToCachedStack(cache.nodeID); err != nil {
return err
}
return r.client.Set(context.Background(), fmt.Sprintf("cacheHelper:%d", cache.nodeID), data, 0).Err()
}

// ToBeCached returns all nodes that haven't been cached
func (r *RedisStorage[T]) ToBeCached() ([]uint32, error) {
data, err := r.client.Get(context.Background(), "toBeCached").Result()
if err != nil {
if strings.Contains(err.Error(), "redis: nil") {
return []uint32{}, nil
}
return nil, err
}
var toBeCached []uint32
Expand Down
Loading