Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
d64ab3ce
提交
d64ab3ce
authored
7月 14, 2017
作者:
Kevin Atkinson
提交者:
Jeromy
9月 03, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use the json repr. of the minimal config as the DiskId.
License: MIT Signed-off-by:
Kevin Atkinson
<
k@kevina.org
>
上级
3bbe0651
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
71 行增加
和
74 行删除
+71
-74
config_test.go
repo/fsrepo/config_test.go
+12
-12
datastores.go
repo/fsrepo/datastores.go
+46
-20
fsrepo.go
repo/fsrepo/fsrepo.go
+13
-42
没有找到文件。
repo/fsrepo/config_test.go
浏览文件 @
d64ab3ce
...
...
@@ -87,9 +87,9 @@ func TestDefaultDatastoreConfig(t *testing.T) {
t
.
Fatal
(
err
)
}
expected
:=
"/blocks:{flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2};/:{levelds;datastore};"
if
dsc
.
Disk
Id
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Id
())
expected
:=
`{"mounts":[{"mountpoint":{"string":"/blocks"},"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"},{"mountpoint":{"string":"/"},"path":"datastore","type":"levelds"}],"type":"mount"}`
if
dsc
.
Disk
Spec
()
.
String
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Spec
()
.
String
())
}
ds
,
err
:=
dsc
.
Create
(
dir
)
...
...
@@ -125,9 +125,9 @@ func TestLevelDbConfig(t *testing.T) {
t
.
Fatal
(
err
)
}
expected
:=
"levelds;datastore"
if
dsc
.
Disk
Id
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Id
())
expected
:=
`{"path":"datastore","type":"levelds"}`
if
dsc
.
Disk
Spec
()
.
String
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Spec
()
.
String
())
}
ds
,
err
:=
dsc
.
Create
(
dir
)
...
...
@@ -163,9 +163,9 @@ func TestFlatfsConfig(t *testing.T) {
t
.
Fatal
(
err
)
}
expected
:=
"flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2"
if
dsc
.
Disk
Id
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Id
())
expected
:=
`{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}`
if
dsc
.
Disk
Spec
()
.
String
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Spec
()
.
String
())
}
ds
,
err
:=
dsc
.
Create
(
dir
)
...
...
@@ -201,9 +201,9 @@ func TestMeasureConfig(t *testing.T) {
t
.
Fatal
(
err
)
}
expected
:=
"flatfs;blocks;/repo/flatfs/shard/v1/next-to-last/2"
if
dsc
.
Disk
Id
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Id
())
expected
:=
`{"path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"}`
if
dsc
.
Disk
Spec
()
.
String
()
!=
expected
{
t
.
Errorf
(
"expected '%s' got '%s' as DiskId"
,
expected
,
dsc
.
Disk
Spec
()
.
String
())
}
ds
,
err
:=
dsc
.
Create
(
dir
)
...
...
repo/fsrepo/datastores.go
浏览文件 @
d64ab3ce
...
...
@@ -2,6 +2,7 @@ package fsrepo
import
(
"bytes"
"encoding/json"
"fmt"
"path/filepath"
...
...
@@ -18,17 +19,28 @@ import (
// ConfigFromMap creates a new datastore config from a map
type
ConfigFromMap
func
(
map
[
string
]
interface
{})
(
DatastoreConfig
,
error
)
type
DiskSpec
map
[
string
]
interface
{}
type
DatastoreConfig
interface
{
// DiskId is a unique id representing the Datastore config as
// stored on disk, runtime config values are not part of this Id.
// Returns an empty string if the datastore does not have an on
// disk representation. No length limit.
DiskId
()
string
// DiskSpec returns a minimal configuration of the datastore
// represting what is stored on disk. Run time values are
// excluded.
DiskSpec
()
DiskSpec
// Create instantiate a new datastore from this config
Create
(
path
string
)
(
repo
.
Datastore
,
error
)
}
func
(
spec
DiskSpec
)
String
()
string
{
b
,
err
:=
json
.
Marshal
(
spec
)
if
err
!=
nil
{
// should not happen
panic
(
err
)
}
b
=
bytes
.
TrimSpace
(
b
)
return
string
(
b
)
}
var
datastores
map
[
string
]
ConfigFromMap
func
init
()
{
...
...
@@ -94,12 +106,19 @@ func MountDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error
return
&
res
,
nil
}
func
(
c
*
mountDatastoreConfig
)
DiskId
()
string
{
buf
:=
new
(
bytes
.
Buffer
)
for
_
,
m
:=
range
c
.
mounts
{
fmt
.
Fprintf
(
buf
,
"%s:{%s};"
,
m
.
prefix
.
String
(),
m
.
ds
.
DiskId
())
func
(
c
*
mountDatastoreConfig
)
DiskSpec
()
DiskSpec
{
cfg
:=
map
[
string
]
interface
{}{
"type"
:
"mount"
}
mounts
:=
make
([]
interface
{},
len
(
c
.
mounts
))
for
i
,
m
:=
range
c
.
mounts
{
c
:=
m
.
ds
.
DiskSpec
()
if
c
==
nil
{
c
=
make
(
map
[
string
]
interface
{})
}
c
[
"mountpoint"
]
=
m
.
prefix
mounts
[
i
]
=
c
}
return
buf
.
String
()
cfg
[
"mounts"
]
=
mounts
return
cfg
}
func
(
c
*
mountDatastoreConfig
)
Create
(
path
string
)
(
repo
.
Datastore
,
error
)
{
...
...
@@ -147,8 +166,12 @@ func FlatfsDatastoreConfig(params map[string]interface{}) (DatastoreConfig, erro
return
&
c
,
nil
}
func
(
c
*
flatfsDatastoreConfig
)
DiskId
()
string
{
return
fmt
.
Sprintf
(
"flatfs;%s;%s"
,
c
.
path
,
c
.
shardFun
.
String
())
func
(
c
*
flatfsDatastoreConfig
)
DiskSpec
()
DiskSpec
{
return
map
[
string
]
interface
{}{
"type"
:
"flatfs"
,
"path"
:
c
.
path
,
"shardFunc"
:
c
.
shardFun
.
String
(),
}
}
func
(
c
*
flatfsDatastoreConfig
)
Create
(
path
string
)
(
repo
.
Datastore
,
error
)
{
...
...
@@ -188,8 +211,11 @@ func LeveldsDatastoreConfig(params map[string]interface{}) (DatastoreConfig, err
return
&
c
,
nil
}
func
(
c
*
leveldsDatastoreConfig
)
DiskId
()
string
{
return
fmt
.
Sprintf
(
"levelds;%s"
,
c
.
path
)
func
(
c
*
leveldsDatastoreConfig
)
DiskSpec
()
DiskSpec
{
return
map
[
string
]
interface
{}{
"type"
:
"levelds"
,
"path"
:
c
.
path
,
}
}
func
(
c
*
leveldsDatastoreConfig
)
Create
(
path
string
)
(
repo
.
Datastore
,
error
)
{
...
...
@@ -211,8 +237,8 @@ func MemDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error)
return
&
memDatastoreConfig
{
params
},
nil
}
func
(
c
*
memDatastoreConfig
)
Disk
Id
()
string
{
return
""
func
(
c
*
memDatastoreConfig
)
Disk
Spec
()
DiskSpec
{
return
nil
}
func
(
c
*
memDatastoreConfig
)
Create
(
string
)
(
repo
.
Datastore
,
error
)
{
...
...
@@ -249,8 +275,8 @@ func (c *logDatastoreConfig) Create(path string) (repo.Datastore, error) {
return
ds
.
NewLogDatastore
(
child
,
c
.
name
),
nil
}
func
(
c
*
logDatastoreConfig
)
Disk
Id
()
string
{
return
c
.
child
.
Disk
Id
()
func
(
c
*
logDatastoreConfig
)
Disk
Spec
()
DiskSpec
{
return
c
.
child
.
Disk
Spec
()
}
type
measureDatastoreConfig
struct
{
...
...
@@ -274,8 +300,8 @@ func MeasureDatastoreConfig(params map[string]interface{}) (DatastoreConfig, err
return
&
measureDatastoreConfig
{
child
,
prefix
},
nil
}
func
(
c
*
measureDatastoreConfig
)
Disk
Id
()
string
{
return
c
.
child
.
Disk
Id
()
func
(
c
*
measureDatastoreConfig
)
Disk
Spec
()
DiskSpec
{
return
c
.
child
.
Disk
Spec
()
}
func
(
c
measureDatastoreConfig
)
Create
(
path
string
)
(
repo
.
Datastore
,
error
)
{
...
...
repo/fsrepo/fsrepo.go
浏览文件 @
d64ab3ce
package
fsrepo
import
(
"encoding/json"
"errors"
"fmt"
"io"
...
...
@@ -368,16 +367,16 @@ func (r *FSRepo) openDatastore() error {
if
err
!=
nil
{
return
err
}
diskId
:=
dsc
.
DiskId
()
spec
:=
dsc
.
DiskSpec
()
old
Id
,
_
,
err
:=
r
.
readSpec
()
old
Spec
,
err
:=
r
.
readSpec
()
if
err
==
nil
{
if
old
Id
!=
diskId
{
if
old
Spec
!=
spec
.
String
()
{
return
fmt
.
Errorf
(
"Datastore configuration of '%s' does not match what is on disk '%s'"
,
old
Id
,
diskId
)
old
Spec
,
spec
.
String
()
)
}
}
else
if
os
.
IsNotExist
(
err
)
{
err
:=
r
.
writeSpec
(
diskId
,
r
.
config
.
Datastore
.
Spec
)
err
:=
r
.
writeSpec
(
spec
.
String
()
)
if
err
!=
nil
{
return
err
}
...
...
@@ -398,55 +397,27 @@ func (r *FSRepo) openDatastore() error {
return
nil
}
var
SpecFn
=
"spec"
var
SpecFn
=
"
datastore_
spec"
func
(
r
*
FSRepo
)
readSpec
()
(
string
,
map
[
string
]
interface
{},
error
)
{
func
(
r
*
FSRepo
)
readSpec
()
(
string
,
error
)
{
fn
,
err
:=
config
.
Path
(
r
.
path
,
SpecFn
)
if
err
!=
nil
{
return
""
,
nil
,
err
return
""
,
err
}
b
,
err
:=
ioutil
.
ReadFile
(
fn
)
if
err
!=
nil
{
return
""
,
nil
,
err
return
""
,
err
}
idspec
:=
make
(
map
[
string
]
interface
{})
err
=
json
.
Unmarshal
(
b
,
&
idspec
)
if
err
!=
nil
{
return
""
,
nil
,
err
}
id
,
ok
:=
idspec
[
"id"
]
.
(
string
)
if
!
ok
{
return
""
,
nil
,
fmt
.
Errorf
(
"could not retrieve 'id' field from spec file"
)
}
spec
,
ok
:=
idspec
[
"spec"
]
.
(
map
[
string
]
interface
{})
if
!
ok
{
return
""
,
nil
,
fmt
.
Errorf
(
"could not retrieve 'spec' field from spec file"
)
}
dsc
,
err
:=
AnyDatastoreConfig
(
spec
)
if
err
!=
nil
{
return
""
,
nil
,
err
}
computedId
:=
dsc
.
DiskId
()
if
computedId
!=
id
{
return
""
,
nil
,
fmt
.
Errorf
(
"bad spec file, computed id (%s) does not match given (%s)"
,
computedId
,
id
)
}
return
id
,
spec
,
nil
return
strings
.
TrimSpace
(
string
(
b
)),
nil
}
func
(
r
*
FSRepo
)
writeSpec
(
id
string
,
spec
map
[
string
]
interface
{}
)
error
{
func
(
r
*
FSRepo
)
writeSpec
(
spec
string
)
error
{
fn
,
err
:=
config
.
Path
(
r
.
path
,
SpecFn
)
if
err
!=
nil
{
return
err
}
idspec
:=
map
[
string
]
interface
{}{
"id"
:
id
,
"spec"
:
spec
,
}
b
,
err
:=
json
.
Marshal
(
idspec
)
err
=
ioutil
.
WriteFile
(
fn
,
b
,
0666
)
b
:=
[]
byte
(
spec
)
err
=
ioutil
.
WriteFile
(
fn
,
b
,
0600
)
if
err
!=
nil
{
return
err
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论