main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // main.go
  2. package main
  3. import (
  4. "flag"
  5. "log"
  6. "os"
  7. "sort"
  8. "tickserver/server/market"
  9. )
  10. func main() {
  11. flag.Parse()
  12. n := flag.NArg()
  13. if n != 2 {
  14. log.Println("must input 2 files to be combined", n)
  15. return
  16. }
  17. fileMain := flag.Arg(0)
  18. fileAppend := flag.Arg(1)
  19. log.Println("main:", fileMain, "append", fileAppend)
  20. candles, err := market.ReadCandleFile(fileAppend)
  21. if err != nil {
  22. log.Println(err)
  23. return
  24. }
  25. candles, err = combin(fileMain, candles)
  26. if err != nil {
  27. log.Println(err)
  28. return
  29. }
  30. w, err := os.Create(fileMain)
  31. if err != nil {
  32. log.Println(err)
  33. return
  34. }
  35. defer w.Close()
  36. err = market.ZipCBuf(w, candles)
  37. if err != nil {
  38. log.Println(err)
  39. return
  40. }
  41. }
  42. // 把之前文件中candles和新的合并
  43. func combin(filename string, candles []market.Candle) ([]market.Candle, error) {
  44. buf, err := market.ReadCandleFile(filename)
  45. if err != nil {
  46. return candles, err
  47. }
  48. // 去除重复数据
  49. ts := buf[len(buf)-1].Timestamp
  50. n := sort.Search(len(candles), func(i int) bool {
  51. if candles[i].Timestamp >= ts {
  52. return true
  53. }
  54. return false
  55. })
  56. if n == len(candles) {
  57. n = 0
  58. }
  59. if candles[n].Timestamp == ts {
  60. n += 1
  61. }
  62. candles = append(buf, candles[n:]...)
  63. return candles, nil
  64. }