Created
December 9, 2022 02:02
-
-
Save michaelneale/2fce24300c4505d5be4eec66201b53ca to your computer and use it in GitHub Desktop.
ChatGPT writes a golang key/value store abstraction
This file contains hidden or 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
store := &InMemoryKVStore{data: make(map[string]string)} | |
store.Set("key1", "value1") | |
value, err := store.Get("key1") | |
if err != nil { | |
// handle error | |
} | |
store.Delete("key1") | |
results, err := store.Query("value") | |
if err != nil { | |
// handle error | |
} |
This file contains hidden or 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
package kvstore | |
// KVStore is an interface that defines methods for working with a key/value store. | |
type KVStore interface { | |
// Set sets the value for a given key. | |
Set(key string, value string) error | |
// Get retrieves the value for a given key. | |
Get(key string) (string, error) | |
// Delete deletes the value for a given key. | |
Delete(key string) error | |
// Query returns all values that match a given query string. | |
Query(query string) ([]string, error) | |
} | |
// InMemoryKVStore is an implementation of the KVStore interface that uses an in-memory map to store key/value pairs. | |
type InMemoryKVStore struct { | |
data map[string]string | |
} | |
// Set sets the value for a given key. | |
func (store *InMemoryKVStore) Set(key string, value string) error { | |
store.data[key] = value | |
return nil | |
} | |
// Get retrieves the value for a given key. | |
func (store *InMemoryKVStore) Get(key string) (string, error) { | |
value, ok := store.data[key] | |
if !ok { | |
return "", fmt.Errorf("key not found: %s", key) | |
} | |
return value, nil | |
} | |
// Delete deletes the value for a given key. | |
func (store *InMemoryKVStore) Delete(key string) error { | |
delete(store.data, key) | |
return nil | |
} | |
// Query returns all values that match a given query string. | |
func (store *InMemoryKVStore) Query(query string) ([]string, error) { | |
var results []string | |
for key, value := range store.data { | |
if strings.Contains(key, query) || strings.Contains(value, query) { | |
results = append(results, value) | |
} | |
} | |
return results, nil | |
} |
This file contains hidden or 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
package kvstore | |
import ( | |
"context" | |
"fmt" | |
"strings" | |
"go.mongodb.org/mongo-driver/bson" | |
"go.mongodb.org/mongo-driver/mongo" | |
"go.mongodb.org/mongo-driver/mongo/options" | |
) | |
// MongoDBKVStore is an implementation of the KVStore interface that uses a MongoDB database to store key/value pairs. | |
type MongoDBKVStore struct { | |
client *mongo.Client | |
database string | |
collection string | |
} | |
// NewMongoDBKVStore creates a new MongoDBKVStore. | |
func NewMongoDBKVStore(client *mongo.Client, database string, collection string) *MongoDBKVStore { | |
return &MongoDBKVStore{ | |
client: client, | |
database: database, | |
collection: collection, | |
} | |
} | |
// Set sets the value for a given key. | |
func (store *MongoDBKVStore) Set(key string, value string) error { | |
_, err := store.client. | |
Database(store.database). | |
Collection(store.collection). | |
ReplaceOne(context.TODO(), bson.M{"_id": key}, bson.M{"_id": key, "value": value}, options.Replace().SetUpsert(true)) | |
return err | |
} | |
// Get retrieves the value for a given key. | |
func (store *MongoDBKVStore) Get(key string) (string, error) { | |
var result bson.M | |
err := store.client. | |
Database(store.database). | |
Collection(store.collection). | |
FindOne(context.TODO(), bson.M{"_id": key}). | |
Decode(&result) | |
if err != nil { | |
return "", fmt.Errorf("key not found: %s", key) | |
} | |
return result["value"].(string), nil | |
} | |
// Delete deletes the value for a given key. | |
func (store *MongoDBKVStore) Delete(key string) error { | |
_, err := store.client. | |
Database(store.database). | |
Collection(store.collection). | |
DeleteOne(context.TODO(), bson.M{"_id": key}) | |
return err | |
} | |
// Query returns all values that match a given query string. | |
func (store *MongoDBKVStore) Query(query string) ([]string, error) { | |
var results []string | |
cur, err := store.client. | |
Database(store.database). | |
Collection(store.collection). | |
Find(context.TODO(), bson.M{"$or": []bson.M{{"_id": bson.M{"$regex": query}}, {"value": bson.M{"$regex": query}}}}) | |
if err != nil { | |
return nil, err | |
} | |
defer cur.Close(context.TODO()) |
This file contains hidden or 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
package kvstore | |
import ( | |
"context" | |
"fmt" | |
"strings" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
) | |
// S3KVStore is an implementation of the KVStore interface that uses an Amazon S3 bucket to store key/value pairs. | |
type S3KVStore struct { | |
bucket string | |
prefix string | |
s3 *s3.S3 | |
} | |
// NewS3KVStore creates a new S3KVStore. | |
func NewS3KVStore(bucket string, prefix string) (*S3KVStore, error) { | |
sess, err := session.NewSession() | |
if err != nil { | |
return nil, err | |
} | |
store := &S3KVStore{ | |
bucket: bucket, | |
prefix: prefix, | |
s3: s3.New(sess), | |
} | |
return store, nil | |
} | |
// Set sets the value for a given key. | |
func (store *S3KVStore) Set(key string, value string) error { | |
_, err := store.s3.PutObject(&s3.PutObjectInput{ | |
Bucket: aws.String(store.bucket), | |
Key: aws.String(store.prefix + key), | |
Body: strings.NewReader(value), | |
}) | |
return err | |
} | |
// Get retrieves the value for a given key. | |
func (store *S3KVStore) Get(key string) (string, error) { | |
result, err := store.s3.GetObject(&s3.GetObjectInput{ | |
Bucket: aws.String(store.bucket), | |
Key: aws.String(store.prefix + key), | |
}) | |
if err != nil { | |
return "", fmt.Errorf("key not found: %s", key) | |
} | |
defer result.Body.Close() | |
buf := new(bytes.Buffer) | |
_, err = buf.ReadFrom(result.Body) | |
if err != nil { | |
return "", err | |
} | |
return buf.String(), nil | |
} | |
// Delete deletes the value for a given key. | |
func (store *S3KVStore) Delete(key string) error { | |
_, err := store.s3.DeleteObject(&s3.DeleteObjectInput{ | |
Bucket: aws.String(store.bucket), | |
Key: aws.String(store.prefix + key), | |
}) | |
return err | |
} | |
// Query returns all values that match a given query string. | |
func (store *S3KVStore) Query(query string) ([]string, error) { | |
var results []string | |
listObjectsInput := &s3.ListObjectsV2Input{ | |
Bucket: aws.String(store.bucket), | |
Prefix: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment