Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ docker run -it -d -v /path/to/your/dir:/dirs --name quics-client -p 6120:6120 -
| connect | `qic connect root` | `--remote` `--password` | connect to the remote root directory | POST | /api/v1/connect/root/remote |
| connect | `qic connect list-remote` | | get the list of remote root directory | GET | /api/v1/connect/list/remote |
| disconnect | `qic disconnect root` | `--path` | disconnect to the root directory | POST | /api/v1/disconnect/root |
| sync | `qic sync status` | `--path` | get the status of the root directory | POST | /api/v1/sync/status |
| sync | `qic sync status` |y `--path` | get the status of the root directory | POST | /api/v1/sync/status |
| sync | `qic sync rescan` | | rescan the all of root directory | POST | /api/v1/sync/rescan |
| conflict | `qic conflict list` | | get the list of the root directory | GET | /api/v1/conflict/list |
| conflict | `qic conflict choose` | `--path` `--candidate` | choose the file of the root directory | POST | /api/v1/conflict/choose |
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b // indirect
github.com/google/wire v0.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b h1:RMpPgZTSApbPf7xaVel+QkoGPRLFLrwFO89uDUHEGf0=
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand Down Expand Up @@ -535,6 +538,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down
19 changes: 19 additions & 0 deletions pkg/app/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package app

import (
"github.com/google/wire"
"github.com/quic-s/quics-client/pkg/db/badger"
"github.com/quic-s/quics-client/pkg/sync"
)

type App struct {
db sync.DB
}

// TODO : DB -> APP

var DBSet = wire.NewSet(
badger.NewBadger,

wire.Bind(new(sync.DB), new(*badger.Badger)),
)
59 changes: 31 additions & 28 deletions pkg/db/badger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"strings"

"github.com/dgraph-io/badger/v3"

"github.com/quic-s/quics-client/pkg/types"
)

func GetUUID() string {
bUUID, err := View("UUID")
/*--------------USER INFO ----------------*/

func (db *Badger) GetUUID() string {
bUUID, err := db.view("UUID")
if err != nil {
log.Println(err)
}
Expand All @@ -23,8 +26,8 @@ func GetUUID() string {
/*--------------SYNC METADATA ----------------*/

// TODO NIL 반환 , 즉 아래 두개 함수를 합쳐야함
func GetSyncMetadata(path string) types.SyncMetadata {
bsyncMetadata, err := View(path)
func (db *Badger) GetSyncMetadata(path string) types.SyncMetadata {
bsyncMetadata, err := db.view(path)
if err != nil {
return types.SyncMetadata{}
}
Expand All @@ -34,8 +37,8 @@ func GetSyncMetadata(path string) types.SyncMetadata {

}

func IsSyncMetadataExisted(path string) bool {
syncMetadata := GetSyncMetadata(path)
func (db *Badger) IsSyncMetadataExisted(path string) bool {
syncMetadata := db.GetSyncMetadata(path)
if reflect.ValueOf(syncMetadata).IsZero() {
return false
}
Expand All @@ -44,13 +47,13 @@ func IsSyncMetadataExisted(path string) bool {

// Get All SyncMetadata in certain rootpath
// e.g. GetAllSyncMetadataInRoot("/home/username/rootdir")
func GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) {
func (db *Badger) GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) {
syncMetadataList := []*types.SyncMetadata{}

// get all file path in rootpath

prefix := rootpath
err := badgerdb.View(func(txn *badger.Txn) error {
err := db.BadgerDB.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
Expand Down Expand Up @@ -80,11 +83,11 @@ func GetAllSyncMetadataInRoot(rootpath string) ([]*types.SyncMetadata, error) {

// Get All SyncMetadata in all rootpath
// e.g. GetAllSyncMetadataAmongRoot()
func GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) {
func (db *Badger) GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) {
syncMetadataList := []*types.SyncMetadata{}
rootDirList := GetRootDirList()
rootDirList := db.GetRootDirList()
for _, rootDir := range rootDirList {
syncMetadata, err := GetAllSyncMetadataInRoot(rootDir.Path)
syncMetadata, err := db.GetAllSyncMetadataInRoot(rootDir.Path)
if err != nil {
return nil, err
}
Expand All @@ -96,8 +99,8 @@ func GetAllSyncMetadataAmongRoot() ([]*types.SyncMetadata, error) {
/*------------ROOT DIRECTORY---------------- */

// key : value == string : []RootDir
func GetRootDirList() []types.RootDir {
bRootDirList, err := View("RootDirList")
func (db *Badger) GetRootDirList() []types.RootDir {
bRootDirList, err := db.view("RootDirList")
if err != nil {
return []types.RootDir{}
}
Expand All @@ -109,8 +112,8 @@ func GetRootDirList() []types.RootDir {

}

func GetRootDir(path string) types.RootDir {
rootDirList := GetRootDirList()
func (db *Badger) GetRootDir(path string) types.RootDir {
rootDirList := db.GetRootDirList()
for _, rootDir := range rootDirList {
if rootDir.Path == path {
return rootDir
Expand All @@ -119,8 +122,8 @@ func GetRootDir(path string) types.RootDir {
return types.RootDir{}
}

func GetBeforePathWithAfterPath(afterPath string) string {
rootDirList := GetRootDirList()
func (db *Badger) GetBeforePathWithAfterPath(afterPath string) string {
rootDirList := db.GetRootDirList()
rootbase := strings.Split(afterPath, "/")[1]
for _, rootDir := range rootDirList {
if rootDir.AfterPath == "/"+rootbase {
Expand All @@ -130,8 +133,8 @@ func GetBeforePathWithAfterPath(afterPath string) string {
return ""
}

func SplitBeforeAfterRoot(path string) (string, string) {
rootDirList := GetRootDirList()
func (db *Badger) SplitBeforeAfterRoot(path string) (string, string) {
rootDirList := db.GetRootDirList()
for _, rootDir := range rootDirList {
if strings.HasPrefix(path, rootDir.BeforePath) {
return rootDir.BeforePath, strings.TrimPrefix(path, rootDir.BeforePath)
Expand All @@ -140,11 +143,11 @@ func SplitBeforeAfterRoot(path string) (string, string) {
return "", ""
}

func AddRootDir(path string) error {
func (db *Badger) AddRootDir(path string) error {

//If Same Absolute Path is already exist, return
//If Same Nickname is already taken , return
rootDirList := GetRootDirList()
rootDirList := db.GetRootDirList()
for _, rootDir := range rootDirList {
if rootDir.Path == path || rootDir.NickName == filepath.Base(path) {
return fmt.Errorf("this Directory is already exist as Root")
Expand All @@ -163,15 +166,15 @@ func AddRootDir(path string) error {

newRootDirList := types.RootDirList{}
newRootDirList = append(rootDirList, rootDir)
err := Update("RootDirList", newRootDirList.Encode())
err := db.update("RootDirList", newRootDirList.Encode())
if err != nil {
return err
}
return nil
}

func UpdateRootdirToRegistered(path string) error {
rootDirList := GetRootDirList()
func (db *Badger) UpdateRootdirToRegistered(path string) error {
rootDirList := db.GetRootDirList()
// Update IsRegistered
newRootDirList := types.RootDirList{}
for _, rootDir := range rootDirList {
Expand All @@ -190,15 +193,15 @@ func UpdateRootdirToRegistered(path string) error {
}
newRootDirList = append(newRootDirList, rootDir)
}
err := Update("RootDirList", newRootDirList.Encode())
err := db.update("RootDirList", newRootDirList.Encode())
if err != nil {
return err
}
return nil
}

func DeleteRootDir(path string) {
rootDirList := GetRootDirList()
func (db *Badger) DeleteRootDir(path string) {
rootDirList := db.GetRootDirList()
newRootDirList := types.RootDirList{}
for i, rootDir := range rootDirList {
if rootDir.Path == path {
Expand All @@ -207,5 +210,5 @@ func DeleteRootDir(path string) {
}
}

Update("RootDirList", newRootDirList.Encode())
db.update("RootDirList", newRootDirList.Encode())
}
21 changes: 9 additions & 12 deletions pkg/db/badger/badger.go → pkg/db/badger/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@ import (
"github.com/dgraph-io/badger/v3"
)

type CRUD interface {
Update(key string, value []byte) error
View(key string) ([]byte, error)
Delete(key string) error
}

func Update(key string, value []byte) error {
func (bg *Badger) update(key string, value []byte) error {
mutex := &bg.Mutex
mutex.Lock()
defer mutex.Unlock()
return badgerdb.Update(func(txn *badger.Txn) error {
return bg.BadgerDB.Update(func(txn *badger.Txn) error {
if err := txn.Set([]byte(key), value); err != nil {
return err
}
return nil
})
}

func View(key string) ([]byte, error) {
func (bg *Badger) view(key string) ([]byte, error) {
mutex := &bg.Mutex
mutex.Lock()
defer mutex.Unlock()
var value []byte

err := badgerdb.View(func(txn *badger.Txn) error {
err := bg.BadgerDB.View(func(txn *badger.Txn) error {

item, err := txn.Get([]byte(key))
if err != nil {
Expand All @@ -44,10 +40,11 @@ func View(key string) ([]byte, error) {
return value, err
}

func Delete(key string) error {
func (bg *Badger) delete(key string) error {
mutex := &bg.Mutex
mutex.Lock()
defer mutex.Unlock()
err := badgerdb.Update(func(txn *badger.Txn) error {
err := bg.BadgerDB.Update(func(txn *badger.Txn) error {
err := txn.Delete([]byte(key))
return err
})
Expand Down
18 changes: 11 additions & 7 deletions pkg/db/badger/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ import (
)

// Declare a global variable for the DB.
var badgerdb *badger.DB
var mutex sync.Mutex
type Badger struct {
BadgerDB *badger.DB
Mutex sync.Mutex `wire:"-"` // ignore this field
}

// Define a function to open the DB.
func OpenDB() {
func NewBadger() *Badger {
// Open the Badger database located in the ./badger directory.
// It will be created if it doesn't exist.
opts := badger.DefaultOptions(utils.GetQuicsDirPath() + "/badger")
db, err := badger.Open(opts)
badgerdb = db
if err != nil {
log.Fatal(err)
}
return &Badger{
BadgerDB: db,
}

}

func CloseDB() {
if err := badgerdb.Close(); err != nil {
func (bg *Badger) CloseDB() {
if err := bg.BadgerDB.Close(); err != nil {
log.Fatal(err)
}
}
5 changes: 5 additions & 0 deletions pkg/net/http3/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import (
"github.com/quic-s/quics-client/pkg/viper"
)

func Http3ServerProvider() {

}
func RestServerStart(port string) {

fmt.Println("\t-----------------------------------------\n")
fmt.Println("\t quics-client start\n")
fmt.Println("\t-----------------------------------------\n")

log.Println("quics-client : starting port " + viper.GetViperEnvVariables("REST_SERVER_PORT"))

//TODO bagder injection
badger.OpenDB()

sync.InitWatcher()
Expand Down
6 changes: 3 additions & 3 deletions pkg/net/qclient/cfAdapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
qstypes "github.com/quic-s/quics/pkg/types"
)

func SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string) (*qstypes.PleaseFileRes, error) {
func (qc *QPClient) SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string) (*qstypes.PleaseFileRes, error) {

breq := qstypes.PleaseFileReq{
UUID: UUID,
Expand All @@ -35,7 +35,7 @@ func SendChooseOne(stream *qp.Stream, UUID string, AfterPath string, side string

}

func SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictListRes, error) {
func (qc *QPClient) SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictListRes, error) {
breq := qstypes.AskConflictListReq{
UUID: UUID,
}
Expand All @@ -59,7 +59,7 @@ func SendAskConflictList(stream *qp.Stream, UUID string) (*qstypes.AskConflictLi

}

func SendConflictDownload(stream *qp.Stream, UUID string, AfterPath string) ([]*qstypes.ConflictDownloadReq, error) {
func (qc *QPClient) SendConflictDownload(stream *qp.Stream, UUID string, AfterPath string) ([]*qstypes.ConflictDownloadReq, error) {

breq := qstypes.AskStagingNumReq{
UUID: UUID,
Expand Down
5 changes: 2 additions & 3 deletions pkg/net/qclient/fcAdapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
qstypes "github.com/quic-s/quics/pkg/types"
)

func ForceSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, string, error) {
func (qc *QPClient) ForceSyncRecvHandler(stream *qp.Stream, badger *badger.Badger) (*qstypes.MustSyncReq, string, error) {

data, fileInfo, fileContent, err := stream.RecvFileBMessage()

if err != nil {
log.Println("", err)
return nil, "", err
Expand Down Expand Up @@ -69,7 +68,7 @@ func ForceSyncRecvHandler(stream *qp.Stream) (*qstypes.MustSyncReq, string, erro

}

func ForceSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error {
func (qc *QPClient) ForceSyncHandler(stream *qp.Stream, UUID string, AfterPath string, LastSyncTimestamp uint64, LastSyncHash string) error {
bres := qstypes.MustSyncRes{
UUID: UUID,
AfterPath: AfterPath,
Expand Down
Loading