當前位置: 首頁>>代碼示例>>Golang>>正文


Golang mongo.Collection類代碼示例

本文整理匯總了Golang中github.com/juju/juju/mongo.Collection的典型用法代碼示例。如果您正苦於以下問題:Golang Collection類的具體用法?Golang Collection怎麽用?Golang Collection使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Collection類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: readSkews

// readSkews reads all clock data for the client's namespace.
func (client *client) readSkews(collection mongo.Collection) (map[string]Skew, error) {

	// Read the clock document, recording the time before and after completion.
	readBefore := client.config.Clock.Now()
	var clockDoc clockDoc
	if err := collection.FindId(client.clockDocId()).One(&clockDoc); err != nil {
		return nil, errors.Trace(err)
	}
	readAfter := client.config.Clock.Now()
	if err := clockDoc.validate(); err != nil {
		return nil, errors.Annotatef(err, "corrupt clock document")
	}

	// Create skew entries for each known writer...
	skews, err := clockDoc.skews(readBefore, readAfter)
	if err != nil {
		return nil, errors.Trace(err)
	}

	// If a writer was previously known to us, and has not written since last
	// time we read, we should keep the original skew, which is more accurate.
	for writer, skew := range client.skews {
		if skews[writer].LastWrite == skew.LastWrite {
			skews[writer] = skew
		}
	}

	// ...and overwrite our own with a zero skew, which will DTRT (assuming
	// nobody's reusing client ids across machines with different clocks,
	// which *should* never happen).
	skews[client.config.Id] = Skew{}
	return skews, nil
}
開發者ID:Pankov404,項目名稱:juju,代碼行數:34,代碼來源:client.go

示例2: exists

// exists returns whether the identified refcount doc exists.
func (nsRefcounts_) exists(coll mongo.Collection, key string) (bool, error) {
	count, err := coll.FindId(key).Count()
	if err != nil {
		return false, errors.Trace(err)
	}
	return count != 0, nil
}
開發者ID:bac,項目名稱:juju,代碼行數:8,代碼來源:refcounts_ns.go

示例3: deleteOldPlaceholderCharmsOps

// deleteOldPlaceholderCharmsOps returns the txn ops required to delete all placeholder charm
// records older than the specified charm URL.
func deleteOldPlaceholderCharmsOps(st *State, charms mongo.Collection, curl *charm.URL) ([]txn.Op, error) {
	// Get a regex with the charm URL and no revision.
	noRevURL := curl.WithRevision(-1)
	curlRegex := "^" + regexp.QuoteMeta(st.docID(noRevURL.String()))

	var docs []charmDoc
	query := bson.D{{"_id", bson.D{{"$regex", curlRegex}}}, {"placeholder", true}}
	err := charms.Find(query).Select(bson.D{{"_id", 1}, {"url", 1}}).All(&docs)
	if err != nil {
		return nil, errors.Trace(err)
	}
	var ops []txn.Op
	for _, doc := range docs {
		if doc.URL.Revision >= curl.Revision {
			continue
		}
		ops = append(ops, txn.Op{
			C:      charmsC,
			Id:     doc.DocID,
			Assert: stillPlaceholder,
			Remove: true,
		})
	}
	return ops, nil
}
開發者ID:makyo,項目名稱:juju,代碼行數:27,代碼來源:charm.go

示例4: StrictIncRefOp

// StrictIncRefOp returns a txn.Op that increments the value of a
// refcount doc, or returns an error if it does not exist.
func (ns nsRefcounts_) StrictIncRefOp(coll mongo.Collection, key string, n int) (txn.Op, error) {
	if exists, err := ns.exists(coll, key); err != nil {
		return txn.Op{}, errors.Trace(err)
	} else if !exists {
		return txn.Op{}, errors.New("does not exist")
	}
	return ns.JustIncRefOp(coll.Name(), key, n), nil
}
開發者ID:bac,項目名稱:juju,代碼行數:10,代碼來源:refcounts_ns.go

示例5: getEntitiesWithStatuses

// getEntitiesWithStatuses returns the ids for all entities that
// have history entries
func getEntitiesWithStatuses(coll mongo.Collection) ([]string, error) {
	var globalKeys []string
	err := coll.Find(nil).Distinct("globalkey", &globalKeys)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return globalKeys, nil
}
開發者ID:claudiu-coblis,項目名稱:juju,代碼行數:10,代碼來源:status.go

示例6: getEntitiesWithStatuses

// getEntitiesWithStatuses returns the ids for all entities that
// have history entries
func getEntitiesWithStatuses(coll mongo.Collection) ([]string, error) {
	var entityKeys []string
	err := coll.Find(nil).Distinct("entityid", &entityKeys)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return entityKeys, nil
}
開發者ID:vonwenm,項目名稱:juju,代碼行數:10,代碼來源:status.go

示例7: AliveDecRefOp

// AliveDecRefOp returns a txn.Op that decrements the value of a
// refcount doc, or an error if the doc does not exist or the count
// would go below 0.
func (ns nsRefcounts_) AliveDecRefOp(coll mongo.Collection, key string) (txn.Op, error) {
	if refcount, err := ns.read(coll, key); err != nil {
		return txn.Op{}, errors.Trace(err)
	} else if refcount < 1 {
		return txn.Op{}, errors.New("cannot decRef below 0")
	}
	return ns.justDecRefOp(coll.Name(), key, 0), nil
}
開發者ID:bac,項目名稱:juju,代碼行數:11,代碼來源:refcounts_ns.go

示例8: StrictCreateOp

// StrictCreateOp returns a txn.Op that creates a refcount document as
// configured, or an error if the document already exists.
func (ns nsRefcounts_) StrictCreateOp(coll mongo.Collection, key string, value int) (txn.Op, error) {
	if exists, err := ns.exists(coll, key); err != nil {
		return txn.Op{}, errors.Trace(err)
	} else if exists {
		return txn.Op{}, errors.New("refcount already exists")
	}
	return ns.JustCreateOp(coll.Name(), key, value), nil
}
開發者ID:bac,項目名稱:juju,代碼行數:10,代碼來源:refcounts_ns.go

示例9: LazyCreateOp

// LazyCreateOp returns a txn.Op that creates a refcount document; or
// false if the document already exists.
func (ns nsRefcounts_) LazyCreateOp(coll mongo.Collection, key string) (txn.Op, bool, error) {
	if exists, err := ns.exists(coll, key); err != nil {
		return txn.Op{}, false, errors.Trace(err)
	} else if exists {
		return txn.Op{}, false, nil
	}
	return ns.JustCreateOp(coll.Name(), key, 0), true, nil
}
開發者ID:bac,項目名稱:juju,代碼行數:10,代碼來源:refcounts_ns.go

示例10: read

// read returns the value stored in the identified refcount doc.
func (nsRefcounts_) read(coll mongo.Collection, key string) (int, error) {
	var doc refcountDoc
	if err := coll.FindId(key).One(&doc); err == mgo.ErrNotFound {
		return 0, errors.NotFoundf("refcount %q", key)
	} else if err != nil {
		return 0, errors.Trace(err)
	}
	return doc.RefCount, nil
}
開發者ID:bac,項目名稱:juju,代碼行數:10,代碼來源:refcounts_ns.go

示例11: newStateCollection

func newStateCollection(collection mongo.Collection, envUUID string) mongo.Collection {
	if multiEnvCollections.Contains(collection.Name()) {
		return &envStateCollection{
			WriteCollection: collection.Writeable(),
			envUUID:         envUUID,
		}
	}
	return collection
}
開發者ID:Pankov404,項目名稱:juju,代碼行數:9,代碼來源:collections.go

示例12: RemoveOp

// RemoveOp returns a txn.Op that removes a refcount doc so long as its
// refcount is the supplied value, or an error.
func (ns nsRefcounts_) RemoveOp(coll mongo.Collection, key string, value int) (txn.Op, error) {
	refcount, err := ns.read(coll, key)
	if err != nil {
		return txn.Op{}, errors.Trace(err)
	}
	if refcount != value {
		return txn.Op{}, errRefcountChanged
	}
	return ns.JustRemoveOp(coll.Name(), key, value), nil
}
開發者ID:bac,項目名稱:juju,代碼行數:12,代碼來源:refcounts_ns.go

示例13: getOldestTimeToKeep

// getOldestTimeToKeep returns the create time for the oldest
// status log to be kept.
func getOldestTimeToKeep(coll mongo.Collection, globalKey string, size int) (int64, bool, error) {
	result := historicalStatusDoc{}
	err := coll.Find(bson.D{{"globalkey", globalKey}}).Sort("-updated").Skip(size - 1).One(&result)
	if err == mgo.ErrNotFound {
		return -1, false, nil
	}
	if err != nil {
		return -1, false, errors.Trace(err)
	}
	return result.Updated, true, nil

}
開發者ID:claudiu-coblis,項目名稱:juju,代碼行數:14,代碼來源:status.go

示例14: DyingDecRefOp

// DyingDecRefOp returns a txn.Op that decrements the value of a
// refcount doc and deletes it if the count reaches 0; if the Op will
// cause a delete, the bool result will be true. It will return an error
// if the doc does not exist or the count would go below 0.
func (ns nsRefcounts_) DyingDecRefOp(coll mongo.Collection, key string) (txn.Op, bool, error) {
	refcount, err := ns.read(coll, key)
	if err != nil {
		return txn.Op{}, false, errors.Trace(err)
	}
	if refcount < 1 {
		return txn.Op{}, false, errors.New("cannot decRef below 0")
	} else if refcount > 1 {
		return ns.justDecRefOp(coll.Name(), key, 1), false, nil
	}
	return ns.JustRemoveOp(coll.Name(), key, 1), true, nil
}
開發者ID:bac,項目名稱:juju,代碼行數:16,代碼來源:refcounts_ns.go

示例15: setStatusOp

// setStatusOp returns a txn.Op that updates the status of the
// identified payload. If the payload doesn't exist, it returns
// errAlreadyRemoved.
func (nsPayloads_) setStatusOp(payloads mongo.Collection, docID string, status string) (txn.Op, error) {
	count, err := payloads.FindId(docID).Count()
	if err != nil {
		return txn.Op{}, errors.Trace(err)
	} else if count == 0 {
		return txn.Op{}, errAlreadyRemoved
	}
	return txn.Op{
		C:      payloads.Name(),
		Id:     docID,
		Assert: txn.DocExists,
		Update: bson.D{{"$set", bson.D{{"state", status}}}},
	}, nil
}
開發者ID:bac,項目名稱:juju,代碼行數:17,代碼來源:payloads_ns.go


注:本文中的github.com/juju/juju/mongo.Collection類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。