Repository navigation
Add support for 'mantaray' manifest, and make it default #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright 2020 The Swarm Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package manifest | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/ethersphere/bee/pkg/file" | ||
| "github.com/ethersphere/bee/pkg/file/joiner" | ||
| "github.com/ethersphere/bee/pkg/file/splitter" | ||
| "github.com/ethersphere/bee/pkg/storage" | ||
| "github.com/ethersphere/bee/pkg/swarm" | ||
| "github.com/ethersphere/manifest/mantaray" | ||
| ) | ||
|
|
||
| const ( | ||
| // ManifestMantarayContentType represents content type used for noting that | ||
| // specific file should be processed as mantaray manifest. | ||
| ManifestMantarayContentType = "application/bzz-manifest-mantaray+octet-stream" | ||
| ) | ||
|
|
||
| type mantarayManifest struct { | ||
| trie *mantaray.Node | ||
|
|
||
| encrypted bool | ||
| storer storage.Storer | ||
|
|
||
| loader mantaray.LoadSaver | ||
| } | ||
|
|
||
| // NewMantarayManifest creates a new mantaray-based manifest. | ||
| func NewMantarayManifest( | ||
| encrypted bool, | ||
| storer storage.Storer, | ||
| ) (Interface, error) { | ||
| return &mantarayManifest{ | ||
| trie: mantaray.New(), | ||
| encrypted: encrypted, | ||
| storer: storer, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewMantarayManifestReference loads existing mantaray-based manifest. | ||
| func NewMantarayManifestReference( | ||
| ctx context.Context, | ||
| reference swarm.Address, | ||
| encrypted bool, | ||
| storer storage.Storer, | ||
| ) (Interface, error) { | ||
| return &mantarayManifest{ | ||
| trie: mantaray.NewNodeRef(reference.Bytes()), | ||
| encrypted: encrypted, | ||
| storer: storer, | ||
| loader: newMantarayLoader(ctx, encrypted, storer), | ||
| }, nil | ||
| } | ||
|
|
||
| func (m *mantarayManifest) Type() string { | ||
| return ManifestMantarayContentType | ||
| } | ||
|
|
||
| func (m *mantarayManifest) Add(path string, entry Entry) error { | ||
| p := []byte(path) | ||
| e := entry.Reference().Bytes() | ||
|
|
||
| return m.trie.Add(p, e, m.loader) | ||
| } | ||
|
|
||
| func (m *mantarayManifest) Remove(path string) error { | ||
| p := []byte(path) | ||
|
|
||
| err := m.trie.Remove(p, m.loader) | ||
| if err != nil { | ||
| if errors.Is(err, mantaray.ErrNotFound) { | ||
| return ErrNotFound | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (m *mantarayManifest) Lookup(path string) (Entry, error) { | ||
| p := []byte(path) | ||
|
|
||
| ref, err := m.trie.Lookup(p, m.loader) | ||
| if err != nil { | ||
| return nil, ErrNotFound | ||
| } | ||
|
|
||
| address := swarm.NewAddress(ref) | ||
|
|
||
| entry := NewEntry(address) | ||
|
|
||
| return entry, nil | ||
| } | ||
|
|
||
| func (m *mantarayManifest) Store(ctx context.Context, mode storage.ModePut) (swarm.Address, error) { | ||
|
|
||
| saver := newMantaraySaver(ctx, m.encrypted, m.storer, mode) | ||
|
|
||
| err := m.trie.Save(saver) | ||
| if err != nil { | ||
| return swarm.ZeroAddress, fmt.Errorf("manifest save error: %w", err) | ||
| } | ||
|
|
||
| address := swarm.NewAddress(m.trie.Reference()) | ||
|
|
||
| return address, nil | ||
| } | ||
|
|
||
| // mantarayLoadSaver implements required interface 'mantaray.LoadSaver' | ||
| type mantarayLoadSaver struct { | ||
| ctx context.Context | ||
| encrypted bool | ||
| storer storage.Storer | ||
| modePut storage.ModePut | ||
| } | ||
|
|
||
| func newMantarayLoader( | ||
| ctx context.Context, | ||
| encrypted bool, | ||
| storer storage.Storer, | ||
| ) *mantarayLoadSaver { | ||
| return &mantarayLoadSaver{ | ||
| ctx: ctx, | ||
| encrypted: encrypted, | ||
| storer: storer, | ||
| } | ||
| } | ||
|
|
||
| func newMantaraySaver( | ||
| ctx context.Context, | ||
| encrypted bool, | ||
| storer storage.Storer, | ||
| modePut storage.ModePut, | ||
| ) *mantarayLoadSaver { | ||
| return &mantarayLoadSaver{ | ||
| ctx: ctx, | ||
| encrypted: encrypted, | ||
| storer: storer, | ||
| modePut: modePut, | ||
| } | ||
| } | ||
|
|
||
| func (ls *mantarayLoadSaver) Load(ref []byte) ([]byte, error) { | ||
| ctx := ls.ctx | ||
|
|
||
| j := joiner.NewSimpleJoiner(ls.storer) | ||
|
|
||
| buf := bytes.NewBuffer(nil) | ||
| _, err := file.JoinReadAll(ctx, j, swarm.NewAddress(ref), buf, ls.encrypted) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return buf.Bytes(), nil | ||
| } | ||
|
|
||
| func (ls *mantarayLoadSaver) Save(data []byte) ([]byte, error) { | ||
| ctx := ls.ctx | ||
|
|
||
| sp := splitter.NewSimpleSplitter(ls.storer, ls.modePut) | ||
|
|
||
| address, err := file.SplitWriteAll(ctx, sp, bytes.NewReader(data), int64(len(data)), ls.encrypted) | ||
| if err != nil { | ||
| return swarm.ZeroAddress.Bytes(), err | ||
| } | ||
|
|
||
| return address.Bytes(), nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't really understand why this stuff is necessary. while refactoring the splitter i noticed that the dependencies to the joiner and splitter have crept into this package and i don't really understand why. this seems like an unnecessary abstraction over simple generic interfaces like
BinaryMarshaler/Unmarshaler. also i don't really understand the semantics here, fromLoadof amantarayLoaderI would expect to return amantaraytype, not a byte slice. this is just an abstraction over splitter and joiner. why?