go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/internal/datalakes/inmemory/kiss.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package inmemory
     5  
     6  import "sync"
     7  
     8  // kvStore is an general-purpose abstraction for key-value stores
     9  type kvStore interface {
    10  	Get(key interface{}) (interface{}, bool)
    11  	Set(key interface{}, value interface{}, cost int64) bool
    12  	Del(key interface{})
    13  }
    14  
    15  // kissDb for synchronously data access
    16  type kissDb struct {
    17  	mu   sync.Mutex
    18  	data map[string]interface{}
    19  }
    20  
    21  func newKissDb() *kissDb {
    22  	return &kissDb{
    23  		data: map[string]interface{}{},
    24  	}
    25  }
    26  
    27  func (c *kissDb) Get(key interface{}) (interface{}, bool) {
    28  	k, ok := key.(string)
    29  	if !ok {
    30  		panic("cannot map key to string for kissDB")
    31  	}
    32  
    33  	c.mu.Lock()
    34  	res, ok := c.data[k]
    35  	c.mu.Unlock()
    36  
    37  	return res, ok
    38  }
    39  
    40  func (c *kissDb) Set(key interface{}, value interface{}, cost int64) bool {
    41  	k, ok := key.(string)
    42  	if !ok {
    43  		panic("cannot map key to string for kissDB")
    44  	}
    45  
    46  	c.mu.Lock()
    47  	c.data[k] = value
    48  	c.mu.Unlock()
    49  
    50  	return true
    51  }
    52  
    53  func (c *kissDb) Del(key interface{}) {
    54  	k, ok := key.(string)
    55  	if !ok {
    56  		panic("cannot map key to string for kissDB")
    57  	}
    58  
    59  	c.mu.Lock()
    60  	delete(c.data, k)
    61  	c.mu.Unlock()
    62  }