Skip to content

Commit

Permalink
remove mutex due to absence of goroutines
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Kumar Sahu <[email protected]>
  • Loading branch information
viveksahu26 committed Jul 17, 2024
1 parent 8bb47be commit a1ebab4
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions pkg/compliance/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@ package compliance

import (
"fmt"
"sync"
)

type db struct {
mu sync.RWMutex
records map[int][]*record // store record as a value of a Map with a key as a "check_key"
ids map[string][]*record // store record as a value of a Map with a key as a "id"
keyIds map[string]map[int][]*record // store record as a value of a Map with a key as a "check_key an id"
allIds map[string]struct{} // Set of all unique ids
keyRecords map[int][]*record // store record as a value of a Map with a key as a "check_key"
idRecords map[string][]*record // store record as a value of a Map with a key as a "id"
idKeyRecords map[string]map[int][]*record // store record as a value of a Map with a key as a "check_key an id"
allIds map[string]struct{} // Set of all unique ids
}

// newDB initializes and returns a new database instance.
func newDB() *db {
return &db{
records: make(map[int][]*record),
ids: make(map[string][]*record),
keyIds: make(map[string]map[int][]*record),
allIds: make(map[string]struct{}),
keyRecords: make(map[int][]*record),
idRecords: make(map[string][]*record),
idKeyRecords: make(map[string]map[int][]*record),
allIds: make(map[string]struct{}),
}
}

// addRecord adds a single record to the database
func (d *db) addRecord(r *record) {
d.mu.Lock()
defer d.mu.Unlock()

// store record using a key
d.records[r.check_key] = append(d.records[r.check_key], r)
d.keyRecords[r.check_key] = append(d.keyRecords[r.check_key], r)

// store record using a id
d.ids[r.id] = append(d.ids[r.id], r)
if d.keyIds[r.id] == nil {
d.keyIds[r.id] = make(map[int][]*record)
d.idRecords[r.id] = append(d.idRecords[r.id], r)
if d.idKeyRecords[r.id] == nil {
d.idKeyRecords[r.id] = make(map[int][]*record)
}

// store record using a key and id
d.keyIds[r.id][r.check_key] = append(d.keyIds[r.id][r.check_key], r)
d.idKeyRecords[r.id][r.check_key] = append(d.idKeyRecords[r.id][r.check_key], r)

d.allIds[r.id] = struct{}{}
}
Expand All @@ -52,15 +47,11 @@ func (d *db) addRecords(rs []*record) {

// getRecords retrieves records by the given "check_key"
func (d *db) getRecords(key int) []*record {
d.mu.RLock()
defer d.mu.RUnlock()
return d.records[key]
return d.keyRecords[key]
}

// getAllIds retrieves all unique ids in the database
func (d *db) getAllIds() []string {
d.mu.RLock()
defer d.mu.RUnlock()
ids := make([]string, 0, len(d.allIds))
for id := range d.allIds {
ids = append(ids, id)
Expand All @@ -70,23 +61,17 @@ func (d *db) getAllIds() []string {

// getRecordsById retrieves records by the given "id"
func (d *db) getRecordsById(id string) []*record {
d.mu.RLock()
defer d.mu.RUnlock()
return d.ids[id]
return d.idRecords[id]
}

// getRecordsByKeyId retrieves records by the given "check_key" and "id"
func (d *db) getRecordsByKeyId(key int, id string) []*record {
d.mu.RLock()
defer d.mu.RUnlock()
return d.keyIds[id][key]
return d.idKeyRecords[id][key]
}

// dumpAll prints all records, optionally filtered by the given keys
func (d *db) dumpAll(keys []int) {
d.mu.RLock()
defer d.mu.RUnlock()
for _, records := range d.records {
for _, records := range d.keyRecords {
for _, r := range records {
if len(keys) == 0 {
fmt.Printf("id: %s, key: %d, value: %s\n", r.id, r.check_key, r.check_value)
Expand Down

0 comments on commit a1ebab4

Please sign in to comment.