Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
09aa147e
Unverified
Commit
09aa147e
authored
Nov 10, 2020
by
Nemanja Zbiljić
Committed by
GitHub
Nov 10, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add file joiner function for iterating through chunk addresses (#923)
parent
a3fdf8bc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
0 deletions
+121
-0
file.go
pkg/file/file.go
+2
-0
joiner.go
pkg/file/joiner/joiner.go
+54
-0
joiner_test.go
pkg/file/joiner/joiner_test.go
+65
-0
No files found.
pkg/file/file.go
View file @
09aa147e
...
...
@@ -20,6 +20,8 @@ type Reader interface {
// Joiner provides the inverse functionality of the Splitter.
type
Joiner
interface
{
Reader
// IterateChunkAddresses is used to iterate over chunks addresses of some root hash.
IterateChunkAddresses
(
swarm
.
AddressIterFunc
)
error
// Size returns the span of the hash trie represented by the joiner's root hash.
Size
()
int64
}
...
...
pkg/file/joiner/joiner.go
View file @
09aa147e
...
...
@@ -210,6 +210,60 @@ func (j *joiner) Seek(offset int64, whence int) (int64, error) {
}
func
(
j
*
joiner
)
IterateChunkAddresses
(
fn
swarm
.
AddressIterFunc
)
error
{
// report root address
stop
:=
fn
(
j
.
addr
)
if
stop
{
return
nil
}
var
eg
errgroup
.
Group
j
.
processChunkAddresses
(
fn
,
j
.
rootData
,
j
.
span
,
&
eg
)
return
eg
.
Wait
()
}
func
(
j
*
joiner
)
processChunkAddresses
(
fn
swarm
.
AddressIterFunc
,
data
[]
byte
,
subTrieSize
int64
,
eg
*
errgroup
.
Group
)
{
// we are at a leaf data chunk
if
subTrieSize
<=
int64
(
len
(
data
))
{
return
}
for
cursor
:=
0
;
cursor
<
len
(
data
);
cursor
+=
j
.
refLength
{
select
{
case
<-
j
.
ctx
.
Done
()
:
return
default
:
}
address
:=
swarm
.
NewAddress
(
data
[
cursor
:
cursor
+
j
.
refLength
])
stop
:=
fn
(
address
)
if
stop
{
break
}
sec
:=
subtrieSection
(
data
,
cursor
,
j
.
refLength
,
subTrieSize
)
if
sec
<=
4096
{
continue
}
func
(
address
swarm
.
Address
,
eg
*
errgroup
.
Group
)
{
eg
.
Go
(
func
()
error
{
ch
,
err
:=
j
.
getter
.
Get
(
j
.
ctx
,
storage
.
ModeGetRequest
,
address
)
if
err
!=
nil
{
return
err
}
chunkData
:=
ch
.
Data
()[
8
:
]
subtrieSpan
:=
int64
(
chunkToSpan
(
ch
.
Data
()))
j
.
processChunkAddresses
(
fn
,
chunkData
,
subtrieSpan
,
eg
)
return
nil
})
}(
address
,
eg
)
}
}
func
(
j
*
joiner
)
Size
()
int64
{
return
j
.
span
}
...
...
pkg/file/joiner/joiner_test.go
View file @
09aa147e
...
...
@@ -12,6 +12,7 @@ import (
"io"
"io/ioutil"
mrand
"math/rand"
"sync"
"testing"
"time"
...
...
@@ -764,3 +765,67 @@ func TestJoinerTwoLevelsAcrossChunk(t *testing.T) {
t
.
Fatalf
(
"last chunk expected read %d bytes; got %d"
,
42
,
c
)
}
}
func
TestJoinerIterateChunkAddresses
(
t
*
testing
.
T
)
{
store
:=
mock
.
NewStorer
()
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
// create root chunk with 2 references and the referenced data chunks
rootChunk
:=
filetest
.
GenerateTestRandomFileChunk
(
swarm
.
ZeroAddress
,
swarm
.
ChunkSize
*
2
,
swarm
.
SectionSize
*
2
)
_
,
err
:=
store
.
Put
(
ctx
,
storage
.
ModePutUpload
,
rootChunk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
firstAddress
:=
swarm
.
NewAddress
(
rootChunk
.
Data
()[
8
:
swarm
.
SectionSize
+
8
])
firstChunk
:=
filetest
.
GenerateTestRandomFileChunk
(
firstAddress
,
swarm
.
ChunkSize
,
swarm
.
ChunkSize
)
_
,
err
=
store
.
Put
(
ctx
,
storage
.
ModePutUpload
,
firstChunk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
secondAddress
:=
swarm
.
NewAddress
(
rootChunk
.
Data
()[
swarm
.
SectionSize
+
8
:
])
secondChunk
:=
filetest
.
GenerateTestRandomFileChunk
(
secondAddress
,
swarm
.
ChunkSize
,
swarm
.
ChunkSize
)
_
,
err
=
store
.
Put
(
ctx
,
storage
.
ModePutUpload
,
secondChunk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
createdAddresses
:=
[]
swarm
.
Address
{
rootChunk
.
Address
(),
firstAddress
,
secondAddress
}
j
,
_
,
err
:=
joiner
.
New
(
ctx
,
store
,
rootChunk
.
Address
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
foundAddresses
:=
make
(
map
[
string
]
struct
{})
var
foundAddressesMu
sync
.
Mutex
err
=
j
.
IterateChunkAddresses
(
func
(
addr
swarm
.
Address
)
(
stop
bool
)
{
foundAddressesMu
.
Lock
()
foundAddresses
[
addr
.
String
()]
=
struct
{}{}
foundAddressesMu
.
Unlock
()
return
false
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
createdAddresses
)
!=
len
(
foundAddresses
)
{
t
.
Fatalf
(
"expected to find %d addresses, got %d"
,
len
(
createdAddresses
),
len
(
foundAddresses
))
}
checkAddressFound
:=
func
(
t
*
testing
.
T
,
foundAddresses
map
[
string
]
struct
{},
address
swarm
.
Address
)
{
t
.
Helper
()
if
_
,
ok
:=
foundAddresses
[
address
.
String
()];
!
ok
{
t
.
Fatalf
(
"expected address %s not found"
,
address
.
String
())
}
}
for
_
,
createdAddress
:=
range
createdAddresses
{
checkAddressFound
(
t
,
foundAddresses
,
createdAddress
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment