-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the want list to work in the on demand mode
After this change the Swift code can request blobs to be downloaded and the blobs remain in the want list for a specific amount of time. This replace the previous approach of downloading all blobs right away and copies the current behaviour exhibited by go-ssb. The new code behaves slightly better as the want list isn't cleared when application exists and is persisted.
- Loading branch information
Showing
19 changed files
with
365 additions
and
30 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,126 @@ | ||
package bolt | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/boreq/errors" | ||
"github.com/planetary-social/go-ssb/service/app/commands" | ||
"github.com/planetary-social/go-ssb/service/domain/refs" | ||
"go.etcd.io/bbolt" | ||
) | ||
|
||
var bucketWantList = bucketName("want_list") | ||
|
||
type WantListRepository struct { | ||
tx *bbolt.Tx | ||
currentTimeProvider commands.CurrentTimeProvider | ||
} | ||
|
||
func NewWantListRepository( | ||
tx *bbolt.Tx, | ||
currentTimeProvider commands.CurrentTimeProvider, | ||
) *WantListRepository { | ||
return &WantListRepository{ | ||
tx: tx, | ||
currentTimeProvider: currentTimeProvider, | ||
} | ||
} | ||
|
||
func (r WantListRepository) AddToWantList(id refs.Blob, until time.Time) error { | ||
bucket, err := r.createBucket() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get the bucket") | ||
} | ||
|
||
key := r.toKey(id) | ||
|
||
value := bucket.Get(key) | ||
if value != nil { | ||
t, err := r.fromValue(value) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read the value") | ||
} | ||
|
||
if t.After(until) { | ||
return nil | ||
} | ||
} | ||
|
||
return bucket.Put(key, r.toValue(until)) | ||
} | ||
|
||
func (r WantListRepository) List() ([]refs.Blob, error) { | ||
var result []refs.Blob | ||
var toDelete []refs.Blob | ||
|
||
bucket, err := r.getBucket() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get the bucket") | ||
} | ||
|
||
if bucket == nil { | ||
return nil, nil | ||
} | ||
|
||
now := r.currentTimeProvider.Get() | ||
|
||
if err := bucket.ForEach(func(k, v []byte) error { | ||
id, err := r.fromKey(k) | ||
if err != nil { | ||
return errors.Wrap(err, "could not read the key") | ||
} | ||
|
||
until, err := r.fromValue(v) | ||
if err != nil { | ||
return errors.Wrap(err, "could not read the value") | ||
} | ||
|
||
if now.After(until) { | ||
toDelete = append(toDelete, id) | ||
return nil | ||
} | ||
|
||
result = append(result, id) | ||
return nil | ||
}); err != nil { | ||
return nil, errors.Wrap(err, "for each failed") | ||
} | ||
|
||
for _, id := range toDelete { | ||
if err := bucket.Delete(r.toKey(id)); err != nil { | ||
return nil, errors.Wrap(err, "deletion failed") | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (r WantListRepository) toKey(id refs.Blob) []byte { | ||
return []byte(id.String()) | ||
} | ||
|
||
func (r WantListRepository) fromKey(key []byte) (refs.Blob, error) { | ||
return refs.NewBlob(string(key)) | ||
} | ||
|
||
func (r WantListRepository) toValue(t time.Time) []byte { | ||
return []byte(t.Format(time.RFC3339)) | ||
} | ||
|
||
func (r WantListRepository) fromValue(v []byte) (time.Time, error) { | ||
return time.Parse(time.RFC3339, string(v)) | ||
} | ||
|
||
func (r WantListRepository) createBucket() (*bbolt.Bucket, error) { | ||
return createBucket(r.tx, r.bucketPath()) | ||
} | ||
|
||
func (r WantListRepository) getBucket() (*bbolt.Bucket, error) { | ||
return getBucket(r.tx, r.bucketPath()) | ||
} | ||
|
||
func (r WantListRepository) bucketPath() []bucketName { | ||
return []bucketName{ | ||
bucketWantList, | ||
} | ||
} |
Oops, something went wrong.