shrink.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Before you execute the program, Launch `cqlsh` and execute:
  3. create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
  4. create table example.tweet(timeline text, id UUID, text text, PRIMARY KEY(id));
  5. create index on example.tweet(timeline);
  6. */
  7. package main
  8. import (
  9. //"time"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. . "git.wecise.com/wecise/odbserver/odb"
  14. "git.wecise.com/wecise/odbserver/odb/test"
  15. "gitee.com/wecisecode/util/logger"
  16. )
  17. // CGO_ENABLED=1 go run --ldflags '-linkmode external -extldflags "-static"' shrink.go
  18. // #cgo LDFLAGS: -L/opt/odbserver/sqlite -lsqlite -ldl
  19. // #include "/opt/odbserver/sqlite/sqlite.h"
  20. // #include <stdint.h>
  21. // #include <stdlib.h>
  22. // extern int64_t uhaha_seed;
  23. // extern int64_t uhaha_ts;
  24. // void uhaha_begin_reader();
  25. // void uhaha_end_reader();
  26. func shrink(g *Gutil, keyspace, table string) (map[string]int, map[string]int) {
  27. colcap := map[string]int{}
  28. cql := fmt.Sprintf(`select column_name from system_schema.columns where keyspace_name='%s' and table_name='%s'`, keyspace, table)
  29. if rows, err := g.RawQuery(cql); err != nil {
  30. logger.Error(err)
  31. return nil, nil
  32. } else {
  33. for _, row := range rows {
  34. col := row["column_name"].(string)
  35. if strings.HasPrefix(col, "_") {
  36. continue
  37. }
  38. if idx := strings.LastIndex(col, "_"); idx == -1 {
  39. continue
  40. } else {
  41. prefix := col[:idx]
  42. n, _ := strconv.Atoi(col[idx+1:])
  43. if max, ok := colcap[prefix]; ok {
  44. if n > max {
  45. colcap[prefix] = n
  46. }
  47. } else {
  48. colcap[prefix] = n
  49. }
  50. }
  51. }
  52. }
  53. cql = fmt.Sprintf(`select cap from colbulk where domain='%s'`, keyspace)
  54. if rows, err := g.RawQuery(cql); err != nil {
  55. logger.Error(err)
  56. return nil, nil
  57. } else {
  58. return colcap, rows[0]["cap"].(map[string]int)
  59. }
  60. }
  61. func main() {
  62. // connect to the cluster
  63. //cluster := gocql.NewCluster("192.168.40.14")
  64. option := &Option{Cache: CacheAll, Keyspace: "matrix", DisableInitialHostLookup: true}
  65. g, err := test.NewG(option)
  66. if err != nil {
  67. logger.Error(err.Error())
  68. } else {
  69. defer g.Close()
  70. }
  71. //logger.SetRollingDaily("C:/test/zkcron/src/test", "test.log")
  72. logger.SetConsole(true)
  73. colcap, realcap := shrink(g, "matrix", "object")
  74. for k, v := range colcap {
  75. logger.Infof("%20s ==> %10d ==> %10d", k, v, realcap[k])
  76. }
  77. }