diff --git a/cmd/cache/cache.go b/cmd/cache/cache.go new file mode 100644 index 0000000..ac6e59b --- /dev/null +++ b/cmd/cache/cache.go @@ -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 +} diff --git a/cmd/root/root.go b/cmd/root/root.go index 5d64f91..2f97af9 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -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{} @@ -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 } diff --git a/pkg/cache.go b/pkg/cache.go index fb8b351..7132868 100644 --- a/pkg/cache.go +++ b/pkg/cache.go @@ -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)) diff --git a/pkg/graph.go b/pkg/graph.go index 04a971f..cc1cf6e 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -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 } diff --git a/pkg/mockGraph.go b/pkg/mockGraph.go index cb78a40..589eeda 100644 --- a/pkg/mockGraph.go +++ b/pkg/mockGraph.go @@ -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 } diff --git a/pkg/redis_storage.go b/pkg/redis_storage.go index 973d69c..79a7331 100644 --- a/pkg/redis_storage.go +++ b/pkg/redis_storage.go @@ -77,6 +77,9 @@ 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() } @@ -84,6 +87,9 @@ func (r *RedisStorage[T]) SaveCache(cache *NodeCache) error { 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