Skip to content

Commit 3273398

Browse files
committed
TreeSchemaCacheClient RWMutex for SchemaIndex
1 parent 991892b commit 3273398

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pkg/tree/tree_schema_cache_client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tree
33
import (
44
"context"
55
"strings"
6+
"sync"
67

78
"github.com/sdcio/data-server/pkg/cache"
89
SchemaClient "github.com/sdcio/data-server/pkg/datastore/clients/schema"
@@ -25,10 +26,11 @@ type TreeSchemaCacheClient interface {
2526
}
2627

2728
type TreeSchemaCacheClientImpl struct {
28-
cc cache.Client
29-
scb SchemaClient.SchemaClientBound
30-
schemaIndex map[string]*sdcpb.GetSchemaResponse
31-
datastore string
29+
cc cache.Client
30+
scb SchemaClient.SchemaClientBound
31+
schemaIndex map[string]*sdcpb.GetSchemaResponse
32+
schemaIndexMutex sync.RWMutex
33+
datastore string
3234
}
3335

3436
func NewTreeSchemaCacheClient(datastore string, cc cache.Client, scb SchemaClient.SchemaClientBound) *TreeSchemaCacheClientImpl {
@@ -65,8 +67,10 @@ func (c *TreeSchemaCacheClientImpl) ToPath(ctx context.Context, path []string) (
6567
p.Elem = append(p.Elem, newPathElem)
6668
// prepare key lookup in index
6769
keylessPathSlice = append(keylessPathSlice, path[i])
70+
c.schemaIndexMutex.RLock()
6871
// lookup the key in the schema index
6972
schema, exists := c.schemaIndex[strings.Join(keylessPathSlice, PATHSEP)]
73+
c.schemaIndexMutex.RUnlock()
7074
// if it does not exist
7175
if !exists {
7276
// retrieve the schema
@@ -110,8 +114,10 @@ func (c *TreeSchemaCacheClientImpl) retrieveSchema(ctx context.Context, p *sdcpb
110114
keylessPathSlice := utils.ToStrings(p, false, true)
111115
keylessPath := strings.Join(keylessPathSlice, PATHSEP)
112116

117+
c.schemaIndexMutex.Lock()
113118
// store the schema in the lookup index
114119
c.schemaIndex[keylessPath] = schemaRsp
120+
c.schemaIndexMutex.Unlock()
115121
return schemaRsp, nil
116122
}
117123

0 commit comments

Comments
 (0)