Fix concurrency

This commit is contained in:
ferreo 2024-07-29 23:17:11 +01:00
parent 7c93f8f5fa
commit d02c6ae6ce
2 changed files with 32 additions and 7 deletions

View File

@ -11,7 +11,6 @@ import (
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
@ -22,7 +21,6 @@ func InitGit() error {
_, err := git.PlainClone(config.Configs.GitCache, false, &git.CloneOptions{
URL: config.Configs.Buildrepo,
SingleBranch: true,
ReferenceName: plumbing.ReferenceName(config.Configs.Buildbranch),
Depth: 1,
Auth: &http.BasicAuth{
Password: config.Configs.GitToken,
@ -53,7 +51,6 @@ func ResetAndPull() error {
Username: config.Configs.GitUser,
},
RemoteName: "origin",
ReferenceName: plumbing.ReferenceName(config.Configs.Buildbranch),
})
return err
}

View File

@ -3,6 +3,7 @@ package fastmap
import (
"fmt"
"strings"
"sync"
"slices"
@ -10,6 +11,7 @@ import (
)
type Fastmap[K comparable, V any] struct {
mu sync.RWMutex
idx map[K]int
store []fastmapValue[K, V]
}
@ -27,6 +29,8 @@ func New[K comparable, V any]() *Fastmap[K, V] {
}
func (m *Fastmap[K, V]) Set(key K, value V) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.idx[key]; ok {
m.store[m.idx[key]].Value = value
return
@ -36,6 +40,8 @@ func (m *Fastmap[K, V]) Set(key K, value V) {
}
func (m *Fastmap[K, V]) Get(key K) (value V, ok bool) {
m.mu.RLock()
defer m.mu.RUnlock()
idx, ok := m.idx[key]
if !ok {
return
@ -44,6 +50,8 @@ func (m *Fastmap[K, V]) Get(key K) (value V, ok bool) {
}
func (m *Fastmap[K, V]) Delete(key K) {
m.mu.Lock()
defer m.mu.Unlock()
idx, ok := m.idx[key]
if !ok {
return
@ -54,15 +62,21 @@ func (m *Fastmap[K, V]) Delete(key K) {
}
func (m *Fastmap[K, V]) Has(key K) bool {
m.mu.RLock()
defer m.mu.RUnlock()
_, ok := m.idx[key]
return ok
}
func (m *Fastmap[K, V]) Len() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.idx)
}
func (m *Fastmap[K, V]) GetPage(pageNum int, pageSize int) *Fastmap[K, V] {
m.mu.RLock()
defer m.mu.RUnlock()
start := pageSize * pageNum
end := start + pageSize
if end > len(m.store) {
@ -77,11 +91,15 @@ func (m *Fastmap[K, V]) GetPage(pageNum int, pageSize int) *Fastmap[K, V] {
}
func (m *Fastmap[K, V]) Clear() {
m.mu.Lock()
defer m.mu.Unlock()
m.idx = make(map[K]int)
m.store = make([]fastmapValue[K, V], 0)
}
func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, v := range m.store {
if !fn(v.Key, v.Value) {
break
@ -90,6 +108,8 @@ func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) {
}
func (m *Fastmap[K, V]) StableSortByKey() {
m.mu.Lock()
defer m.mu.Unlock()
slices.SortStableFunc(m.store, func(a, b fastmapValue[K, V]) int {
aKey := fmt.Sprint(a.Key)
bKey := fmt.Sprint(b.Key)
@ -103,6 +123,8 @@ func (m *Fastmap[K, V]) StableSortByKey() {
}
func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var builder strings.Builder
for _, v := range m.store {
builder.WriteString(fmt.Sprintf("%v:%v\n", v.Key, v.Value))
@ -111,6 +133,8 @@ func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
}
func (m *Fastmap[K, V]) UnmarshalText(text []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
m.Clear()
lines := strings.Split(string(text), "\n")
for _, line := range lines {
@ -135,6 +159,8 @@ func (m *Fastmap[K, V]) UnmarshalText(text []byte) error {
}
func (m *Fastmap[K, V]) MarshalJSON() ([]byte, error) {
m.mu.RLock()
defer m.mu.RUnlock()
temp := make(map[K]V)
for _, v := range m.store {
temp[v.Key] = v.Value
@ -143,6 +169,8 @@ func (m *Fastmap[K, V]) MarshalJSON() ([]byte, error) {
}
func (m *Fastmap[K, V]) UnmarshalJSON(data []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
temp := make(map[K]V)
if err := json.Unmarshal(data, &temp); err != nil {
return err