Created
September 4, 2014 23:38
-
-
Save beefsack/da10f922b52e530dc41d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "log" | |
| "strings" | |
| r "github.com/dancannon/gorethink" | |
| ) | |
| func CreateDatabaseIfNotExists(sess *r.Session, dName string) error { | |
| cur, err := r.DbList().Run(sess) | |
| if err != nil { | |
| return err | |
| } | |
| dbName := "" | |
| found := false | |
| for cur.Next(&dbName) { | |
| if dbName == dName { | |
| found = true | |
| break | |
| } | |
| } | |
| if !found { | |
| log.Printf("creating database %s\n", dName) | |
| _, err := r.DbCreate(dName).RunWrite(sess) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| } | |
| func CreateTableIfNotExists(sess *r.Session, dName, tName string) error { | |
| cur, err := r.Db(dName).TableList().Run(sess) | |
| if err != nil { | |
| return err | |
| } | |
| defer cur.Close() | |
| tableName := "" | |
| found := false | |
| for cur.Next(&tableName) { | |
| if tableName == tName { | |
| found = true | |
| break | |
| } | |
| } | |
| if !found { | |
| log.Printf("creating table %s\n", tName) | |
| _, err := r.Db(dName).TableCreate(tName).RunWrite(sess) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| } | |
| func IndexName(indexes ...string) string { | |
| return strings.Join(indexes, ":") | |
| } | |
| func CreateIndexIfNotExists( | |
| sess *r.Session, | |
| dName, tName string, | |
| indexes ...string, | |
| ) error { | |
| cur, err := r.Db(dName).Table(tName).IndexList().Run(sess) | |
| if err != nil { | |
| return err | |
| } | |
| defer cur.Close() | |
| indexName := IndexName(indexes...) | |
| n := "" | |
| found := false | |
| for cur.Next(&n) { | |
| if n == indexName { | |
| found = true | |
| break | |
| } | |
| } | |
| if found { | |
| return nil | |
| } | |
| log.Printf("creating index %s.%s\n", tName, indexName) | |
| if len(indexes) == 1 { | |
| _, err := r.Db(dName).Table(tName).IndexCreate(indexes[0]).RunWrite( | |
| sess) | |
| if err != nil { | |
| return nil | |
| } | |
| } else { | |
| _, err := r.Db(dName).Table(tName).IndexCreateFunc(indexName, | |
| func(row r.Term) interface{} { | |
| index := make([]interface{}, len(indexes)) | |
| for i, in := range indexes { | |
| index[i] = row.Field(in) | |
| } | |
| return index | |
| }).RunWrite(sess) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| _, err = r.Db(dName).Table(tName).IndexWait(indexName).Run(sess) | |
| return err | |
| } |
This file contains hidden or 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
| package main | |
| import ( | |
| "log" | |
| "time" | |
| r "github.com/dancannon/gorethink" | |
| ) | |
| type Aggregate struct { | |
| Id string `gorethink:"id,omitempty"` | |
| Language string `gorethink:"language"` | |
| Type string `gorethink:"type"` | |
| Start time.Time `gorethink:"start"` | |
| End time.Time `gorethink:"end"` | |
| Total int `gorethink:"total"` | |
| TotalDirty bool `gorethink:"total_dirty"` | |
| Ratio float64 `gorethink:"ratio"` | |
| RatioDirty bool `gorethink:"ratio_dirty"` | |
| Rank int `gorethink:"rank"` | |
| RankDirty bool `gorethink:"rank_dirty"` | |
| } | |
| var ( | |
| sess *r.Session | |
| ) | |
| const ( | |
| DName = "platest" | |
| TName = "created_aggregate" | |
| Address = "localhost:28015" | |
| ) | |
| func must(args ...interface{}) { | |
| if len(args) == 0 { | |
| log.Fatal("no args passed") | |
| } | |
| last := args[len(args)-1] | |
| if last == nil { | |
| return | |
| } | |
| log.Fatal(last.(error)) | |
| } | |
| func main() { | |
| var err error | |
| // Connect | |
| sess, err = r.Connect(r.ConnectOpts{ | |
| Address: Address, | |
| }) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // Create | |
| must(CreateDatabaseIfNotExists(sess, DName)) | |
| must(CreateTableIfNotExists(sess, DName, TName)) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "language")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "type")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "start")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "end")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "total")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "total_dirty")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "language", "type")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "type", "start")) | |
| must(CreateIndexIfNotExists(sess, DName, TName, "language", "type", "start")) | |
| // Stage | |
| must(r.Db(DName).Table(TName).Delete().RunWrite(sess)) | |
| must(r.Db(DName).Table(TName).Insert(Aggregate{ | |
| End: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC), | |
| Id: "09ffb19d-3fcf-4f05-82ab-7afff125f47f", | |
| Language: "Glyph", | |
| Rank: 112, | |
| RankDirty: true, | |
| Ratio: 0, | |
| RatioDirty: true, | |
| Start: time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC), | |
| Total: 0, | |
| TotalDirty: true, | |
| Type: "year", | |
| }).RunWrite(sess)) | |
| // Query | |
| cur, err := r.Db(DName).Table(TName).GetAllByIndex("total_dirty", true).Run(sess) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer cur.Close() | |
| aggregates := []Aggregate{} | |
| must(cur.All(&aggregates)) | |
| log.Print(aggregates[0].Start) | |
| } |
This file contains hidden or 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
| ➜ issue git:(master) ✗ go run *.go | |
| 2014/09/05 09:35:42 0001-01-01 00:00:00 +0000 UTC | |
| ➜ issue git:(master) ✗ rethinkdb --version | |
| rethinkdb 1.14.0 (GCC 4.9.1) | |
| ➜ issue git:(master) ✗ cd $GOPATH/src/github.com/dancannon/gorethink && git rev-parse HEAD | |
| 1f3a35cd67d01f00bb3172debc8ad86d85006d8d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment