Skip to content

Commit

Permalink
Simplify the API
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Jun 13, 2023
1 parent e1906bc commit 0ecb22d
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 183 deletions.
44 changes: 2 additions & 42 deletions blob.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package blobs

import (
"bytes"
"context"
"io"
"time"

Expand Down Expand Up @@ -44,55 +42,17 @@ func (blob *Blob) CreatedAt() (time.Time, error) {
return time.Unix(int64(decodeUInt64(data)), 0), err
}

// Returns the content of the blob as a byte slice.
func (blob *Blob) Content(ctx context.Context) ([]byte, error) {
var b bytes.Buffer
var buf = make([]byte, blob.chunkSize*blob.chunksPerTransaction)
r, err := blob.Reader()

if err != nil {
return b.Bytes(), err
}

for {
err := ctx.Err()
if err != nil {
return b.Bytes(), err
}

n, err := r.Read(buf)

if n > 0 {
_, err := b.Write(buf[:n])
if err != nil {
return b.Bytes(), err
}
}

if err == io.EOF {
return b.Bytes(), nil
}

if err != nil {
return b.Bytes(), err
}

}
}

// Returns a new reader for the content of the blob.
//
// New chunks are fetched on demand based on the chunk size and number of chunks
// per transaction configured for the store.
func (blob *Blob) Reader() (io.Reader, error) {
_, err := blob.CreatedAt()

func (blob *Blob) Reader() io.Reader {
reader := &reader{
db: blob.db,
dir: blob.dir,
chunkSize: blob.chunkSize,
chunksPerTransaction: blob.chunksPerTransaction,
}

return reader, err
return reader
}
19 changes: 1 addition & 18 deletions blob_example_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
package blobs

import (
"context"
"fmt"
"io"
"log"
)

func ExampleBlob_Content() {
ctx := context.Background()
blob := createTestBlob()

content, err := blob.Content(ctx)
if err != nil {
log.Fatal("Could not get blob content")
}

fmt.Printf("Blob content: %s", content)
// Output: Blob content: My blob content
}

func ExampleBlob_CreatedAt() {
blob := createTestBlob()

Expand Down Expand Up @@ -47,10 +33,7 @@ func ExampleBlob_Len() {
func ExampleBlob_Reader() {
blob := createTestBlob()

r, err := blob.Reader()
if err != nil {
log.Fatal("Could not get blob reader")
}
r := blob.Reader()

lr := io.LimitReader(r, 10)

Expand Down
7 changes: 2 additions & 5 deletions blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package blobs

import (
"bytes"
"context"
"crypto/rand"
"testing"
"time"
Expand All @@ -21,8 +20,7 @@ func TestLen(t *testing.T) {
_, err := rand.Read(input)
assert.NoError(t, err)

ctx := context.Background()
blob, err := s.Create(ctx, bytes.NewReader(input))
blob, err := s.Create(bytes.NewReader(input))
assert.NoError(t, err)

want := length
Expand All @@ -42,8 +40,7 @@ func TestCreatedAt(t *testing.T) {
_, err := rand.Read(input)
assert.NoError(t, err)

ctx := context.Background()
blob, err := s.Create(ctx, bytes.NewReader(input))
blob, err := s.Create(bytes.NewReader(input))
assert.NoError(t, err)

createdAt, err := blob.CreatedAt()
Expand Down
18 changes: 7 additions & 11 deletions options_example_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package blobs

import (
"context"
"fmt"
"io"
"log"
"strings"
"time"
)

func ExampleWithChunkSize() {
ctx := context.Background()
db := fdbConnect()

store, err := NewStore(db, testNamespace(), WithChunkSize(256))
if err != nil {
log.Fatalln("Could not create store")
}

blob, err := store.Create(ctx, strings.NewReader("Blob content"))
blob, err := store.Create(strings.NewReader("Blob content"))
if err != nil {
log.Fatal("Could not create blob")
}

content, err := blob.Content(ctx)
content, err := io.ReadAll(blob.Reader())
if err != nil {
log.Fatal("Could not read blob content")
}
Expand All @@ -32,20 +31,19 @@ func ExampleWithChunkSize() {
}

func ExampleWithChunksPerTransaction() {
ctx := context.Background()
db := fdbConnect()

store, err := NewStore(db, testNamespace(), WithChunksPerTransaction(10))
if err != nil {
log.Fatalln("Could not create store")
}

blob, err := store.Create(ctx, strings.NewReader("Blob content"))
blob, err := store.Create(strings.NewReader("Blob content"))
if err != nil {
log.Fatal("Could not create blob")
}

content, err := blob.Content(ctx)
content, err := io.ReadAll(blob.Reader())
if err != nil {
log.Fatal("Could not read blob content")
}
Expand All @@ -55,7 +53,6 @@ func ExampleWithChunksPerTransaction() {
}

func ExampleWithSystemTime() {
ctx := context.Background()
db := fdbConnect()

now, _ := time.Parse(time.RFC3339, "2023-01-01T00:00:00Z")
Expand All @@ -66,7 +63,7 @@ func ExampleWithSystemTime() {
log.Fatalln("Could not create store")
}

blob, err := store.Create(ctx, strings.NewReader("Blob content"))
blob, err := store.Create(strings.NewReader("Blob content"))
if err != nil {
log.Fatal("Could not create blob")
}
Expand All @@ -81,7 +78,6 @@ func ExampleWithSystemTime() {
}

func ExampleWithIdGenerator() {
ctx := context.Background()
db := fdbConnect()

idGenerator := &TestIdgenerator{}
Expand All @@ -91,7 +87,7 @@ func ExampleWithIdGenerator() {
}

for i := 0; i < 3; i++ {
blob, err := store.Create(ctx, strings.NewReader("Blob content"))
blob, err := store.Create(strings.NewReader("Blob content"))
if err != nil {
log.Fatal("Could not create blob")
}
Expand Down
4 changes: 4 additions & 0 deletions removed.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (store *Store) RemoveBlob(id Id) error {
removedPath := append(store.removedDir.GetPath(), string(id))
dst, err := blobDir.MoveTo(tr, removedPath)

if err != nil {
return err
}

unixTimestamp := store.systemTime.Now().Unix()
tr.Set(dst.Sub("deletedAt"), encodeUInt64(uint64(unixTimestamp)))

Expand Down
15 changes: 6 additions & 9 deletions removed_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package blobs

import (
"context"
"fmt"
"io"
"strings"
"testing"
"time"
Expand All @@ -14,9 +14,8 @@ func TestRemoveBlob(t *testing.T) {
store := createTestStore(WithChunkSize(100))

t.Run("can't retrieve blob after it is removed", func(t *testing.T) {
ctx := context.Background()

blob, err := store.Create(ctx, strings.NewReader("blob"))
blob, err := store.Create(strings.NewReader("blob"))
assert.NoError(t, err)
err = store.RemoveBlob(blob.Id())
assert.NoError(t, err)
Expand All @@ -27,14 +26,13 @@ func TestRemoveBlob(t *testing.T) {
})

t.Run("already retrieved blobs are accessible", func(t *testing.T) {
ctx := context.Background()

blob, err := store.Create(ctx, strings.NewReader("blob"))
blob, err := store.Create(strings.NewReader("blob"))
assert.NoError(t, err)
err = store.RemoveBlob(blob.Id())
assert.NoError(t, err)

data, err := blob.Content(ctx)
data, err := io.ReadAll(blob.Reader())
assert.NoError(t, err)

assert.Equal(t, "blob", string(data))
Expand All @@ -52,19 +50,18 @@ func TestDeleteRemovedBlobsBefore(t *testing.T) {
)

t.Run("Test that old uploads can be cleaned", func(t *testing.T) {
ctx := context.Background()

st.Time = date.AddDate(0, -2, 0)
for i := 0; i < 5; i++ {
blob, err := store.Create(ctx, strings.NewReader("content"))
blob, err := store.Create(strings.NewReader("content"))
assert.NoError(t, err)
err = store.RemoveBlob(blob.Id())
assert.NoError(t, err)
}

st.Time = date
for i := 0; i < 5; i++ {
blob, err := store.Create(ctx, strings.NewReader("content"))
blob, err := store.Create(strings.NewReader("content"))
assert.NoError(t, err)
err = store.RemoveBlob(blob.Id())
assert.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package blobs

import (
"context"
"fmt"
"io"

Expand Down Expand Up @@ -106,8 +105,8 @@ func (store *Store) Blob(id Id) (*Blob, error) {
}

// Creates and returns a new blob with the content of the given reader r.
func (store *Store) Create(ctx context.Context, r io.Reader) (*Blob, error) {
token, err := store.Upload(ctx, r)
func (store *Store) Create(r io.Reader) (*Blob, error) {
token, err := store.Upload(r)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0ecb22d

Please sign in to comment.