5
5
package fastly
6
6
7
7
import (
8
- "sync"
9
-
10
8
"github.com/fastly/compute-sdk-go/internal/abi/prim"
11
9
)
12
10
@@ -28,26 +26,14 @@ func fastlyConfigStoreOpen(
28
26
// ConfigStore represents a Fastly config store a collection of read-only
29
27
// key/value pairs. For convenience, keys are modeled as Go strings, and values
30
28
// as byte slices.
31
- //
32
- // NOTE: wasm, by definition, is a single-threaded execution environment. This
33
- // allows us to use valueBuf scratch space between the guest and host to avoid
34
- // allocations any larger than necessary, without locking.
35
29
type ConfigStore struct {
36
30
h configstoreHandle
37
-
38
- mu sync.Mutex // protects valueBuf
39
- valueBuf [configstoreMaxValueLen ]byte
40
31
}
41
32
42
- // Dictionaries are subject to very specific limitations: 255 character keys and 8000 character values, utf-8 encoded.
43
- // The current storage collation limits utf-8 representations to 3 bytes in length .
33
+ // Config Stores are limited to keys of length 255 character. By default, values are limited to 8000 character values,
34
+ // but this can be adjust on a per-customer basis .
44
35
// https://docs.fastly.com/en/guides/about-edge-dictionaries#limitations-and-considerations
45
- // https://dev.mysql.com/doc/refman/8.4/en/charset-unicode-utf8mb3.html
46
- // https://en.wikipedia.org/wiki/UTF-8#Encoding
47
- const (
48
- configstoreMaxKeyLen = 255 * 3 // known maximum size for config store keys: 755 bytes, for 255 3-byte utf-8 encoded characters
49
- configstoreMaxValueLen = 8000 * 3 // known maximum size for config store values: 24,000 bytes, for 8000 3-byte utf-8 encoded characters
50
- )
36
+ const configstoreMaxKeyLen = 255 * 3 // known maximum size for config store keys: 755 bytes, for 255 3-byte utf-8 encoded characters
51
37
52
38
// OpenConfigStore returns a reference to the named config store, if it exists.
53
39
func OpenConfigStore (name string ) (* ConfigStore , error ) {
@@ -85,39 +71,33 @@ func fastlyConfigStoreGet(
85
71
nWritten prim.Pointer [prim.Usize ],
86
72
) FastlyStatus
87
73
88
- // Get the value for key, if it exists. The returned slice's backing array is
89
- // shared between multiple calls to getBytesUnlocked .
90
- func (c * ConfigStore ) getBytesUnlocked (key string ) ([]byte , error ) {
74
+ // GetBytes returns a slice of newly-allocated memory for the value
75
+ // corresponding to key .
76
+ func (c * ConfigStore ) GetBytes (key string ) ([]byte , error ) {
91
77
keyBuffer := prim .NewReadBufferFromString (key )
92
78
if keyBuffer .Len () > configstoreMaxKeyLen {
93
79
return nil , FastlyStatusInval .toError ()
94
80
}
95
- buf := prim .NewWriteBufferFromBytes (c .valueBuf [:]) // fresh slice of backing array
96
81
keyStr := keyBuffer .Wstring ()
97
- status := fastlyConfigStoreGet (
98
- c .h ,
99
- keyStr .Data , keyStr .Len ,
100
- prim .ToPointer (buf .Char8Pointer ()), buf .Cap (),
101
- prim .ToPointer (buf .NPointer ()),
102
- )
103
- if err := status .toError (); err != nil {
104
- return nil , err
105
- }
106
- return buf .AsBytes (), nil
107
- }
108
82
109
- // GetBytes returns a slice of newly-allocated memory for the value
110
- // corresponding to key.
111
- func (c * ConfigStore ) GetBytes (key string ) ([]byte , error ) {
112
- c .mu .Lock ()
113
- defer c .mu .Unlock ()
114
- v , err := c .getBytesUnlocked (key )
115
- if err != nil {
116
- return nil , err
83
+ n := DefaultSmallBufLen
84
+ for {
85
+ buf := prim .NewWriteBuffer (n )
86
+ status := fastlyConfigStoreGet (
87
+ c .h ,
88
+ keyStr .Data , keyStr .Len ,
89
+ prim .ToPointer (buf .Char8Pointer ()), buf .Cap (),
90
+ prim .ToPointer (buf .NPointer ()),
91
+ )
92
+ if status == FastlyStatusBufLen && buf .NValue () > 0 {
93
+ n = int (buf .NValue ())
94
+ continue
95
+ }
96
+ if err := status .toError (); err != nil {
97
+ return nil , err
98
+ }
99
+ return buf .AsBytes (), nil
117
100
}
118
- p := make ([]byte , len (v ))
119
- copy (p , v )
120
- return p , nil
121
101
}
122
102
123
103
// Has returns true if key is found.
0 commit comments