-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from ipfs-force-community/feat/shard-to-mysql
feat: persist shard to mysql
- Loading branch information
Showing
11 changed files
with
268 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package badger | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/filecoin-project/dagstore" | ||
) | ||
|
||
// dagstore already implements ShardRepo, so we don't need to it again. | ||
// https://github.com/ipfs-force-community/dagstore/blob/master/shard_repo.go#L27 | ||
type Shard struct{} | ||
|
||
func NewShardRepo() *Shard { | ||
return &Shard{} | ||
} | ||
|
||
func (s *Shard) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error { | ||
panic("implement me") | ||
} | ||
func (s *Shard) GetShard(ctx context.Context, key string) (*dagstore.PersistedShard, error) { | ||
panic("implement me") | ||
} | ||
func (s *Shard) ListShards(ctx context.Context) ([]*dagstore.PersistedShard, error) { | ||
panic("implement me") | ||
} | ||
func (s *Shard) HasShard(ctx context.Context, key string) (bool, error) { | ||
panic("implement me") | ||
} | ||
func (s *Shard) DeleteShard(ctx context.Context, key string) error { | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/filecoin-project/dagstore" | ||
"github.com/ipfs-force-community/droplet/v2/models/repo" | ||
"gorm.io/gorm" | ||
) | ||
|
||
const shardTableName = "shards" | ||
|
||
type shard struct { | ||
Key string `gorm:"column:key;primaryKey;type:varchar(128)"` | ||
URL string `gorm:"column:url;type:varchar(256)"` | ||
TransientPath string `gorm:"column:transient_path;type:varchar(256)"` | ||
State dagstore.ShardState `gorm:"column:state;type:varchar(32)"` | ||
Lazy bool `gorm:"column:lazy"` | ||
Error string `gorm:"column:error;type:varchar(256)"` | ||
} | ||
|
||
func (s *shard) TableName() string { | ||
return shardTableName | ||
} | ||
|
||
type shardRepo struct { | ||
*gorm.DB | ||
} | ||
|
||
func NewShardRepo(db *gorm.DB) repo.IShardRepo { | ||
return &shardRepo{DB: db} | ||
} | ||
|
||
func to(s *shard) *dagstore.PersistedShard { | ||
return &dagstore.PersistedShard{ | ||
Key: s.Key, | ||
URL: s.URL, | ||
TransientPath: s.TransientPath, | ||
State: s.State, | ||
Lazy: s.Lazy, | ||
Error: s.Error, | ||
} | ||
} | ||
|
||
func from(s *dagstore.PersistedShard) *shard { | ||
return &shard{ | ||
Key: s.Key, | ||
URL: s.URL, | ||
TransientPath: s.TransientPath, | ||
State: s.State, | ||
Lazy: s.Lazy, | ||
Error: s.Error, | ||
} | ||
} | ||
|
||
func (s *shardRepo) SaveShard(ctx context.Context, shard *dagstore.PersistedShard) error { | ||
return s.DB.WithContext(ctx).Save(from(shard)).Error | ||
} | ||
|
||
func (s *shardRepo) GetShard(ctx context.Context, key string) (*dagstore.PersistedShard, error) { | ||
var shard shard | ||
if err := s.DB.WithContext(ctx).Take(&shard, "`key` = ?", key).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return to(&shard), nil | ||
} | ||
|
||
func (s *shardRepo) ListShards(ctx context.Context) ([]*dagstore.PersistedShard, error) { | ||
var shards []*shard | ||
if err := s.DB.WithContext(ctx).Find(&shards).Error; err != nil { | ||
return nil, err | ||
} | ||
out := make([]*dagstore.PersistedShard, 0, len(shards)) | ||
for _, shard := range shards { | ||
out = append(out, to(shard)) | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func (s *shardRepo) HasShard(ctx context.Context, key string) (bool, error) { | ||
var count int64 | ||
if err := s.DB.Model(&shard{}).WithContext(ctx).Where("`key` = ?", key). | ||
Count(&count).Error; err != nil { | ||
return false, nil | ||
} | ||
return count > 0, nil | ||
} | ||
|
||
func (s *shardRepo) DeleteShard(ctx context.Context, key string) error { | ||
return s.DB.WithContext(ctx).Where("`key` = ?", key).Delete(&shard{}).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/filecoin-project/dagstore" | ||
"github.com/filecoin-project/venus/venus-shared/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var columns = []string{"key", "url", "transient_path", "state", "lazy", "error"} | ||
|
||
func TestSaveShard(t *testing.T) { | ||
r, mock, db := setup(t) | ||
|
||
var shard dagstore.PersistedShard | ||
testutil.Provide(t, &shard) | ||
|
||
mock.ExpectBegin() | ||
mock.ExpectExec(regexp.QuoteMeta("UPDATE `shards` SET `url`=?,`transient_path`=?,`state`=?,`lazy`=?,`error`=? WHERE `key` = ?")). | ||
WithArgs(shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error, shard.Key). | ||
WillReturnResult(sqlmock.NewResult(1, 1)) | ||
mock.ExpectCommit() | ||
|
||
err := r.ShardRepo().SaveShard(context.Background(), &shard) | ||
assert.NoError(t, err) | ||
|
||
_ = closeDB(mock, db) | ||
} | ||
|
||
func TestGetShard(t *testing.T) { | ||
r, mock, db := setup(t) | ||
|
||
var shard dagstore.PersistedShard | ||
testutil.Provide(t, &shard) | ||
|
||
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `shards` WHERE `key` = ? LIMIT 1")).WithArgs(shard.Key). | ||
WillReturnRows(sqlmock.NewRows(columns). | ||
AddRow(shard.Key, shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error)) | ||
|
||
res, err := r.ShardRepo().GetShard(context.Background(), shard.Key) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &shard, res) | ||
|
||
_ = closeDB(mock, db) | ||
} | ||
|
||
func TestListShards(t *testing.T) { | ||
r, mock, db := setup(t) | ||
|
||
var shard dagstore.PersistedShard | ||
testutil.Provide(t, &shard) | ||
|
||
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `shards`")).WillReturnRows(sqlmock.NewRows(columns). | ||
AddRow(shard.Key, shard.URL, shard.TransientPath, shard.State, shard.Lazy, shard.Error)) | ||
|
||
res, err := r.ShardRepo().ListShards(context.Background()) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &shard, res[0]) | ||
|
||
_ = closeDB(mock, db) | ||
} | ||
|
||
func TestHasShard(t *testing.T) { | ||
r, mock, db := setup(t) | ||
|
||
var shard dagstore.PersistedShard | ||
testutil.Provide(t, &shard) | ||
|
||
mock.ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM `shards` WHERE `key` = ?")).WithArgs(shard.Key). | ||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) | ||
|
||
res, err := r.ShardRepo().HasShard(context.Background(), shard.Key) | ||
assert.NoError(t, err) | ||
assert.True(t, res) | ||
|
||
_ = closeDB(mock, db) | ||
} | ||
|
||
func TestDeleteShard(t *testing.T) { | ||
r, mock, db := setup(t) | ||
|
||
var shard dagstore.PersistedShard | ||
testutil.Provide(t, &shard) | ||
|
||
mock.ExpectBegin() | ||
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM `shards` WHERE `key` = ?")).WithArgs(shard.Key). | ||
WillReturnResult(sqlmock.NewResult(1, 1)) | ||
mock.ExpectCommit() | ||
|
||
err := r.ShardRepo().DeleteShard(context.Background(), shard.Key) | ||
assert.NoError(t, err) | ||
|
||
_ = closeDB(mock, db) | ||
} |
Oops, something went wrong.