本文整理汇总了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
}
示例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)
}
}
示例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
}
示例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
}
示例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)
}
}
示例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)
}
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}