Exemplo n.º 1
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")
	}

	fmt.Println("Loop through updates")
	for {
		pl := rand.Intn(10000)
		sc := rand.Intn(6) - 2

		// startingTime := time.Now()
		_, err = r.Table("scores").Get(strconv.Itoa(pl)).Update(map[string]interface{}{
			"Score": r.Row.Field("Score").Add(sc),
		}).RunWrite(session)
		// fmt.Println(time.Now().Sub(startingTime))

		time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
	}
}
Exemplo n.º 2
0
// NewRethinkdb creates a new Rethinkdb database adaptor
func NewRethinkdb(p *pipe.Pipe, path string, extra Config) (StopStartListener, error) {
	var (
		conf rethinkDbConfig
		err  error
	)
	if err = extra.Construct(&conf); err != nil {
		return nil, err
	}

	u, err := url.Parse(conf.URI)
	if err != nil {
		return nil, err
	}

	if conf.Debug {
		fmt.Printf("rethinkDbConfig: %#v\n", conf)
	}

	r := &Rethinkdb{
		uri:  u,
		pipe: p,
		path: path,
		tail: conf.Tail,
	}

	r.database, r.table, err = extra.splitNamespace()
	if err != nil {
		return r, err
	}
	r.debug = conf.Debug

	opts := gorethink.ConnectOpts{
		Address: r.uri.Host,
		MaxIdle: 10,
		Timeout: time.Second * 10,
	}
	if conf.Timeout > 0 {
		opts.Timeout = time.Duration(conf.Timeout) * time.Second
	}

	r.client, err = gorethink.Connect(opts)
	if err != nil {
		return r, err
	}
	r.client.Use(r.database)

	if r.tail {
		constraint, _ := version.NewConstraint(">= 1.16")
		if err := r.assertServerVersion(constraint); err != nil {
			return r, err
		}
	}

	return r, nil
}
Exemplo n.º 3
0
func Startup(opts r.ConnectOpts) error {
	var err error

	client, err = r.Connect(opts)

	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 4
0
func connect(config *Config) *rt.Session {
	session, err := rt.Connect(rt.ConnectOpts{
		Address:  config.DB.Address,
		Database: config.DB.DBName,
	})

	if err != nil {
		panic(err.Error())
	}

	return session
}
func init() {
	RethinkDBName = getEnvOrDefault("RETHINKDB_DB", "osin_rethinkdb_storage")

	session, err := r.Connect(getConfig())
	if err != nil {
		panic(err)
	}
	Rethink = session

	dropTestDatabase()
	createTestDatabase()
}
Exemplo n.º 6
0
func init() {
	Session, _ = rt.Connect(rt.ConnectOpts{
		Address:  "localhost:28015",
		Database: "test",
	})

	// rt.DB("test").TableDrop("card").Run(Session)
	// rt.DB("test").TableCreate("card").Run(Session)

	r = mux.NewRouter()
	SetupRouter(r, "/api/v1")

}
Exemplo n.º 7
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)
		}
	}
}
Exemplo n.º 8
0
func main() {
	var err error

	e := echo.New()

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

	if err != nil {
		panic("Could not connect to rethink")
	}

	session.SetMaxOpenConns(10)

	e.Get("/", setupHandler)
	e.Get("/v1", getAllHandler)
	e.Post("/v1", createNotificationHandler)
	e.WebSocket("/v1/subscribe", subscribeHandler)

	e.Run(":5000")
}
Exemplo n.º 9
0
Arquivo: n.go Projeto: esiqveland/den
func main() {
	// verify tesseract is available...
	goss, err := gosseract.NewClient()
	if err != nil {
		log.Fatalln(err.Error())
	}

	session, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "test",
	})
	if err != nil {
		log.Fatalln(err.Error())
	}

	mux := mux.NewRouter()
	mux.Handle("/post/new", createImageHandler(goss, NewPostingStore(session))).
		Methods("POST")

	mw := negroni.Classic()
	mw.UseHandler(mux)
	mw.Run(":1234")
}
Exemplo n.º 10
0
func main() {
	fmt.Println("I'm watching You!")

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

	if err != nil {
		log.Fatal(err)
	}

	res, err := r.Table("scores").Changes().Run(session)

	if err != nil {
		log.Fatalln(err)
	}

	var value interface{}
	for res.Next(&value) {
		fmt.Println(value)
	}
}
Exemplo n.º 11
0
// NewRethinkdb creates a new Rethinkdb database adaptor
func NewRethinkdb(p *pipe.Pipe, path string, extra Config) (StopStartListener, error) {
	var (
		conf rethinkDbConfig
		err  error
	)
	if err = extra.Construct(&conf); err != nil {
		return nil, err
	}

	u, err := url.Parse(conf.URI)
	if err != nil {
		return nil, err
	}

	if conf.Debug {
		fmt.Printf("RethinkDB Config %+v\n", conf)
	}

	r := &Rethinkdb{
		uri:  u,
		pipe: p,
		path: path,
		tail: conf.Tail,
	}

	r.database, r.tableMatch, err = extra.compileNamespace()
	if err != nil {
		return r, err
	}
	r.debug = conf.Debug
	if r.debug {
		fmt.Printf("tableMatch: %+v\n", r.tableMatch)
	}

	// test the connection with a timeout
	testConn, err := gorethink.Connect(gorethink.ConnectOpts{
		Address: r.uri.Host,
		Timeout: time.Second * 10,
	})
	if err != nil {
		return r, err
	}
	testConn.Close()

	// we don't want a timeout here because we want to keep connections open
	r.client, err = gorethink.Connect(gorethink.ConnectOpts{
		Address: r.uri.Host,
		MaxIdle: 10,
	})
	if err != nil {
		return r, err
	}
	r.client.Use(r.database)

	constraint, _ := version.NewConstraint(">= 2.0")
	if err := r.assertServerVersion(constraint); err != nil {
		return r, err
	}

	return r, nil
}