Skip to content

Commit 942fb88

Browse files
committed
concurrent map iteration
validator remove print
1 parent 5d78e5b commit 942fb88

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/db/types/map.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,42 @@ func (m *Map[K, V]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
170170
}
171171
return "TEXT"
172172
}
173+
174+
// Iterate returns a channel to iterate over all key-value pairs in the map.
175+
func (m Map[K, V]) Iterate() <-chan struct {
176+
Key K
177+
Value V
178+
} {
179+
out := make(chan struct {
180+
Key K
181+
Value V
182+
})
183+
184+
// Iterate synchronously over shards and keys
185+
go func() {
186+
defer close(out)
187+
for _, shard := range m.shards {
188+
shard.RLock()
189+
for key, value := range shard.items {
190+
out <- struct {
191+
Key K
192+
Value V
193+
}{Key: key, Value: value}
194+
}
195+
shard.RUnlock()
196+
}
197+
}()
198+
199+
return out
200+
}
201+
202+
// Range iterates over all key-value pairs and applies the callback function to each pair.
203+
func (m Map[K, V]) Range(f func(key K, value V)) {
204+
for _, shard := range m.shards {
205+
shard.RLock()
206+
for key, value := range shard.items {
207+
f(key, value)
208+
}
209+
shard.RUnlock()
210+
}
211+
}

lib/validation/validators.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,6 @@ func alphaValidator(match []string, value *generic.Value) error {
839839
if v == "" || v == "<nil>" {
840840
return nil
841841
}
842-
fmt.Println("alpha:", v)
843842
for _, r := range v {
844843
if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == ' ') {
845844
return fmt.Errorf("is not alpha")

0 commit comments

Comments
 (0)