本文整理匯總了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
}
示例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)
}
}
示例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())
}
}
示例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
}
示例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()
}
示例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()
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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,
}
}
示例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)
}
}
示例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
}