當前位置: 首頁>>代碼示例>>Golang>>正文


Golang db.OpenDB函數代碼示例

本文整理匯總了Golang中github.com/HouzuoGuo/tiedot/db.OpenDB函數的典型用法代碼示例。如果您正苦於以下問題:Golang OpenDB函數的具體用法?Golang OpenDB怎麽用?Golang OpenDB使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了OpenDB函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GetDB

func GetDB() *db.DB {
	myDB, err := db.OpenDB(tiedotmartini2.DATABASE_DIR)
	if err != nil {
		panic(err)
	}
	return myDB
}
開發者ID:jmptrader,項目名稱:TiedotMartini2,代碼行數:7,代碼來源:base.go

示例2: Start

func Start(dir string, port int, tlsCrt, tlsKey, jwtPubKey, jwtPrivateKey string) {
	var err error
	HttpDB, err = db.OpenDB(dir)
	if err != nil {
		panic(err)
	}

	// These endpoints are always available and do not require JWT auth
	http.HandleFunc("/", Welcome)
	http.HandleFunc("/version", Version)
	http.HandleFunc("/memstats", MemStats)

	if jwtPrivateKey != "" {
		// JWT support
		ServeJWTEnabledEndpoints(jwtPubKey, jwtPrivateKey)
	} else {
		// No JWT
		ServeEndpoints()
	}

	if tlsCrt != "" {
		tdlog.Noticef("Will listen on all interfaces (HTTPS), port %d.", port)
		if err := http.ListenAndServeTLS(fmt.Sprintf(":%d", port), tlsCrt, tlsKey, nil); err != nil {
			tdlog.Panicf("Failed to start HTTPS service - %s", err)
		}
	} else {
		tdlog.Noticef("Will listen on all interfaces (HTTP), port %d.", port)
		http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
	}
}
開發者ID:Lanzafame,項目名稱:tiedot,代碼行數:30,代碼來源:srv.go

示例3: initialize

func initialize() {
	// remove temp files
	os.RemoveAll(DBdir)

	// open db
	d, err := db.OpenDB(DBdir)

	if err != nil {
		panic(err)
	}

	// create collection
	if err := d.Create("Entries"); err != nil {
		panic(err)
	}

	// collection instance
	docEntries := d.Use("Entries")

	// dummy data
	entries := Entries{
		&Entry{1, time.Now(), "Entry 1", "First Entry!"},
		&Entry{2, time.Now(), "Golang", "Go language is awesome"},
	}

	// insert each
	for _, entry := range entries {
		docEntries.Insert(structs.New(entry).Map())
	}
}
開發者ID:jancarloviray,項目名稱:go-adventures,代碼行數:30,代碼來源:main.go

示例4: initDB

// Setup the database connection
func initDB() *db.Col {
	// (Create if not exist) open a database
	myDB, err := db.OpenDB("database")
	if err != nil {
		log.Println("Opening existing database")
	} else {
		log.Println("Creating new database")
	}

	// Create two collections: Feeds and Votes
	if err := myDB.Create("Users"); err != nil {
		log.Println("Using existing users database")
	} else {
		log.Println("Using fresh new users database")
	}

	users := myDB.Use("Users")
	if err := users.Index([]string{"Email"}); err != nil {
		log.Println("Using existing email index on users")
	} else {
		log.Println("Creating new index on email field of users database")
	}

	// Add a random user or 2
	//	users.Insert(dbRow{"Email": "[email protected]", "Password": "abc123"})

	return users
}
開發者ID:steveoc64,項目名稱:ksm,代碼行數:29,代碼來源:rego.go

示例5: main

func main() {
	// It is very important to initialize random number generator seed!
	rand.Seed(time.Now().UTC().UnixNano())

	jsondb, err := db.OpenDB(dbname)
	if err != nil {
		panic(err)
	}
	defer jsondb.Close()

	if _, err = os.Stat(filepath.Join(dbname, clname)); os.IsNotExist(err) {
		if err = jsondb.Create(clname); err != nil {
			panic(err)
		}
		/*
			userscl = jsondb.Use(clname)
			if err = userscl.Index([]string{"courseCode"}); err != nil {
				panic(err)
			}
		*/
	}

	if _, err = os.Stat(filepath.Join(dbname, qlname)); os.IsNotExist(err) {
		if err = jsondb.Create(qlname); err != nil {
			panic(err)
		}
		qlCl = jsondb.Use(qlname)
		if err := qlCl.Index([]string{"courseId"}); err != nil {
			panic(err)
		}
	}
	userscl = jsondb.Use(clname)
	qlCl = jsondb.Use(qlname)

	/*
	       needs to create index upon first time
	   	if err := qlCl.Index([]string{"courseId"}); err != nil {
	   		panic(err)
	   	}

	   	if err := userscl.Index([]string{"courseCode"}); err != nil {
	   		panic(err)
	   	}
	*/

	m := martini.Classic()
	m.Get("/users", GetUsers)
	m.Post("/users", CreateUser)
	m.Get("/users/:id", GetUser)
	m.Delete("/users/:id", DeleteUser)
	m.Put("/users/:id", UpdateUser)
	m.Get("/courses", GetCourses)
	m.Get("/courses/:id", SearchCourse)
	m.Post("/courses", CreateCourse)
	m.Post("/questionlists", CreateQuestionList)
	m.Get("/questionLists", SearchQuestionListByCourseId)
	m.Get("/questionLists/:id", SearchQuestionList)
	m.Post("/questionLists/:id", UpdateQuestionList)
	m.Run()
}
開發者ID:rleiwang,項目名稱:ars,代碼行數:60,代碼來源:server.go

示例6: main

func main() {
	// It is very important to initialize random number generator seed!
	rand.Seed(time.Now().UTC().UnixNano())

	jsondb, err := db.OpenDB(dbname)
	if err != nil {
		panic(err)
	}
	defer jsondb.Close()

	if _, err = os.Stat(filepath.Join(dbname, clname)); os.IsNotExist(err) {
		if err = jsondb.Create(clname); err != nil {
			panic(err)
		}
	}
	userscl = jsondb.Use(clname)

	userscl.Insert(map[string]interface{}{"name": "Course1", "status": "activ", "Age": 3})
	userscl.Insert(map[string]interface{}{"name": "Course 2", "status": "active", "Age": 3})
	userscl.Insert(map[string]interface{}{"name": "Course 3", "status": "golang.org", "Age": 3})

	m := martini.Classic()
	m.Get("/users", GetUsers)
	m.Post("/users", CreateUser)
	m.Get("/users/:id", GetUser)
	m.Delete("/users/:id", DeleteUser)
	m.Put("/users/:id", UpdateUser)
	m.Get("/courses", SearchCourse)
	m.Run()
}
開發者ID:rleiwang,項目名稱:go-ember,代碼行數:30,代碼來源:server.go

示例7: mkTmpDBAndCol

// Create a temporary database and collection for benchmark use.
func mkTmpDBAndCol(dbPath string, colName string) (*db.DB, *db.Col) {
	os.RemoveAll(dbPath)
	tmpDB, err := db.OpenDB(dbPath)
	if err != nil {
		panic(err)
	}
	if err = tmpDB.Create(colName); err != nil {
		panic(err)
	}
	return tmpDB, tmpDB.Use(colName)
}
開發者ID:marble58,項目名稱:tiedot,代碼行數:12,代碼來源:benchmark.go

示例8: NewDatabaseConnection

func NewDatabaseConnection(rootPath string) (database *db.DB) {
	dir := rootPath + "/data"
	database, err := db.OpenDB(dir)
	if err != nil {
		panic(err)
	}

	initDatabase(database)

	return
}
開發者ID:Term1nal,項目名稱:squircy2,代碼行數:11,代碼來源:data.go

示例9: mkTmpDBAndCol

// Create a temporary database and collection for benchmark use.
func mkTmpDBAndCol(dbPath string, colName string) (col *db.Col) {
	os.RemoveAll(dbPath)
	tmpDB, err := db.OpenDB(dbPath)
	if err != nil {
		panic(err)
	}
	tmpCol, err := db.OpenCol(tmpDB, colName)
	if err != nil {
		panic(err)
	}
	return tmpCol
}
開發者ID:huanshi,項目名稱:tiedot,代碼行數:13,代碼來源:benchmark.go

示例10: OpenDatabase

func (tdb *DBTiedot) OpenDatabase() {
	dir := "/tmp/seed-db"

	db, err := tiedot.OpenDB(dir)
	if err != nil {
		panic(err)
	}

	if err := db.Create("Users", 50); err != nil {
		fmt.Println("Collection Users already created.")
	}

	tdb.Db = db
}
開發者ID:jamesunger,項目名稱:seed,代碼行數:14,代碼來源:tiedot.go

示例11: OpenDB

func (tdb *DBTiedot) OpenDB(constr string) (interface{}, error) {
	dir := "/tmp/seed-db"

	db, err := tiedot.OpenDB(dir)
	if err != nil {
		return nil, err
	}

	//if err := db.Create("Users"); err != nil {
	//	fmt.Println("Collection Users already created.")
	//}

	tdb.Db = db
	return db, nil
}
開發者ID:jamesunger,項目名稱:seed,代碼行數:15,代碼來源:tiedot.go

示例12: NewDatabase

func NewDatabase(path string) (*Database, error) {

	d, err := db.OpenDB(path)

	if err != nil {
		return nil, err
	}

	d.Create("Projects")
	d.Create("Resources")

	return &Database{
		db: d,
	}, nil
}
開發者ID:kildevaeld,項目名稱:projects,代碼行數:15,代碼來源:_database.go

示例13: GetDB

func GetDB(path string) *DataStore {
	db, err := db.OpenDB(path)
	if err != nil {
		panic(err)
	}

	db.Create("Users", 2)
	users := db.Use("Users")

	users.Index([]string{"nick", "ip"})

	return &DataStore{
		url:   path,
		db:    db,
		users: users,
	}
}
開發者ID:voldyman,項目名稱:GILL,代碼行數:17,代碼來源:datastore.go

示例14: home

func home(w http.ResponseWriter, req *http.Request) {
	// local vars
	var query interface{}
	entries := Entries{}
	queryResults := make(map[int]struct{})

	// open database
	d, err := db.OpenDB(DBdir)

	if err != nil {
		panic(err)
	}

	// use collection
	docEntries := d.Use("Entries")

	// build query from json and convert to interface{}
	json.Unmarshal([]byte(`"all"`), &query)

	// execute query and pass results to queryResults
	if err := db.EvalQuery(query, docEntries, &queryResults); err != nil {
		panic(err)
	}

	// queryResults contains []int of IDs
	for id := range queryResults {
		entry := Entry{}

		readBack, _ := docEntries.Read(id)

		// map[string]interface{} TO struct hack

		j, _ := json.Marshal(readBack) // struct to json
		json.Unmarshal(j, &entry)      // json to actual type

		entries = append(entries, &entry)
	}

	// compile template with data
	if err := index.Execute(w, entries); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
開發者ID:jancarloviray,項目名稱:go-adventures,代碼行數:43,代碼來源:main.go

示例15: NewTieDb

func NewTieDb(dir string, maxDay int) *TieDb {
	mydb, err := db.OpenDB(dir)
	if err != nil {
		panic(err)
	}
	tdb := &TieDb{
		tdb:           mydb,
		RequestTable:  newTable(mydb, "req"),
		ResponseTable: newTable(mydb, "res"),
	}
	if maxDay > 0 {
		maxTime := time.Now().Unix() - int64(maxDay)*86400
		if tdb.RequestTable.Gc(maxTime) > 100 {
			tdb.RequestTable.Scrub()
		}
		if tdb.ResponseTable.Gc(maxTime) > 100 {
			tdb.ResponseTable.Scrub()
		}
	}
	return tdb
}
開發者ID:koolhazz,項目名稱:pproxy,代碼行數:21,代碼來源:kvdb.go


注:本文中的github.com/HouzuoGuo/tiedot/db.OpenDB函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。