當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。