/* Before you execute the program, Launch `cqlsh` and execute: create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; create table example.tweet(timeline text, id UUID, text text, PRIMARY KEY(id)); create index on example.tweet(timeline); */ package main import ( //"time" "fmt" "strconv" "strings" . "git.wecise.com/wecise/odbserver/odb" "git.wecise.com/wecise/odbserver/odb/test" "gitee.com/wecisecode/util/logger" ) // CGO_ENABLED=1 go run --ldflags '-linkmode external -extldflags "-static"' shrink.go // #cgo LDFLAGS: -L/opt/odbserver/sqlite -lsqlite -ldl // #include "/opt/odbserver/sqlite/sqlite.h" // #include // #include // extern int64_t uhaha_seed; // extern int64_t uhaha_ts; // void uhaha_begin_reader(); // void uhaha_end_reader(); func shrink(g *Gutil, keyspace, table string) (map[string]int, map[string]int) { colcap := map[string]int{} cql := fmt.Sprintf(`select column_name from system_schema.columns where keyspace_name='%s' and table_name='%s'`, keyspace, table) if rows, err := g.RawQuery(cql); err != nil { logger.Error(err) return nil, nil } else { for _, row := range rows { col := row["column_name"].(string) if strings.HasPrefix(col, "_") { continue } if idx := strings.LastIndex(col, "_"); idx == -1 { continue } else { prefix := col[:idx] n, _ := strconv.Atoi(col[idx+1:]) if max, ok := colcap[prefix]; ok { if n > max { colcap[prefix] = n } } else { colcap[prefix] = n } } } } cql = fmt.Sprintf(`select cap from colbulk where domain='%s'`, keyspace) if rows, err := g.RawQuery(cql); err != nil { logger.Error(err) return nil, nil } else { return colcap, rows[0]["cap"].(map[string]int) } } func main() { // connect to the cluster //cluster := gocql.NewCluster("192.168.40.14") option := &Option{Cache: CacheAll, Keyspace: "matrix", DisableInitialHostLookup: true} g, err := test.NewG(option) if err != nil { logger.Error(err.Error()) } else { defer g.Close() } //logger.SetRollingDaily("C:/test/zkcron/src/test", "test.log") logger.SetConsole(true) colcap, realcap := shrink(g, "matrix", "object") for k, v := range colcap { logger.Infof("%20s ==> %10d ==> %10d", k, v, realcap[k]) } }