Skip to content

Commit

Permalink
remove deep copy of cache entries now passed by reference to api
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed May 15, 2024
1 parent 14c17d6 commit acc6ba6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
22 changes: 4 additions & 18 deletions internal/reporter/downstream_reporter/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
b64 "encoding/base64"
"encoding/json"
"errors"
"reflect"
"soarca/logger"
Expand Down Expand Up @@ -203,9 +202,10 @@ func (cacheReporter *Cache) GetExecutions() ([]cache_report.ExecutionEntry, erro
// NOTE: fetched via fifo register key reference as is ordered array,
// needed to test and report back ordered executions stored
for _, executionEntryKey := range cacheReporter.fifoRegister {
entry, err := cacheReporter.copyExecutionEntry(executionEntryKey)
if err != nil {
return []cache_report.ExecutionEntry{}, err
// NOTE: cached executions are passed by reference, so they must not be modified
entry, present := cacheReporter.Cache[executionEntryKey]
if present != true {
return []cache_report.ExecutionEntry{}, errors.New("internal error. cache fifo register and cache executions mismatch.")
}
executions = append(executions, entry)
}
Expand All @@ -221,17 +221,3 @@ func (cacheReporter *Cache) GetExecutionReport(executionKey uuid.UUID) (cache_re

return report, nil
}

func (cacheReporter *Cache) copyExecutionEntry(executionKeyStr string) (cache_report.ExecutionEntry, error) {
// NOTE: Deep copy via JSON serialization and de-serialization, longer computation time than custom function
// might want to implement custom deep copy in future
origJSON, err := json.Marshal(cacheReporter.Cache[executionKeyStr])
if err != nil {
return cache_report.ExecutionEntry{}, err
}
clone := cache_report.ExecutionEntry{}
if err = json.Unmarshal(origJSON, &clone); err != nil {
return cache_report.ExecutionEntry{}, err
}
return clone, nil
}
5 changes: 0 additions & 5 deletions routes/reporter/reporter_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"soarca/logger"
)

// TODOs
// Change api parsing model to actual data types and add json bson strings for formatting
// Refactor reporter parsing in getting info from cache via now using new model
// Remove copy of cache entries and just pass the objects

var log *logger.Log

type Empty struct{}
Expand Down

0 comments on commit acc6ba6

Please sign in to comment.