本文整理汇总了Golang中github.com/dancannon/gorethink.Db函数的典型用法代码示例。如果您正苦于以下问题:Golang Db函数的具体用法?Golang Db怎么用?Golang Db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Db函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetupSuite
func (suite *UserStoreSuite) SetupSuite() {
store, err := newTestUserStore()
if err != nil {
panic(err)
}
suite.store = store
// create necessary DB, table, indices
_, err = r.DbCreate("test").Run(suite.store.conn)
if err != nil {
panic(err)
}
_, err = r.Db("test").TableCreate("users").Run(suite.store.conn)
if err != nil {
panic(err)
}
_, err = r.Db("test").Table("users").IndexCreate("email").Run(suite.store.conn)
if err != nil {
panic(err)
}
suite.user = NewUser("[email protected]", "password")
_, err = r.Db("test").Table("users").Insert(suite.user).RunWrite(suite.store.conn)
if err != nil {
panic(err)
}
}
示例2: createTableWithOptions
func (db *RethinkDBAdapter) createTableWithOptions(tableName string, rdbOptions *r.TableCreateOpts) *CASServerError {
logMessagef(db.LogLevel, "INFO", "Creating table [%s], options: %v", tableName, rdbOptions)
// Check again that rdbOptions is not nil, optionally leave out argument
var err error
if rdbOptions == nil {
// Create table with no options
_, err = r.Db(db.dbName).TableCreate(tableName).Run(db.session)
} else {
// Set and get the table options (so that they can be retrieved later
db.setTableSetupOptions(tableName, rdbOptions)
options, err := db.getTableSetupOptions(tableName)
if err != nil {
casError := &FailedToCreateTableError
casError.err = &err
return casError
}
// Create table
_, err = r.Db(db.dbName).TableCreate(tableName, *options).Run(db.session)
}
if err != nil {
casError := &FailedToCreateTableError
casError.err = &err
return casError
}
return nil
}
示例3: main
func main() {
// Connecting to the database
addr := *rethinkdbHost + ":" + *rethinkdbPort
log.Info("Connecting to RethinkDB at ", addr)
sess, err := r.Connect(r.ConnectOpts{
Address: addr,
Database: *env,
})
if err != nil {
log.Fatal(err.Error())
}
defer sess.Close()
_, err = r.Db(*env).TableCreate(tmpTable).RunWrite(sess)
if err != nil {
log.Fatal("Couldn't create temp table. ", err.Error())
}
log.Info("Created temp table ", *tmpTable, " in db ", *env)
// init tmp table with accounts
// process array of table names (i.e. emails, files, etc.)
for _, t := range bigTables {
processTable(sess, t)
}
_, err = r.Db(*env).TableDrop(*tmpTable).RunWrite(sess)
if err != nil {
log.Fatal("Couldn't drop temp table. ", err.Error())
}
log.Info("Dropped temp table ", *tmpTable, " in db ", *env)
}
示例4: Example_IndexCreate_compound
func Example_IndexCreate_compound() {
sess, err := r.Connect(r.ConnectOpts{
Address: url,
AuthKey: authKey,
})
if err != nil {
log.Fatalf("Error connecting to DB: %s", err)
}
// Setup database
r.Db("test").TableDrop("table").Run(sess)
r.Db("test").TableCreate("table").Run(sess)
response, err := r.Db("test").Table("table").IndexCreateFunc("full_name", func(row r.Term) interface{} {
return []interface{}{row.Field("first_name"), row.Field("last_name")}
}).RunWrite(sess)
if err != nil {
log.Fatalf("Error creating index: %s", err)
}
fmt.Printf("%d index created", response.Created)
// Output:
// 1 index created
}
示例5: checkCleared
func (sr *Runner) checkCleared(check stalker.Check) {
log.Debugln("check cleared")
log.Infof("%s %s detected as cleared", check.Hostname, check.Check)
query := map[string]string{"hostname": check.Hostname, "check": check.Check}
cursor, err := r.Db(STALKERDB).Table("notifications").Filter(query).Run(sr.rsess)
if err != nil {
log.Errorln("Error checking for existing notification:", err.Error())
return
}
defer cursor.Close()
result := stalker.Notification{}
cursor.One(&result)
if result.Active == false {
log.Infoln("No notification to clear")
return
}
_, err = r.Db(STALKERDB).Table("notifications").Filter(query).Delete().RunWrite(sr.rsess)
if err != nil {
log.Errorln("Error deleting notification entry:", err.Error())
return
}
sr.emitClear(check)
return
}
示例6: processTable
func processTable(sess *r.Session, table string) {
rows, err := r.Db(*env).Table(table).Run(sess)
if err != nil {
log.Fatal("Couldn't fetch rows for table ", table)
}
defer rows.Close()
var doc map[string]interface{}
var size uint64
var id string
var ok bool
// WIP
for rows.Next(&doc) {
if size, ok = doc["size"].(uint64); !ok {
if id, ok = doc["id"].(string); !ok {
log.Warn("Found a document without ID! ", doc)
continue
}
sizeTerm, err := r.Db(*env).Table(table).Get(id).
CoerceTo("string").CoerceTo("binary").Count().Run(sess)
err = sizeTerm.One(&size)
if err != nil {
log.Warn("Couldn't compute the size of document ", id, ". It might've been deleted.", err)
continue
}
}
fmt.Println(doc)
return
} else {
}
}
示例7: init
func init() {
var err error
session, err = gorethink.Connect(gorethink.ConnectOpts{
Address: "127.0.0.1:28015",
})
if err != nil {
panic(err)
}
key2find = uniuri.New()
// Create a new table
gorethink.Db("test").TableDrop("benchmark_keys_list").Run(session)
gorethink.Db("test").TableCreate("benchmark_keys_list").Run(session)
var klist []*KeysList
// Populate with sample data
for n := 0; n < 300; n++ {
keys := rndStringSlice(999)
keys = randomlyInsert(keys, key2find)
y := uniuri.New()
if n == 153 {
table2search = y
}
klist = append(klist, &KeysList{
ID: y,
Voted: keys,
})
}
gorethink.Db("test").Table("benchmark_keys_list").Insert(klist).Run(session)
}
示例8: main
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
// Connect to RethinkDB
var err error
session, err = r.Connect(r.ConnectOpts{
Address: *rethinkAddress,
})
if err != nil {
log.Fatal(err)
}
// Create the database and tables
r.DbCreate(*rethinkName).Exec(session)
r.Db(*rethinkName).TableCreate("invites").Exec(session)
r.Db(*rethinkName).Table("invites").IndexCreate("email").Exec(session)
r.Db(*rethinkName).Table("invites").IndexCreate("name").Exec(session)
// Add a CORS middleware
goji.Use(cors.New(cors.Options{
AllowCredentials: true,
}).Handler)
// Add routes to goji
goji.Get("/", index)
goji.Post("/check", check)
goji.Post("/free", free)
goji.Post("/create", create)
// Start the server
goji.Serve()
}
示例9: main
func main() {
flag.Parse()
log = logrus.New()
if *logFormatterType == "text" {
log.Formatter = &logrus.TextFormatter{
ForceColors: *logForceColors,
}
} else if *logFormatterType == "json" {
log.Formatter = &logrus.JSONFormatter{}
}
log.Level = logrus.DebugLevel
if *enableHub || *enableSender {
var err error
session, err = r.Connect(r.ConnectOpts{
Address: *rethinkdbAddress,
})
if err != nil {
log.WithField("error", err.Error()).Fatal("Unable to connect to RethinkDB")
}
r.DbCreate(*rethinkdbDatabase).Exec(session)
r.Db(*rethinkdbDatabase).TableCreate("templates").Exec(session)
r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("name").Exec(session)
r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("version").Exec(session)
r.Db(*rethinkdbDatabase).TableCreate("hub_state").Exec(session)
r.Db(*rethinkdbDatabase).Table("hub_state").IndexCreate("time").Exec(session)
}
up := strings.Split(*usernames, ",")
pp := strings.Split(*passwords, ",")
if len(up) != len(pp) {
log.Fatal("length of usernames and passwords is different")
}
if *enableSender {
go initTemplates()
for i, username := range up {
go initSender(username, pp[i])
}
}
if *enableHub {
change := make(chan struct{})
go initChurner(change)
go initHub(change)
}
select {}
}
示例10: BenchmarkDelete
func BenchmarkDelete(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").DeleteAt(
gorethink.Expr(gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").IndexesOf(key2find).AtIndex(0)),
).Run(session)
if err != nil {
b.Log(err)
b.Fail()
}
}
}
示例11: InitTable
func InitTable(s *r.Session, databaseName string, tableName string) error {
exists, err := ListContains(s, r.Db(databaseName).TableList(), tableName)
if err != nil {
return err
}
if !exists {
_, err = r.Db(databaseName).TableCreate(tableName).RunWrite(s)
if err != nil {
return err
}
}
return nil
}
示例12: main
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var response []interface{}
var session *r.Session
session, err := r.Connect(map[string]interface{}{
"address": "localhost:28015",
"database": "test",
"maxIdle": 10,
"idleTimeout": time.Second * 10,
})
if err != nil {
panic(err)
}
r.Db("test").TableCreate("test").Exec(session)
objects := []interface{}{}
for i := 3000; i < 3001; i++ {
www := FetchURL("http://www.cnn.com")
row := map[string]interface{}{"id": i, "g1": 6771, "www": www}
objects = append(objects, row)
}
log.Println(objects)
query := r.Db("test").Table("Table1").Insert(objects)
_, err = query.Run(session)
if err != nil {
panic(err)
}
query = r.Db("test").Table("Table1").OrderBy("id")
rows, err := query.Run(session)
if err != nil {
panic(err)
}
err = rows.ScanAll(&response)
log.Println(response)
}
示例13: expireNotifications
// scan the notifications db for checks older than our expiration time and remove them. This will allow them be re-alerted on.
func (sm *Manager) expireNotifications() {
log.Debugln("Expiring notifications")
err := r.Db(STALKERDB).Table("notifications").Filter(r.Row.Field("ts").Lt(time.Now().Unix() - sm.notificationExpiration)).Delete().Exec(sm.rsess)
if err != nil {
log.Errorln("Error deleting expired notifications:", err.Error())
}
}
示例14: createTable
func createTable(name string) error {
ensureSession()
// TODO return the object in a useable format instead of just the error
// object, error
_, err := r.Db(*db).TableCreate(name).Run(session)
return err
}
示例15: InitDB
func InitDB() *rethink.Session {
session, err := rethink.Connect(map[string]interface{}{
"address": "localhost:8080", //os.Getenv("RETHINKDB_URL"),
"database": "test",
"maxIdle": 10,
"idleTimeout": time.Second * 10,
})
if err != nil {
log.Println(err)
}
err = rethink.DbCreate("test").Exec(session)
if err != nil {
log.Println(err)
}
_, err = rethink.Db("test").TableCreate("articles").RunWrite(session)
if err != nil {
log.Println(err)
}
return session
}