Beispiel #1
0
func (r *Rethinkdb) recreateTable() {
	if r.debug {
		fmt.Printf("dropping and creating table '%s' on database '%s'\n", r.table, r.database)
	}
	gorethink.DB(r.database).TableDrop(r.table).RunWrite(r.client)
	gorethink.DB(r.database).TableCreate(r.table).RunWrite(r.client)
}
func subscribeHandler(c *echo.Context) (err error) {
	ws := c.Socket()

	if err = websocket.JSON.Send(ws, "connected"); err != nil {
		log.Println(err)
		return
	}

	res, err := r.DB("notifications").Table("notification").Changes().Run(session)

	if err != nil {
		log.Println(err)
		return
	}

	var value interface{}

	defer res.Close()

	for res.Next(&value) {
		websocket.JSON.Send(ws, value)
	}

	return
}
Beispiel #3
0
func TableSetup(db, table string, optArgs ...r.TableCreateOpts) (r.WriteResponse, error) {
	wr, err := r.DBCreate(db).RunWrite(Session())
	if err != nil {
		return wr, err
	}

	return r.DB(db).TableCreate(table, optArgs...).RunWrite(Session())
}
Beispiel #4
0
func main() {
	fmt.Println("Connecting to RethinkDB")

	session, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:7704",
		Database: "players",
	})

	session.SetMaxOpenConns(10)

	if err != nil {
		log.Fatal("Could not connect")
	}

	r.DBDrop("players").Exec(session)
	err = r.DBCreate("players").Exec(session)
	if err != nil {
		panic(err)
	}

	err = r.DB("players").TableDrop("scores").Exec(session)
	err = r.DB("players").TableCreate("scores").Exec(session)
	if err != nil {
		log.Fatal("Could not create table", err)
	}

	err = r.DB("players").Table("scores").IndexCreate("Score").Exec(session)
	if err != nil {
		log.Fatal("Could not create index")
	}

	fmt.Println("Inserting new records")
	for i := 0; i < 10000; i++ {
		player := new(ScoreEntry)
		player.ID = strconv.Itoa(i)
		player.PlayerName = fmt.Sprintf("Player %d", i)
		player.Score = rand.Intn(100)
		_, err := r.Table("scores").Insert(player).RunWrite(session)
		if err != nil {
			log.Fatal(err)
		}
	}
}
func createNotificationHandler(response http.ResponseWriter, request *http.Request) {

	sent_value := request.FormValue("value")

	notif := new(Notification)
	notif.Value = sent_value

	err := r.DB("notifications").Table("notification").Insert(notif).Exec(session)

	if err != nil {
		http.Error(response, err.Error(), 500)
		log.Println(err)
	}
}
func setupHandler(response http.ResponseWriter, request *http.Request) {

	err := r.DBCreate("notifications").Exec(session)
	if err != nil {
		http.Error(response, err.Error(), 500)
		log.Println(err)

		err = r.DB("notifications").TableCreate("notification").Exec(session)
		if err != nil {
			http.Error(response, err.Error(), 500)
			log.Println(err)
		}
	}
}
func getAllHandler(response http.ResponseWriter, request *http.Request) {
	rows, err := r.DB("notifications").Table("notification").Run(session)

	if err == nil {
		defer rows.Close()

		var row Notification

		for rows.Next(&row) {
			response.Write([]byte(row.Value))
			response.Write([]byte(" | "))
		}
	} else {
		http.Error(response, err.Error(), 500)
		log.Println(err)
	}
}
Beispiel #8
0
func (r *Rethinkdb) assertServerVersion(constraint version.Constraints) error {
	cursor, err := gorethink.DB("rethinkdb").Table("server_status").Run(r.client)
	if err != nil {
		return err
	}

	if cursor.IsNil() {
		return errors.New("could not determine the RethinkDB server version: no rows returned from the server_status table")
	}

	var serverStatus rethinkDbServerStatus
	cursor.Next(&serverStatus)

	if serverStatus.Process.Version == "" {
		return errors.New("could not determine the RethinkDB server version: process.version key missing")
	}

	pieces := strings.Split(serverStatus.Process.Version, " ")
	if len(pieces) < 2 {
		return fmt.Errorf("could not determine the RethinkDB server version: malformed version string (%v)", serverStatus.Process.Version)
	}

	versionString := rethinkDbVersionMatcher.FindString(pieces[1])
	if versionString == "" {
		return fmt.Errorf("could not determine the RethinkDB server version: malformed version string (%v)", serverStatus.Process.Version)
	}

	if r.debug {
		fmt.Printf("RethinkDB version: %v\n", versionString)
	}

	serverVersion, err := version.NewVersion(versionString)
	if err != nil {
		return fmt.Errorf("could not determine the RethinkDB server version: malformed version string (%v)", serverStatus.Process.Version)
	}

	if !constraint.Check(serverVersion) {
		return fmt.Errorf("RethinkDB server version too old: expected %v, but was %v", constraint, serverVersion)
	}

	return nil
}
Beispiel #9
0
// Start the adaptor as a source
func (r *Rethinkdb) Start() error {
	tables, err := gorethink.DB(r.database).TableList().Run(r.client)
	if err != nil {
		return err
	}
	defer tables.Close()

	var (
		wg     sync.WaitGroup
		outerr error
		table  string
	)
	for tables.Next(&table) {
		if match := r.tableMatch.MatchString(table); !match {
			if r.debug {
				fmt.Printf("table, %s, didn't match\n", table)
			}
			continue
		}
		wg.Add(1)
		start := make(chan bool)
		if r.tail {
			go r.startupChangesForTable(table, start, &wg)
		}
		go func(table string) {
			defer wg.Done()

			if err := r.sendAllDocuments(table); err != nil {
				r.pipe.Err <- err
				outerr = err
			} else if r.tail {
				start <- true
			}
			close(start)

		}(table)
	}
	wg.Wait()

	return nil
}
Beispiel #10
0
func Run(db, table string, run DBRun, optArgs ...r.RunOpts) (*r.Cursor, error) {
	tbl := r.DB(db).Table(table)
	term := run(tbl)
	return term.Run(Session(), optArgs...)
}
Beispiel #11
0
func TableCreate(db, table string, optArgs ...r.TableCreateOpts) (r.WriteResponse, error) {
	return r.DB(db).TableCreate(table, optArgs...).RunWrite(Session())
}
Beispiel #12
0
func TestDial(t *testing.T) {
	Convey("Given rethinkdb host, db name, table name", t, func() {
		host := "localhost:28015"
		dbName := "rethinker_test"
		tableName := "contacts"

		Printf("host[%s] db[%s] table[%s] \n", host, dbName, tableName)

		Convey("Startup connection", func() {
			var err error
			err = Startup(r.ConnectOpts{
				Address: host,
			})

			So(err, ShouldBeNil)

			Convey("Create db", func() {
				_, err = r.DBCreate(dbName).RunWrite(Session())
				So(err, ShouldBeNil)

				Convey("Create table", func() {
					_, err = r.DB(dbName).TableCreate(tableName).RunWrite(Session())
					So(err, ShouldBeNil)

					Convey("Insert item", func() {
						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
							return table.Insert(map[string]interface{}{
								"first_name": "tester",
							})
						})
						So(err, ShouldBeNil)
					})

					//					Convey("Find item", func() {
					//						c, err := Run(dbName, tableName, func(table r.Term) r.Term {
					//							return table.GetAllByIndex(0)
					//						})

					//						So(err, ShouldBeNil)

					//						item := make(map[string]interface{})
					//						c.One(&item)

					//						So(item["first_name"], ShouldEqual, "tester")
					//					})

					//					Convey("Delete item", func() {
					//						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
					//							return table.Delete()
					//						})
					//						So(err, ShouldBeNil)
					//					})

					Reset(func() {
						r.DB(dbName).TableDrop(tableName).RunWrite(Session())
					})
				})

			})

			Reset(func() {
				r.DBDrop(dbName).RunWrite(Session())
			})
		})
	})
}
Beispiel #13
0
func RunWrite(db, table string, run DBRun, optArgs ...r.RunOpts) (r.WriteResponse, error) {
	tbl := r.DB(db).Table(table)
	term := run(tbl)
	return term.RunWrite(Session(), optArgs...)
}
func dropTable(name string) {
	r.DB(RethinkDBName).TableDrop(name).Exec(Rethink)
}
func createTable(name string) {
	r.DB(RethinkDBName).TableCreate(name).Exec(Rethink)
}
Beispiel #16
0
func IndexCreate(db, table string, name interface{}, optArgs ...r.IndexCreateOpts) (r.WriteResponse, error) {
	return r.DB(db).Table(table).IndexCreate(name, optArgs...).RunWrite(Session())
}
Beispiel #17
0
func TableDrop(db, table string) (r.WriteResponse, error) {
	return r.DB(db).TableDrop(table).RunWrite(Session())
}
Beispiel #18
0
func Table(db, table string) r.Term {
	return r.DB(db).Table(table)
}