当前位置: 首页>>代码示例>>Golang>>正文


Golang mgo.IsDup函数代码示例

本文整理汇总了Golang中labix/org/v2/mgo.IsDup函数的典型用法代码示例。如果您正苦于以下问题:Golang IsDup函数的具体用法?Golang IsDup怎么用?Golang IsDup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了IsDup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Upsert

// Upsert will insert a new Exception if nothing matching uniqueid exists
// Then, it will add to instances/traces of the Exception
func (exception *Exception) Upsert(trace *Trace, db *mgo.Database) (err error) {

	log.Printf("Upserting: %#v\n", exception)

	if err = db.C("exceptions").EnsureIndex(mgo.Index{Key: []string{"uniqueid"}, Unique: true}); err != nil {
		return
	}

	// First, we'll try to insert
	// Due to the unique constraint on UniqueID, this will fail if the record exists already
	err = db.C("exceptions").Insert(&exception)
	if err != nil && !mgo.IsDup(err) { // Swallow duplicate error
		return
	}

	// Now, we're going to push the instance to mark when this happened
	err = db.C("exceptions").Update(bson.M{"uniqueid": exception.UniqueId}, bson.M{
		"$push": bson.M{"instances": time.Now()},
	})

	if err != nil {
		return err
	}

	// Finally, we'll add the trace (for now, unlimited)
	err = db.C("exceptions").Update(bson.M{"uniqueid": exception.UniqueId}, bson.M{
		"$push": bson.M{"traces": &trace},
	})

	if err != nil {
		return err
	}

	return
}
开发者ID:hayesgm,项目名称:go-lurch,代码行数:37,代码来源:exception.go

示例2: Test_newUser

func Test_newUser(t *testing.T) {
	c, session, err := connect(users)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("Test without email")
	foo, err := newUser(session, "Foo", "password", "")
	if err != nil {
		t.Error(err)
	}
	t.Logf("Test with email")
	nemo1, err := newUser(session, "Nemo", "password", "[email protected]")
	if err != nil {
		t.Error(err)
	}
	t.Logf("Test duplicate user")
	nemo2, err := newUser(session, "Nemo", "password", "[email protected]")
	if !mgo.IsDup(err) {
		t.Error(err)
	}

	if err = c.Remove(foo); err != nil {
		t.Error(err)
	}
	if err = c.Remove(nemo1); err != nil {
		t.Error(err)
	}
	if err = c.Remove(nemo2); err != mgo.ErrNotFound {
		t.Error(err)
	}
}
开发者ID:corburn,项目名称:mgoblog,代码行数:32,代码来源:user_test.go

示例3: PlatformAdd

// PlatformAdd add a new platform to tsuru
func PlatformAdd(name string, args map[string]string, w io.Writer) error {
	var (
		provisioner provision.ExtensibleProvisioner
		ok          bool
	)
	if provisioner, ok = Provisioner.(provision.ExtensibleProvisioner); !ok {
		return errors.New("Provisioner is not extensible")
	}
	if name == "" {
		return errors.New("Platform name is required.")
	}
	p := Platform{Name: name}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	err = conn.Platforms().Insert(p)
	if err != nil {
		if mgo.IsDup(err) {
			return DuplicatePlatformError{}
		}
		return err
	}
	err = provisioner.PlatformAdd(name, args, w)
	if err != nil {
		db_err := conn.Platforms().RemoveId(p.Name)
		if db_err != nil {
			return fmt.Errorf("Caused by: %s and %s", err.Error(), db_err.Error())
		}
		return err
	}
	return nil
}
开发者ID:ningjh,项目名称:tsuru,代码行数:34,代码来源:platform.go

示例4: New

// New creates a representation of a git repository. It creates a Git
// repository using the "bare-dir" setting and saves repository's meta data in
// the database.
func New(name string, users []string, isPublic bool) (*Repository, error) {
	log.Debugf("Creating repository %q", name)
	r := &Repository{Name: name, Users: users, IsPublic: isPublic}
	if v, err := r.isValid(); !v {
		log.Errorf("repository.New: Invalid repository %q: %s", name, err)
		return r, err
	}
	if err := newBare(name); err != nil {
		log.Errorf("repository.New: Error creating bare repository for %q: %s", name, err)
		return r, err
	}
	barePath := barePath(name)
	if barePath != "" && isPublic {
		ioutil.WriteFile(barePath+"/git-daemon-export-ok", []byte(""), 0644)
		if f, err := fs.Filesystem().Create(barePath + "/git-daemon-export-ok"); err == nil {
			f.Close()
		}
	}
	conn, err := db.Conn()
	if err != nil {
		return nil, err
	}
	defer conn.Close()
	err = conn.Repository().Insert(&r)
	if mgo.IsDup(err) {
		log.Errorf("repository.New: Duplicate repository %q", name)
		return r, fmt.Errorf("A repository with this name already exists.")
	}
	return r, err
}
开发者ID:rochacon,项目名称:gandalf,代码行数:33,代码来源:repository.go

示例5: PutDistance

func (c *MongoCache) PutDistance(route Route, distance uint64) {
	s := c.session.Clone()
	defer s.Close()

	err := s.DB("").C("routes").Insert(NewCachedRoute(route, distance))
	if err != nil && !mgo.IsDup(err) {
		log.Println("MongoCache: PUT error -> ", err)
	}
}
开发者ID:Bowbaq,项目名称:bikage,代码行数:9,代码来源:mongo_cache.go

示例6: PutTrip

func (c *MongoCache) PutTrip(username string, trip Trip) {
	s := c.session.Clone()
	defer s.Close()

	err := s.DB("").C("trips").Insert(NewCachedTrip(username, trip))
	if err != nil && !mgo.IsDup(err) {
		log.Println("MongoCache: PUT error -> ", err)
	}
}
开发者ID:Bowbaq,项目名称:bikage,代码行数:9,代码来源:mongo_cache.go

示例7: Add

// Add inserts new package and ignores if package exist already
func (p *Package) Add(pr *PackageRow) (*PackageRow, error) {
	pr.Created = time.Now()
	pr.Id = bson.NewObjectId()
	err := p.coll.Insert(pr)
	if err != nil && !mgo.IsDup(err) {
		return nil, err
	}
	return pr, nil
}
开发者ID:gophergala2016,项目名称:gobench,代码行数:10,代码来源:package.go

示例8: insertUser

// insertUser inserts User to database.
// the user with that email exist. Note that indexes musted create fo MongoDB.
func (m *MgoUserManager) insertUser(u *auth.User) error {
	err := m.UserColl.Insert(u)
	if err != nil {
		if mgo.IsDup(err) {
			return auth.ErrDuplicateEmail
		}
		return err
	}

	return nil
}
开发者ID:kidstuff,项目名称:WebAuth,代码行数:13,代码来源:account.go

示例9: Post

func (rs *controller) Post(w http.ResponseWriter, request *http.Request, p httprouter.Params) {
	defer request.Body.Close()

	data, err := ioutil.ReadAll(request.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	var rule Rule
	err = json.Unmarshal(data, &rule)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	err = Save(&rule)
	if err != nil {
		if mgo.IsDup(err) {
			http.Error(w, err.Error(), http.StatusBadRequest)
		} else {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		return
	}
	axn, err := rule.GetAction()
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	actions.Add(axn)
	req, err := http.NewRequest("POST", config.RuleEndpoint(), strings.NewReader(rule.EPL))
	if err != nil {
		mylog.Alert("rule controller POST", err)
	}
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("rule controller POST", err, resp)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if resp.StatusCode/100 != 2 {
		mylog.Alert("rule controller POST", resp.Status)
		http.Error(w, resp.Status, http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusCreated)
	dataBack, err := json.Marshal(rule)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Write(dataBack)

}
开发者ID:robertocs,项目名称:cepiot,代码行数:53,代码来源:rule_controller.go

示例10: Register

// Register adds a new node to the scheduler, registering for use in
// the given team. The team parameter is optional, when set to "", the node
// will be used as a fallback node.
func (segregatedScheduler) Register(params map[string]string) error {
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	node := node{ID: params["ID"], Address: params["address"], Teams: []string{params["team"]}}
	err = conn.Collection(schedulerCollection).Insert(node)
	if mgo.IsDup(err) {
		return errNodeAlreadyRegister
	}
	return err
}
开发者ID:rpeterson,项目名称:tsuru,代码行数:16,代码来源:scheduler.go

示例11: addNodeToScheduler

// AddNodeToScheduler adds a new node to the scheduler, registering for use in
// the given team. The team parameter is optional, when set to "", the node
// will be used as a fallback node.
func addNodeToScheduler(n cluster.Node, team string) error {
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	node := node{ID: n.ID, Address: n.Address, Team: team}
	err = conn.Collection(schedulerCollection).Insert(node)
	if mgo.IsDup(err) {
		return errNodeAlreadyRegister
	}
	return err
}
开发者ID:nemx,项目名称:tsuru,代码行数:16,代码来源:scheduler.go

示例12: insertUser

func (ctx *MgoUserCtx) insertUser(u *Account, notif, app bool) error {
	err := ctx.userColl.Insert(u)
	if err != nil {
		if mgo.IsDup(err) {
			return membership.ErrDuplicateEmail
		}
		return err
	}

	if notif {
		return ctx.notifer.AccountAdded(u)
	}
	return nil
}
开发者ID:kidstuff,项目名称:mtoy,代码行数:14,代码来源:mgoauth.go

示例13: insertUser

func (a *AuthMongoDBCtx) insertUser(u *User, notif, app bool) error {
	err := a.userColl.Insert(u)
	if err != nil {
		if mgo.IsDup(err) {
			return ErrDuplicateEmail
		}
		return err
	}

	if notif {
		return a.notifer.AccountAdded(u.Email, app)
	}
	return nil
}
开发者ID:openvn,项目名称:toys,代码行数:14,代码来源:authmgo.go

示例14: New

// New creates a representation of a git repository. It creates a Git
// repository using the "bare-dir" setting and saves repository's meta data in
// the database.
func New(name string, users []string, isPublic bool) (*Repository, error) {
	r := &Repository{Name: name, Users: users, IsPublic: isPublic}
	if v, err := r.isValid(); !v {
		return r, err
	}
	if err := newBare(name); err != nil {
		return r, err
	}
	err := db.Session.Repository().Insert(&r)
	if mgo.IsDup(err) {
		return r, fmt.Errorf("A repository with this name already exists.")
	}
	return r, err
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:17,代码来源:repository.go

示例15: PlatformAdd

// PlatformAdd add a new platform to tsuru
func PlatformAdd(name string, file string) error {
	p := Platform{Name: name}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	err = conn.Platforms().Insert(p)
	if err != nil {
		if mgo.IsDup(err) {
			return DuplicatePlatformError{}
		}
		return err
	}
	err = Provisioner.PlatformAdd(name, file)
	return nil
}
开发者ID:renanoliveira,项目名称:tsuru,代码行数:17,代码来源:platform.go


注:本文中的labix/org/v2/mgo.IsDup函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。