github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/meta_cache.go (about)

     1  package client
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
     7  )
     8  
     9  // Magical timestamps for communicating with server
    10  const (
    11  	StrongTimestamp     uint64 = 0
    12  	EventuallyTimestamp uint64 = 1
    13  	BoundedTimestamp    uint64 = 2
    14  )
    15  
    16  type collInfo struct {
    17  	ID               int64          // collection id
    18  	Name             string         // collection name
    19  	Schema           *entity.Schema // collection schema, with fields schema and primary key definition
    20  	ConsistencyLevel entity.ConsistencyLevel
    21  }
    22  
    23  var MetaCache = metaCache{
    24  	sessionTsMap: make(map[string]uint64),
    25  	collInfoMap:  make(map[string]collInfo),
    26  }
    27  
    28  // timestampMap collects the last-write-timestamp of every collection, which is required by session consistency level.
    29  type metaCache struct {
    30  	sessionMu    sync.RWMutex
    31  	colMu        sync.RWMutex
    32  	sessionTsMap map[string]uint64 // collectionName -> last-write-timestamp
    33  	collInfoMap  map[string]collInfo
    34  }
    35  
    36  func (m *metaCache) getSessionTs(cName string) (uint64, bool) {
    37  	m.sessionMu.RLock()
    38  	defer m.sessionMu.RUnlock()
    39  	ts, ok := m.sessionTsMap[cName]
    40  	return ts, ok
    41  }
    42  
    43  func (m *metaCache) setSessionTs(cName string, ts uint64) {
    44  	m.sessionMu.Lock()
    45  	defer m.sessionMu.Unlock()
    46  	m.sessionTsMap[cName] = max(m.sessionTsMap[cName], ts) // increase monotonically
    47  }
    48  
    49  func (m *metaCache) setCollectionInfo(cName string, c *collInfo) {
    50  	m.colMu.Lock()
    51  	defer m.colMu.Unlock()
    52  	if c == nil {
    53  		delete(m.collInfoMap, cName)
    54  	} else {
    55  		m.collInfoMap[cName] = *c
    56  	}
    57  }
    58  
    59  func (m *metaCache) getCollectionInfo(cName string) (*collInfo, bool) {
    60  	m.colMu.RLock()
    61  	defer m.colMu.RUnlock()
    62  	col, ok := m.collInfoMap[cName]
    63  	if !ok {
    64  		return nil, false
    65  	}
    66  	return &collInfo{
    67  		ID:               col.ID,
    68  		Name:             col.Name,
    69  		Schema:           col.Schema,
    70  		ConsistencyLevel: col.ConsistencyLevel,
    71  	}, true
    72  }
    73  
    74  func (m *metaCache) reset() {
    75  	m.colMu.Lock()
    76  	defer m.colMu.Unlock()
    77  	m.collInfoMap = make(map[string]collInfo)
    78  }
    79  
    80  func max(x, y uint64) uint64 {
    81  	if x > y {
    82  		return x
    83  	}
    84  	return y
    85  }