good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
3eff652a
Commit
3eff652a
authored
6 years ago
by
holisticode
Committed by
Anton Evangelatov
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
swarm/storage: Get all chunk references for a given file (#19002)
parent
75c9570c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swarm/storage/filestore.go
+40
-0
40 additions, 0 deletions
swarm/storage/filestore.go
swarm/storage/filestore_test.go
+36
-0
36 additions, 0 deletions
swarm/storage/filestore_test.go
with
76 additions
and
0 deletions
swarm/storage/filestore.go
+
40
−
0
View file @
3eff652a
...
...
@@ -19,6 +19,7 @@ package storage
import
(
"context"
"io"
"sort"
)
/*
...
...
@@ -96,3 +97,42 @@ func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEnc
func
(
f
*
FileStore
)
HashSize
()
int
{
return
f
.
hashFunc
()
.
Size
()
}
// Public API. This endpoint returns all chunk hashes (only) for a given file
func
(
f
*
FileStore
)
GetAllReferences
(
ctx
context
.
Context
,
data
io
.
Reader
,
toEncrypt
bool
)
(
addrs
AddressCollection
,
err
error
)
{
// create a special kind of putter, which only will store the references
putter
:=
&
HashExplorer
{
hasherStore
:
NewHasherStore
(
f
.
ChunkStore
,
f
.
hashFunc
,
toEncrypt
),
References
:
make
([]
Reference
,
0
),
}
// do the actual splitting anyway, no way around it
_
,
_
,
err
=
PyramidSplit
(
ctx
,
data
,
putter
,
putter
)
if
err
!=
nil
{
return
nil
,
err
}
// collect all references
addrs
=
NewAddressCollection
(
0
)
for
_
,
ref
:=
range
putter
.
References
{
addrs
=
append
(
addrs
,
Address
(
ref
))
}
sort
.
Sort
(
addrs
)
return
addrs
,
nil
}
// HashExplorer is a special kind of putter which will only store chunk references
type
HashExplorer
struct
{
*
hasherStore
References
[]
Reference
}
// HashExplorer's Put will add just the chunk hashes to its `References`
func
(
he
*
HashExplorer
)
Put
(
ctx
context
.
Context
,
chunkData
ChunkData
)
(
Reference
,
error
)
{
// Need to do the actual Put, which returns the references
ref
,
err
:=
he
.
hasherStore
.
Put
(
ctx
,
chunkData
)
if
err
!=
nil
{
return
nil
,
err
}
// internally store the reference
he
.
References
=
append
(
he
.
References
,
ref
)
return
ref
,
nil
}
This diff is collapsed.
Click to expand it.
swarm/storage/filestore_test.go
+
36
−
0
View file @
3eff652a
...
...
@@ -173,3 +173,39 @@ func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
t
.
Fatalf
(
"Comparison error after clearing memStore."
)
}
}
// TestGetAllReferences only tests that GetAllReferences returns an expected
// number of references for a given file
func
TestGetAllReferences
(
t
*
testing
.
T
)
{
tdb
,
cleanup
,
err
:=
newTestDbStore
(
false
,
false
)
defer
cleanup
()
if
err
!=
nil
{
t
.
Fatalf
(
"init dbStore failed: %v"
,
err
)
}
db
:=
tdb
.
LDBStore
memStore
:=
NewMemStore
(
NewDefaultStoreParams
(),
db
)
localStore
:=
&
LocalStore
{
memStore
:
memStore
,
DbStore
:
db
,
}
fileStore
:=
NewFileStore
(
localStore
,
NewFileStoreParams
())
checkRefs
:=
func
(
dataSize
int
,
expectedLen
int
)
{
slice
:=
testutil
.
RandomBytes
(
1
,
dataSize
)
addrs
,
err
:=
fileStore
.
GetAllReferences
(
context
.
Background
(),
bytes
.
NewReader
(
slice
),
false
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
addrs
)
!=
expectedLen
{
t
.
Fatalf
(
"Expected reference array length to be %d, but is %d"
,
expectedLen
,
len
(
addrs
))
}
}
// testRuns[i] and expectedLen[i] are dataSize and expected length respectively
testRuns
:=
[]
int
{
1024
,
8192
,
16000
,
30000
,
1000000
}
expectedLens
:=
[]
int
{
1
,
3
,
5
,
9
,
248
}
for
i
,
r
:=
range
testRuns
{
checkRefs
(
r
,
expectedLens
[
i
])
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment