Fetch the repository succeeded.
package entitystoragemongodb
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"io"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/storage/storage_common"
)
const (
_DEFAULT_DB_NAME = "goworld"
)
var (
db *mgo.Database
)
type mongoDBEntityStorge struct {
db *mgo.Database
}
// OpenMongoDB opens mongodb as entity storage
func OpenMongoDB(url string, dbname string) (storagecommon.EntityStorage, error) {
gwlog.Debugf("Connecting MongoDB ...")
session, err := mgo.Dial(url)
if err != nil {
return nil, err
}
session.SetMode(mgo.Monotonic, true)
if dbname == "" {
// if db is not specified, use default
dbname = _DEFAULT_DB_NAME
}
db = session.DB(dbname)
return &mongoDBEntityStorge{
db: db,
}, nil
}
func (es *mongoDBEntityStorge) Write(typeName string, entityID common.EntityID, data interface{}) error {
col := es.getCollection(typeName)
_, err := col.UpsertId(entityID, bson.M{
"data": data,
})
return err
}
func (es *mongoDBEntityStorge) Read(typeName string, entityID common.EntityID) (interface{}, error) {
col := es.getCollection(typeName)
q := col.FindId(entityID)
var doc bson.M
err := q.One(&doc)
if err != nil {
return nil, err
}
return map[string]interface{}(doc["data"].(bson.M)), nil
}
//func (es *mongoDBEntityStorge) convertM2Map(m bson.M) map[string]interface{} {
// ma := map[string]interface{}(m)
// for k, v := range ma {
// if m, ok := v.(bson.M); ok {
// ma[k] = es.convertM2Map(m)
// }
// }
// return ma
//}
func (es *mongoDBEntityStorge) getCollection(typeName string) *mgo.Collection {
return es.db.C(typeName)
}
func (es *mongoDBEntityStorge) List(typeName string) ([]common.EntityID, error) {
col := es.getCollection(typeName)
var docs []bson.M
err := col.Find(nil).Select(bson.M{"_id": 1}).All(&docs)
if err != nil {
return nil, err
}
entityIDs := make([]common.EntityID, len(docs))
for i, doc := range docs {
entityIDs[i] = common.EntityID(doc["_id"].(string))
}
return entityIDs, nil
}
func (es *mongoDBEntityStorge) Exists(typeName string, entityID common.EntityID) (bool, error) {
col := es.getCollection(typeName)
query := col.FindId(entityID)
var doc bson.M
err := query.One(&doc)
if err == nil {
// doc found
return true, nil
} else if err == mgo.ErrNotFound {
return false, nil
} else {
return false, err
}
}
func (es *mongoDBEntityStorge) Close() {
es.db.Session.Close()
}
func (es *mongoDBEntityStorge) IsEOF(err error) bool {
return err == io.EOF || err == io.ErrUnexpectedEOF
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。