]> 127.0.0.1 Git - part/.git/commitdiff
add v0.28.0+2023072640eba56
authorqydysky <qydysky@foxmail.com>
Wed, 26 Jul 2023 01:32:56 +0000 (09:32 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 26 Jul 2023 01:32:56 +0000 (09:32 +0800)
component/Component.go
component/Component_test.go

index 4b0a43c2d857f7d596ba98af1fb5b2efb409bb30..a90ab63deb4cb5600de0f94c199859a6bdf1b844 100644 (file)
@@ -37,8 +37,9 @@ var (
 type components struct {
        MatchF func(mkey, key string) bool
 
-       m  []*CItem
-       mm map[string]int
+       withLock bool
+       m        []*CItem
+       mm       map[string]int
        sync.RWMutex
 }
 
@@ -47,16 +48,19 @@ type components struct {
 // strings.HasSuffix
 //
 // DotMatch
-func NewComp(matchf func(mkey, key string) bool) *components {
+func NewComp(withLock bool, matchf func(mkey, key string) bool) *components {
        return &components{
-               MatchF: matchf,
-               mm:     make(map[string]int),
+               MatchF:   matchf,
+               withLock: withLock,
+               mm:       make(map[string]int),
        }
 }
 
 func (t *components) Put(item *CItem) error {
-       t.Lock()
-       defer t.Unlock()
+       if t.withLock {
+               t.Lock()
+               defer t.Unlock()
+       }
        if _, ok := t.mm[item.Key]; ok {
                return errors.Join(ErrConflict, errors.New(item.Key))
        }
@@ -71,7 +75,10 @@ func (t *components) Put(item *CItem) error {
 }
 
 func (t *components) Del(key string) {
-       t.Lock()
+       if t.withLock {
+               t.Lock()
+               defer t.Unlock()
+       }
        for i := 0; i < len(t.m); i++ {
                if t.MatchF(t.m[i].Key, key) {
                        delete(t.mm, t.m[i].Key)
@@ -82,12 +89,13 @@ func (t *components) Del(key string) {
        for i := 0; i < len(t.m); i++ {
                t.mm[t.m[i].Key] = i
        }
-       t.Unlock()
 }
 
 func (t *components) Run(key string, ctx context.Context, ptr any) error {
-       t.RLock()
-       defer t.RUnlock()
+       if t.withLock {
+               t.RLock()
+               defer t.RUnlock()
+       }
 
        var (
                i   = 0
@@ -103,6 +111,7 @@ func (t *components) Run(key string, ctx context.Context, ptr any) error {
                        return errors.Join(ErrCItemErr, e)
                }
        }
+
        if !got {
                return errors.Join(ErrNotExist, errors.New(key))
        }
@@ -126,12 +135,12 @@ func Run[T any](key string, ctx context.Context, ptr *T) error {
        return Comp.Run(key, ctx, ptr)
 }
 
-func Init(f func(mkey, key string) bool) {
-       Comp = NewComp(f)
+func Init(withLock bool, f func(mkey, key string) bool) {
+       Comp = NewComp(withLock, f)
 }
 
 func DotMatch(mkey, key string) bool {
        return mkey == key || (len(mkey) > len(key) && mkey[0:len(key)] == key && mkey[len(key)] == '.')
 }
 
-var Comp *components = NewComp(DotMatch)
+var Comp *components = NewComp(false, DotMatch)
index 7216575c9ce8847fc1aa92f9874b72758fa26dbc..0976ae4eb3468a2dfb5dc899d7d01690048d59b2 100644 (file)
@@ -9,7 +9,7 @@ import (
 )
 
 func Test1(t *testing.T) {
-       Init(DotMatch)
+       Init(false, DotMatch)
 
        Put(`1`, func(ctx context.Context, ptr *int) error {
                if *ptr > 1 {
@@ -67,7 +67,7 @@ func Test1(t *testing.T) {
 }
 
 func TestDot(t *testing.T) {
-       Init(DotMatch)
+       Init(false, DotMatch)
        Put[int](`1`, func(ctx context.Context, ptr *int) error {
                if *ptr == 1 {
                        return nil
@@ -88,7 +88,7 @@ func TestDot(t *testing.T) {
 }
 
 func Test3(t *testing.T) {
-       Init(DotMatch)
+       Init(false, DotMatch)
        sumup := func(ctx context.Context, ptr *int) error {
                return nil
        }
@@ -104,7 +104,7 @@ func Test3(t *testing.T) {
 }
 
 func Benchmark2(b *testing.B) {
-       Init(DotMatch)
+       Init(false, DotMatch)
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                Put[int](strconv.Itoa(i), func(ctx context.Context, ptr *int) error {
@@ -114,9 +114,9 @@ func Benchmark2(b *testing.B) {
 }
 
 func Benchmark1(b *testing.B) {
-       Init(DotMatch)
+       Init(false, DotMatch)
        for i := 0; i < 1000; i++ {
-               Put[int](strconv.Itoa(i), func(ctx context.Context, ptr *int) error {
+               Put[int](`1`, func(ctx context.Context, ptr *int) error {
                        return nil
                })
        }