This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcollection.go
More file actions
60 lines (49 loc) · 1.81 KB
/
collection.go
File metadata and controls
60 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright 2024 Hypermode Inc.
* Licensed under the terms of the Apache License, Version 2.0
* See the LICENSE file that accompanied this code for further details.
*
* SPDX-FileCopyrightText: 2024 Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package collections
import (
"fmt"
"github.com/hypermodeinc/modus/runtime/collections/index/interfaces"
"github.com/puzpuzpuz/xsync/v3"
)
type collection struct {
collectionNamespaceMap *xsync.MapOf[string, interfaces.CollectionNamespace]
}
func newCollection() *collection {
return &collection{
collectionNamespaceMap: xsync.NewMapOf[string, interfaces.CollectionNamespace](),
}
}
func (c *collection) getCollectionNamespaceMap() map[string]interfaces.CollectionNamespace {
m := make(map[string]interfaces.CollectionNamespace, c.collectionNamespaceMap.Size())
c.collectionNamespaceMap.Range(func(key string, value interfaces.CollectionNamespace) bool {
m[key] = value
return true
})
return m
}
func (c *collection) findNamespace(namespace string) (interfaces.CollectionNamespace, error) {
ns, found := c.collectionNamespaceMap.Load(namespace)
if !found {
return nil, errNamespaceNotFound
}
return ns, nil
}
func (c *collection) findOrCreateNamespace(namespace string, index interfaces.CollectionNamespace) (interfaces.CollectionNamespace, error) {
result, _ := c.collectionNamespaceMap.LoadOrStore(namespace, index)
return result, nil // TODO: remove unused error
}
func (c *collection) createCollectionNamespace(namespace string, index interfaces.CollectionNamespace) (interfaces.CollectionNamespace, error) {
_, found := c.collectionNamespaceMap.Load(namespace)
if found {
return nil, fmt.Errorf("namespace with name %s already exists", namespace)
}
c.collectionNamespaceMap.Store(namespace, index)
return index, nil
}