github.com/quay/claircore@v1.5.28/datastore/postgres/initialized.go (about)

     1  package postgres
     2  
     3  import (
     4  	"context"
     5  	"sync/atomic"
     6  )
     7  
     8  func (s *MatcherStore) Initialized(ctx context.Context) (bool, error) {
     9  	const query = `
    10  SELECT EXISTS(SELECT 1 FROM vuln LIMIT 1);
    11  `
    12  	ok := atomic.LoadUint32(&s.initialized) != 0
    13  	if ok {
    14  		return true, nil
    15  	}
    16  
    17  	if err := s.pool.QueryRow(ctx, query).Scan(&ok); err != nil {
    18  		return false, err
    19  	}
    20  	// There were no rows when we looked, so report that. Don't update the bool,
    21  	// because it's in the 'false' state.
    22  	if !ok {
    23  		return false, nil
    24  	}
    25  	// If this fails, it means a concurrent goroutine already swapped. Any
    26  	// subsequent calls will see the 'true' value.
    27  	atomic.CompareAndSwapUint32(&s.initialized, 0, 1)
    28  	return true, nil
    29  }