github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/ptwindow/pool.go (about)

     1  package ptwindow
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/GuanceCloud/cliutils/pkg/hash"
     7  )
     8  
     9  type WindowPool struct {
    10  	sync.RWMutex
    11  	pool map[[3]uint64]*PtWindow
    12  }
    13  
    14  func (m *WindowPool) Register(before, after int, k, v []string) {
    15  	m.Lock()
    16  	defer m.Unlock()
    17  
    18  	key := [3]uint64{
    19  		hash.Fnv1aHash(k),
    20  		hash.Fnv1aHash(v),
    21  		uint64(len(k)),
    22  	}
    23  
    24  	if m.pool == nil {
    25  		m.pool = make(map[[3]uint64]*PtWindow)
    26  	}
    27  
    28  	if _, ok := m.pool[key]; !ok {
    29  		m.pool[key] = NewWindow(before, after)
    30  	}
    31  }
    32  
    33  func (m *WindowPool) Get(k, v []string) (*PtWindow, bool) {
    34  	m.RLock()
    35  	defer m.RUnlock()
    36  
    37  	key := [3]uint64{
    38  		hash.Fnv1aHash(k),
    39  		hash.Fnv1aHash(v),
    40  		uint64(len(k)),
    41  	}
    42  	w, ok := m.pool[key]
    43  	return w, ok
    44  }
    45  
    46  func (m *WindowPool) Deprecated() {
    47  	m.Lock()
    48  	defer m.Unlock()
    49  	for _, v := range m.pool {
    50  		v.deprecated()
    51  	}
    52  }
    53  
    54  func NewManager() *WindowPool {
    55  	return &WindowPool{
    56  		pool: make(map[[3]uint64]*PtWindow),
    57  	}
    58  }