Skip to content

Commit

Permalink
concurrent map iteration
Browse files Browse the repository at this point in the history
validator remove print
  • Loading branch information
iesreza committed Jan 1, 2025
1 parent 5d78e5b commit 942fb88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/db/types/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,42 @@ func (m *Map[K, V]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}
return "TEXT"
}

// Iterate returns a channel to iterate over all key-value pairs in the map.
func (m Map[K, V]) Iterate() <-chan struct {
Key K
Value V
} {
out := make(chan struct {
Key K
Value V
})

// Iterate synchronously over shards and keys
go func() {
defer close(out)
for _, shard := range m.shards {
shard.RLock()
for key, value := range shard.items {
out <- struct {
Key K
Value V
}{Key: key, Value: value}
}
shard.RUnlock()
}
}()

return out
}

// Range iterates over all key-value pairs and applies the callback function to each pair.
func (m Map[K, V]) Range(f func(key K, value V)) {
for _, shard := range m.shards {
shard.RLock()
for key, value := range shard.items {
f(key, value)
}
shard.RUnlock()
}
}
1 change: 0 additions & 1 deletion lib/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,6 @@ func alphaValidator(match []string, value *generic.Value) error {
if v == "" || v == "<nil>" {
return nil
}
fmt.Println("alpha:", v)
for _, r := range v {
if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == ' ') {
return fmt.Errorf("is not alpha")
Expand Down

0 comments on commit 942fb88

Please sign in to comment.