-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* merge support is ready, but never tested * update merge, not tested
- Loading branch information
1 parent
b6117f2
commit 6789ba7
Showing
9 changed files
with
199 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"goat/pkg/db" | ||
"goat/pkg/logger" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func TestMergeSimple(t *testing.T) { | ||
var sources []*db.DB | ||
|
||
sourceNames := []string{ | ||
"../samples/data/strategy_data.dumpdb", | ||
"../samples/data/strategy_data.dumpdb", | ||
} | ||
for _, name := range sourceNames { | ||
tmp, err := db.NewSQLiteDataBase(name, false) | ||
if err != nil { | ||
logger.Logger.Error("failed to open database", zap.Error(err)) | ||
t.Fatal("failed to open database") | ||
} | ||
sources = append(sources, tmp) | ||
} | ||
|
||
defer os.Remove("tempoutput.dumpdb") | ||
output, err := db.NewSQLiteDataBase("tempoutput.dumpdb", true) | ||
if err != nil { | ||
logger.Logger.Error("failed to create output database", zap.Error(err)) | ||
t.Fatal("failed to create output database") | ||
} | ||
|
||
if err := db.MergeDBs(output, sources); err != nil { | ||
logger.Logger.Error("failed to merge databases", zap.Error(err)) | ||
t.Fatal("failed to merge databases") | ||
} | ||
} |
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,66 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"goat/pkg/logger" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func MergeDBs(output *DB, sources []*DB) error { | ||
if output == nil { | ||
return fmt.Errorf("output db is nil") | ||
} | ||
if len(sources) == 0 { | ||
return fmt.Errorf("no input db") | ||
} | ||
|
||
var totalCount int64 | ||
for _, source := range sources { | ||
count := source.FetchAll(true) | ||
totalCount += count | ||
} | ||
logger.Logger.Info("total count", zap.Int64("count", totalCount)) | ||
|
||
loopNext: | ||
for { | ||
var nextData *BarData | ||
nextIdx := -1 | ||
for idx, oneSource := range sources { | ||
cmpNextData, err := oneSource.Peek() | ||
if err != nil { | ||
logger.Logger.Error("failed to peek data", zap.Error(err)) | ||
os.Exit(1) | ||
} | ||
if cmpNextData == nil { | ||
if len(sources) == 1 { | ||
return nil | ||
} | ||
sources = append(sources[:idx], sources[idx+1:]...) | ||
continue loopNext | ||
} | ||
if nextData == nil || cmpNextData.DateTime < nextData.DateTime || | ||
(cmpNextData.Frequency < nextData.Frequency && cmpNextData.Frequency >= 0) { | ||
nextData = cmpNextData | ||
nextIdx = idx | ||
} | ||
} | ||
bar := &BarData{ | ||
Symbol: nextData.Symbol, | ||
DateTime: nextData.DateTime, | ||
Open: nextData.Open, | ||
High: nextData.High, | ||
Low: nextData.Low, | ||
Close: nextData.Close, | ||
Volume: nextData.Volume, | ||
AdjClose: nextData.AdjClose, | ||
Frequency: nextData.Frequency, | ||
Note: nextData.Note, | ||
} | ||
output.Create(bar) | ||
sources[nextIdx].Next() | ||
} | ||
// we should never reach here | ||
} |
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