github.com/KinWaiYuen/client-go/v2@v2.5.4/util/ts_set.go (about)

     1  // Copyright 2021 TiKV Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/util/ts_set.go
    19  //
    20  
    21  // Copyright 2021 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package util
    36  
    37  import "sync"
    38  
    39  // TSSet is a set of timestamps.
    40  type TSSet struct {
    41  	sync.RWMutex
    42  	m map[uint64]struct{}
    43  }
    44  
    45  // Put puts timestamps into the map.
    46  func (s *TSSet) Put(tss ...uint64) {
    47  	s.Lock()
    48  	defer s.Unlock()
    49  
    50  	// Lazy initialization..
    51  	// Most of the time, there is no transaction lock conflict.
    52  	// So allocate this in advance is unnecessary and bad for performance.
    53  	if s.m == nil {
    54  		s.m = make(map[uint64]struct{}, 5)
    55  	}
    56  
    57  	for _, ts := range tss {
    58  		s.m[ts] = struct{}{}
    59  	}
    60  }
    61  
    62  // GetAll returns all timestamps in the set.
    63  func (s *TSSet) GetAll() []uint64 {
    64  	s.RLock()
    65  	defer s.RUnlock()
    66  	if len(s.m) == 0 {
    67  		return nil
    68  	}
    69  	ret := make([]uint64, 0, len(s.m))
    70  	for ts := range s.m {
    71  		ret = append(ret, ts)
    72  	}
    73  	return ret
    74  }