-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fast add credential type lookups (#4177)
- Loading branch information
Showing
5 changed files
with
255 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package x | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// SyncMap provides a thread-safe map with generic keys and values | ||
type SyncMap[K comparable, V any] struct { | ||
mu sync.RWMutex | ||
data map[K]V | ||
} | ||
|
||
// NewSyncMap initializes a new SyncMap instance | ||
func NewSyncMap[K comparable, V any]() *SyncMap[K, V] { | ||
return &SyncMap[K, V]{ | ||
data: make(map[K]V), | ||
} | ||
} | ||
|
||
// Load retrieves a value for a key. It returns the value and a boolean indicating if the key exists. | ||
func (m *SyncMap[K, V]) Load(key K) (V, bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
val, ok := m.data[key] | ||
return val, ok | ||
} | ||
|
||
// Store sets a value for a key, replacing any existing value. | ||
func (m *SyncMap[K, V]) Store(key K, value V) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.data[key] = value | ||
} | ||
|
||
// LoadOrStore retrieves the existing value for a key if it exists, or stores and returns a given value if it doesn't. | ||
func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if existing, ok := m.data[key]; ok { | ||
return existing, true | ||
} | ||
m.data[key] = value | ||
return value, false | ||
} | ||
|
||
// Delete removes a key-value pair from the map. | ||
func (m *SyncMap[K, V]) Delete(key K) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
delete(m.data, key) | ||
} | ||
|
||
// Range iterates over all entries in the map, calling the provided function for each key-value pair. | ||
// If the function returns false, the iteration stops. | ||
func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
for k, v := range m.data { | ||
if !f(k, v) { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package x | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSyncMapStoreAndLoad(t *testing.T) { | ||
m := NewSyncMap[int, string]() | ||
|
||
m.Store(1, "one") | ||
|
||
// Test Load for an existing key | ||
val, ok := m.Load(1) | ||
require.True(t, ok, "Expected key 1 to exist") | ||
assert.Equal(t, "one", val, "Expected value 'one' for key 1") | ||
|
||
// Test Load for a non-existing key | ||
_, ok = m.Load(2) | ||
assert.False(t, ok, "Expected key 2 to be absent") | ||
} | ||
|
||
func TestSyncMapLoadOrStore(t *testing.T) { | ||
m := NewSyncMap[int, string]() | ||
|
||
// Store a new key-value pair | ||
val, loaded := m.LoadOrStore(1, "one") | ||
require.False(t, loaded, "Expected key 1 to be newly stored") | ||
assert.Equal(t, "one", val, "Expected value 'one' for key 1 after LoadOrStore") | ||
|
||
// Attempt to store a new value for an existing key | ||
val, loaded = m.LoadOrStore(1, "uno") | ||
require.True(t, loaded, "Expected key 1 to already exist") | ||
assert.Equal(t, "one", val, "Expected existing value 'one' for key 1") | ||
} | ||
|
||
func TestSyncMapDelete(t *testing.T) { | ||
m := NewSyncMap[int, string]() | ||
|
||
m.Store(1, "one") | ||
m.Delete(1) | ||
|
||
_, ok := m.Load(1) | ||
assert.False(t, ok, "Expected key 1 to be deleted") | ||
} | ||
|
||
func TestSyncMapRange(t *testing.T) { | ||
m := NewSyncMap[int, string]() | ||
|
||
m.Store(1, "one") | ||
m.Store(2, "two") | ||
m.Store(3, "three") | ||
|
||
expected := map[int]string{ | ||
1: "one", | ||
2: "two", | ||
3: "three", | ||
} | ||
|
||
m.Range(func(key int, value string) bool { | ||
expectedVal, exists := expected[key] | ||
require.True(t, exists, "Unexpected key found in map") | ||
assert.Equal(t, expectedVal, value, "Unexpected value for key %d", key) | ||
delete(expected, key) | ||
return true | ||
}) | ||
|
||
assert.Empty(t, expected, "Not all entries were iterated over") | ||
} | ||
|
||
func TestSyncMapConcurrentAccess(t *testing.T) { | ||
m := NewSyncMap[int, int]() | ||
var wg sync.WaitGroup | ||
|
||
// Run multiple goroutines to test concurrent access | ||
for i := 0; i < 100; i++ { | ||
wg.Add(1) | ||
go func(i int) { | ||
defer wg.Done() | ||
m.Store(i, i) | ||
}(i) | ||
} | ||
|
||
wg.Wait() | ||
|
||
// Verify all stored values | ||
for i := 0; i < 100; i++ { | ||
val, ok := m.Load(i) | ||
require.True(t, ok, "Expected key %d to exist", i) | ||
assert.Equal(t, i, val, "Expected value %d for key %d", i, i) | ||
} | ||
} |