标签:创建 nbsp col com abort commit ble 建立 emd
// Create a sample struct type Person struct { Email string Name string Age int } // 创建数据库的 DBSchema , 它包括多个表结构: Tables map[string]*TableSchema
// 每个表结构是 TableSchema, 它定义对应的index :
// type TableSchema struct { // Name string // Indexes map[string]*IndexSchema // }
schema := &memdb.DBSchema{ Tables: map[string]*memdb.TableSchema{ "person": &memdb.TableSchema{ Name: "person", Indexes: map[string]*memdb.IndexSchema{ "id": &memdb.IndexSchema{ // 从代码来看,是基于 email 来建立 index Name: "id", Unique: true, Indexer: &memdb.StringFieldIndex{Field: "Email"}, }, }, }, }, } // 基于schema 创建数据库 db, err := memdb.NewMemDB(schema) if err != nil { panic(err) } // 创建写事务 txn := db.Txn(true) // 插入记录 p := &Person{"joe@aol.com", "Joe", 30} if err := txn.Insert("person", p); err != nil { panic(err) } // Commit txn.Commit() // 创建只读事务 txn = db.Txn(false) defer txn.Abort() // 返回第一个符合的记录 raw, err := txn.First("person", "id", "joe@aol.com") if err != nil { panic(err) } // Say hi! fmt.Printf("Hello %s!", raw.(*Person).Name)
标签:创建 nbsp col com abort commit ble 建立 emd
原文地址:http://www.cnblogs.com/liyiscuu/p/8012688.html