Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
716aa867
提交
716aa867
authored
10月 18, 2016
作者:
Jeromy Johnson
提交者:
GitHub
10月 18, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3310 from ipfs/kevina/cid2dskey
ds-help: add helper functions to convert from Cid to DsKey and the reverse
上级
62fcf7e9
c4fbe348
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
20 行增加
和
12 行删除
+20
-12
blockstore.go
blocks/blockstore/blockstore.go
+6
-11
blockstore_test.go
blocks/blockstore/blockstore_test.go
+1
-1
key.go
thirdparty/ds-help/key.go
+13
-0
没有找到文件。
blocks/blockstore/blockstore.go
浏览文件 @
716aa867
...
...
@@ -87,7 +87,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
return
nil
,
ErrNotFound
}
maybeData
,
err
:=
bs
.
datastore
.
Get
(
dshelp
.
NewKeyFromBinary
(
k
.
KeyString
()
))
maybeData
,
err
:=
bs
.
datastore
.
Get
(
dshelp
.
CidToDsKey
(
k
))
if
err
==
ds
.
ErrNotFound
{
return
nil
,
ErrNotFound
}
...
...
@@ -112,7 +112,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
}
func
(
bs
*
blockstore
)
Put
(
block
blocks
.
Block
)
error
{
k
:=
dshelp
.
NewKeyFromBinary
(
block
.
Cid
()
.
KeyString
())
k
:=
dshelp
.
CidToDsKey
(
block
.
Cid
())
// Has is cheaper than Put, so see if we already have it
exists
,
err
:=
bs
.
datastore
.
Has
(
k
)
...
...
@@ -128,7 +128,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
return
err
}
for
_
,
b
:=
range
blocks
{
k
:=
dshelp
.
NewKeyFromBinary
(
b
.
Cid
()
.
KeyString
())
k
:=
dshelp
.
CidToDsKey
(
b
.
Cid
())
exists
,
err
:=
bs
.
datastore
.
Has
(
k
)
if
err
==
nil
&&
exists
{
continue
...
...
@@ -143,11 +143,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
}
func
(
bs
*
blockstore
)
Has
(
k
*
cid
.
Cid
)
(
bool
,
error
)
{
return
bs
.
datastore
.
Has
(
dshelp
.
NewKeyFromBinary
(
k
.
KeyString
()
))
return
bs
.
datastore
.
Has
(
dshelp
.
CidToDsKey
(
k
))
}
func
(
s
*
blockstore
)
DeleteBlock
(
k
*
cid
.
Cid
)
error
{
return
s
.
datastore
.
Delete
(
dshelp
.
NewKeyFromBinary
(
k
.
KeyString
()
))
return
s
.
datastore
.
Delete
(
dshelp
.
CidToDsKey
(
k
))
}
// AllKeysChan runs a query for keys from the blockstore.
...
...
@@ -180,17 +180,12 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
}
// need to convert to key.Key using key.KeyFromDsKey.
kb
,
err
:=
dshelp
.
BinaryFromDsKey
(
ds
.
NewKey
(
e
.
Key
))
// TODO: calling NewKey isnt free
c
,
err
:=
dshelp
.
DsKeyToCid
(
ds
.
NewKey
(
e
.
Key
))
// TODO: calling NewKey isnt free
if
err
!=
nil
{
log
.
Warningf
(
"error parsing key from DsKey: "
,
err
)
return
nil
,
true
}
c
,
err
:=
cid
.
Cast
(
kb
)
if
err
!=
nil
{
log
.
Warning
(
"error parsing cid from decoded DsKey: "
,
err
)
return
nil
,
true
}
log
.
Debug
(
"blockstore: query got key"
,
c
)
return
c
,
true
...
...
blocks/blockstore/blockstore_test.go
浏览文件 @
716aa867
...
...
@@ -190,7 +190,7 @@ func TestValueTypeMismatch(t *testing.T) {
block
:=
blocks
.
NewBlock
([]
byte
(
"some data"
))
datastore
:=
ds
.
NewMapDatastore
()
k
:=
BlockPrefix
.
Child
(
dshelp
.
NewKeyFromBinary
(
block
.
Cid
()
.
KeyString
()))
k
:=
BlockPrefix
.
Child
(
dshelp
.
CidToDsKey
(
block
.
Cid
()))
datastore
.
Put
(
k
,
"data that isn't a block!"
)
blockstore
:=
NewBlockstore
(
ds_sync
.
MutexWrap
(
datastore
))
...
...
thirdparty/ds-help/key.go
浏览文件 @
716aa867
...
...
@@ -3,6 +3,7 @@ package dshelp
import
(
base32
"gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32"
ds
"gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
cid
"gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
)
// TODO: put this code into the go-datastore itself
...
...
@@ -13,3 +14,15 @@ func NewKeyFromBinary(s string) ds.Key {
func
BinaryFromDsKey
(
k
ds
.
Key
)
([]
byte
,
error
)
{
return
base32
.
RawStdEncoding
.
DecodeString
(
k
.
String
()[
1
:
])
}
func
CidToDsKey
(
k
*
cid
.
Cid
)
ds
.
Key
{
return
NewKeyFromBinary
(
k
.
KeyString
())
}
func
DsKeyToCid
(
dsKey
ds
.
Key
)
(
*
cid
.
Cid
,
error
)
{
kb
,
err
:=
BinaryFromDsKey
(
dsKey
)
if
err
!=
nil
{
return
nil
,
err
}
return
cid
.
Cast
(
kb
)
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论