-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from fastly/dm/rename_object_store_to_kv_store
- Loading branch information
Showing
9 changed files
with
139 additions
and
94 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
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,71 @@ | ||
package kvstore | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/fastly/compute-sdk-go/internal/abi/fastly" | ||
) | ||
|
||
var ErrKeyNotFound = errors.New("kvstore: key not found") | ||
|
||
type Entry struct { | ||
io.Reader | ||
|
||
validString bool | ||
s string | ||
} | ||
|
||
func (e *Entry) String() string { | ||
if e.validString { | ||
return e.s | ||
} | ||
|
||
// TODO(dgryski): replace with StringBuilder + io.Copy ? | ||
b, err := io.ReadAll(e) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
e.s = string(b) | ||
e.validString = true | ||
return e.s | ||
} | ||
|
||
// Store represents a Fastly kv store | ||
type Store struct { | ||
kvstore *fastly.KVStore | ||
} | ||
|
||
// Open returns a handle to the named kv store | ||
func Open(name string) (*Store, error) { | ||
o, err := fastly.OpenKVStore(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Store{kvstore: o}, nil | ||
} | ||
|
||
// Lookup fetches a key from the associated kv store. If the key does not | ||
// exist, Lookup returns the sentinel error ErrKeyNotFound. | ||
func (s *Store) Lookup(key string) (*Entry, error) { | ||
val, err := s.kvstore.Lookup(key) | ||
if err != nil { | ||
|
||
// turn FastlyStatusNone into NotFound | ||
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone { | ||
return nil, ErrKeyNotFound | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return &Entry{Reader: val}, err | ||
} | ||
|
||
// Insert adds a key to the associated kv store. | ||
func (s *Store) Insert(key string, value io.Reader) error { | ||
err := s.kvstore.Insert(key, value) | ||
return err | ||
} |
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,2 @@ | ||
// Deprecated: Use the kvstore package instead. | ||
package objectstore |
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 |
---|---|---|
@@ -1,72 +1,22 @@ | ||
package objectstore | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/fastly/compute-sdk-go/internal/abi/fastly" | ||
"github.com/fastly/compute-sdk-go/kvstore" | ||
) | ||
|
||
var ErrKeyNotFound = errors.New("objectstore: key not found") | ||
|
||
type Entry struct { | ||
io.Reader | ||
|
||
validString bool | ||
s string | ||
} | ||
|
||
func (e *Entry) String() string { | ||
if e.validString { | ||
return e.s | ||
} | ||
var ErrKeyNotFound = kvstore.ErrKeyNotFound | ||
|
||
// TODO(dgryski): replace with StringBuilder + io.Copy ? | ||
b, err := io.ReadAll(e) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
e.validString = true | ||
|
||
return string(b) | ||
|
||
} | ||
// Deprecated: Use the kvstore package instead. | ||
type Entry = kvstore.Entry | ||
|
||
// Store represents a Fastly object store | ||
type Store struct { | ||
objectstore *fastly.ObjectStore | ||
} | ||
// | ||
// Deprecated: Use the kvstore package instead. | ||
type Store = kvstore.Store | ||
|
||
// Open returns a handle to the named object store | ||
// | ||
// Deprecated: Use kvstore.Open() instead. | ||
func Open(name string) (*Store, error) { | ||
o, err := fastly.OpenObjectStore(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Store{objectstore: o}, nil | ||
} | ||
|
||
// Lookup fetches a key from the associated object store. If the key does not | ||
// exist, Lookup returns the sentinel error ErrKeyNotFound. | ||
func (s *Store) Lookup(key string) (*Entry, error) { | ||
val, err := s.objectstore.Lookup(key) | ||
if err != nil { | ||
|
||
// turn FastlyStatusNone into NotFound | ||
if status, ok := fastly.IsFastlyError(err); ok && status == fastly.FastlyStatusNone { | ||
return nil, ErrKeyNotFound | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return &Entry{Reader: val}, err | ||
} | ||
|
||
// Insert adds a key to the associated object store. | ||
func (s *Store) Insert(key string, value io.Reader) error { | ||
err := s.objectstore.Insert(key, value) | ||
return err | ||
return kvstore.Open(name) | ||
} |
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,21 @@ | ||
rules: | ||
- id: deprecated-objectstore-store | ||
pattern: objectstore.Store | ||
fix: kvstore.Store | ||
message: objectstore package is deprecated | ||
languages: [go] | ||
severity: ERROR | ||
|
||
- id: deprecated-objectstore-open | ||
pattern: objectstore.Open($N) | ||
fix: kvstore.Open($N) | ||
message: objectstore package is deprecated | ||
languages: [go] | ||
severity: ERROR | ||
|
||
- id: deprecated-objectstore-errkeynotfound | ||
pattern: objectstore.ErrKeyNotFound | ||
fix: kvstore.ErrKeyNotFound | ||
message: objectstore package is deprecated | ||
languages: [go] | ||
severity: ERROR |