Skip to content

Commit 5990c4a

Browse files
committed
Merge pull request #356 from ipfs-force-community/feat/car-index
feat: tools: generate car index and import index to mongo
1 parent 7ac0895 commit 5990c4a

File tree

9 files changed

+408
-1
lines changed

9 files changed

+408
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ badgerStAskDb/
88
*.puml
99
/droplet-client
1010
/droplet
11+
/index-tool
1112
sequence_chart.md
1213
.idea
1314
.vscode

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ droplet-client: $(BUILD_DEPS)
7070
rm -f droplet-client
7171
go build -o ./droplet-client $(GOFLAGS) ./cmd/droplet-client
7272

73+
index: $(BUILD_DEPS)
74+
rm -f index-tool
75+
go build -o ./index-tool $(GOFLAGS) ./tools/index
76+
7377
add-debug-flag:
7478
GOFLAGS+=-gcflags="all=-N -l"
7579

dagstore/mongo_topindex.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type MongoTopIndex struct {
2525
indexCol *mongo.Collection
2626
}
2727

28-
func NewMongoTopIndex(ctx context.Context, url string) (index.Inverted, error) {
28+
func NewMongoTopIndex(ctx context.Context, url string) (*MongoTopIndex, error) {
2929
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
3030
defer cancel()
3131
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
@@ -72,3 +72,12 @@ func (mongoTopIndex *MongoTopIndex) GetShardsForMultihash(ctx context.Context, h
7272
}
7373
return shardKeys, nil
7474
}
75+
76+
func (mongoTopIndex *MongoTopIndex) HasShard(ctx context.Context, shard shard.Key) (bool, error) {
77+
count, err := mongoTopIndex.indexCol.CountDocuments(ctx, bson.M{"pieces": shard.String()})
78+
if err != nil {
79+
return false, err
80+
}
81+
82+
return count > 0, nil
83+
}

dagstore/mongo_topindex_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ func TestAddMultihashesForShard(t *testing.T) {
5656
})
5757
assert.Nil(t, err)
5858
}
59+
60+
{
61+
has, err := indexSaver.HasShard(ctx, shard.KeyFromString(entry.Name()))
62+
assert.NoError(t, err)
63+
assert.True(t, has)
64+
}
5965
}
6066
}
6167

models/badger/shard.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func NewShardRepo() *Shard {
1414
return &Shard{}
1515
}
1616

17+
func (s *Shard) CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error {
18+
panic("implement me")
19+
}
20+
1721
func (s *Shard) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error {
1822
panic("implement me")
1923
}

models/mysql/shard.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func from(s *dagstore.PersistedShard) *shard {
5353
}
5454
}
5555

56+
func (s *shardRepo) CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error {
57+
return s.DB.WithContext(ctx).Create(from(shard)).Error
58+
}
59+
5660
func (s *shardRepo) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error {
5761
return s.DB.WithContext(ctx).Save(from(shard)).Error
5862
}

models/repo/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type ICidInfoRepo interface {
9999
}
100100

101101
type IShardRepo interface {
102+
CreateShard(ctx context.Context, shard *dagstore.PersistedShard) error
102103
dagstore.ShardRepo
103104
}
104105

tools/index/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 索引工具
2+
3+
主要有两个功能,一个是给未生成索引的 active 订单生成索引,另一个是迁移 top index 到 MongoDB,迁移 shard 到 MySQL。
4+
5+
### 编译
6+
7+
```
8+
make index
9+
```
10+
11+
### 生成索引
12+
13+
先去 droplet 获取订单状态是 active 的订单,然后去遍历 car 文件,如果被 active 订单使用且未生成索引,则为其生成索引。
14+
15+
* --car-dir:存储 car 文件的目录。
16+
* --index-dir:存储索引文件的目录,`droplet` 默认在 `~/.droplet/dagstore/index`
17+
* --mongo-url:MongoDB 的连接地址,用于存储 top index,数据库是 `market_index`,collection 是 `top_index`
18+
* --mysql-url:MySQL 的连接地址,用于存储 shard 状态,要和 `droplet` 使用同一个数据库,表名是 `shards`
19+
* --droplet-url:droplet 服务的 RPC 地址。
20+
* --droplet-token:droplet 服务的 token。
21+
22+
```bash
23+
./index-tool gen-index \
24+
--car-dir=<car dir> \
25+
--index-dir=<index dir> \
26+
--mongo-url="mongodb://user:pass@host/?retryWrites=true&w=majority" \
27+
--mysql-url="user:pass@(127.0.0.1:3306)/venus-market?parseTime=true&loc=Local" \
28+
--droplet-urls="/ip4/127.0.0.1/tcp/41235" \
29+
--droplet-token=<token>
30+
```
31+
32+
> 成功生成索引会输出类似日志:`generate index success: xxxxxx`
33+
34+
### 迁移索引
35+
36+
目前 top index 和 shard 都是存储在 badger,这样多个 droplet 时不能共享,所有需要把 top index 存储到 MongoDB,shard 存储到 MySQL,方便共享数据。
37+
38+
* --index-dir:存储索引文件的目录,`droplet` 默认在 `~/.droplet/dagstore/index`
39+
* --mongo-url:MongoDB 的连接地址,用于存储 top index,数据库是 `market_index``collection``top_index`
40+
* --mysql-url:MySQL 的连接地址,用于存储 shard 状态,要和 `droplet` 使用同一个数据库,表名是 `shards`
41+
42+
```bash
43+
./index-tool migrate-index \
44+
--index-dir=<index dir> \
45+
--mongo-url="mongodb://user:pass@host/?retryWrites=true&w=majority" \
46+
--mysql-url="user:pass@(127.0.0.1:3306)/venus-market?parseTime=true&loc=Local" \
47+
--droplet-urls="/ip4/127.0.0.1/tcp/41235" \
48+
--droplet-token=<token>
49+
```
50+
51+
> 成功迁移索引会输出类似日志:`migrate xxxxx success`

0 commit comments

Comments
 (0)