// getTxnRevno returns the transaction revision number of the
// given document id in the given collection. It is useful to enable
// a watcher.Watcher to be primed with the correct revision
// id.
968433
func getTxnRevno(coll mongo.Collection, id interface{}) (int64, error) {
	doc := struct {
		TxnRevno int64 `bson:"txn-revno"`
	}{}
	fields := bson.D{{"txn-revno", 1}}
	if err := coll.FindId(id).Select(fields).One(&doc); err == mgo.ErrNotFound {
		return -1, nil
	} else if err != nil {
		return 0, err
	}
	return doc.TxnRevno, nil
}
586640
func (c *appContext) getTxTicketInfo(w http.ResponseWriter, r *http.Request) {
	txid, err := m.GetTxIDCtx(r)
	if err != nil {
		http.Error(w, http.StatusText(422), 422)
		return
	}
	tinfo, err := c.AuxDataSource.GetTicketInfo(txid.String())
	if err != nil {
		err = fmt.Errorf("unable to get ticket info for tx %v: %v",
			txid, err)
		apiLog.Error(err)
		http.Error(w, err.Error(), 422)
		return
	}
	writeJSON(w, tinfo, c.getIndentQuery(r))
}
102233
func (s *sweeperStore) GetLastPublishedTx() (*wire.MsgTx, error) {
	var sweepTx *wire.MsgTx

	err := s.db.View(func(tx *bbolt.Tx) error {
		lastTxBucket := tx.Bucket(lastTxBucketKey)
		if lastTxBucket == nil {
			return errors.New("last tx bucket does not exist")
		}

		sweepTxRaw := lastTxBucket.Get(lastTxKey)
		if sweepTxRaw == nil {
			return nil
		}

		sweepTx = &wire.MsgTx{}
		txReader := bytes.NewReader(sweepTxRaw)
		if err := sweepTx.Deserialize(txReader); err != nil {
			return fmt.Errorf("tx deserialize: %v", err)
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return sweepTx, nil
}
696202
func (r *RepoStats) GetOrgRepos() int {
	if r == nil || r.OrgRepos == nil {
		return 0
	}
	return *r.OrgRepos
}
1306321
func (doc *Document) GetDocument() ([]byte, error) {
	if doc.Id != "" {
		return doc.getDocFromId()
	}

	return nil, errors.New("An id required to search for the document.")
}
731705
func RetrieveFundingTxByTxIn(ctx context.Context, db *sql.DB, txHash string, vinIndex uint32) (id uint64, tx string, err error) {
	err = db.QueryRowContext(ctx, internal.SelectFundingTxByTxIn, txHash, vinIndex).
		Scan(&id, &tx)
	return
}
248826
func (l *kvLedger) GetBlockByTxID(txID string) (*common.Block, error) {
	block, err := l.blockStore.RetrieveBlockByTxID(txID)
	l.blockAPIsRWLock.RLock()
	l.blockAPIsRWLock.RUnlock()
	return block, err
}
375275
func (r *Reader) getOriginalDoc(doc bson.M, c string, s *mgo.Session) (result bson.M, err error) {
	id, exists := doc["_id"]
	if !exists {
		return result, fmt.Errorf("can't get _id from document")
	}

	query := bson.M{}
	if f, ok := r.collectionFilters[c]; ok {
		query = bson.M(f)
	}
	query["_id"] = id

	err = s.DB("").C(c).Find(query).One(&result)
	if err != nil {
		err = fmt.Errorf("%s.%s %v %v", s.DB("").Name, c, id, err)
	}
	return
}
396289
func (g *Graveyard) ReapTxn(tx *memdb.Txn, idx uint64) error {
	// This does a full table scan since we currently can't index on a
	// numeric value. Since this is all in-memory and done infrequently
	// this pretty reasonable.
	stones, err := tx.Get("tombstones", "id")
	if err != nil {
		return fmt.Errorf("failed querying tombstones: %s", err)
	}

	// Find eligible tombstones.
	var objs []interface{}
	for stone := stones.Next(); stone != nil; stone = stones.Next() {
		if stone.(*Tombstone).Index <= idx {
			objs = append(objs, stone)
		}
	}

	// Delete the tombstones in a separate loop so we don't trash the
	// iterator.
	for _, obj := range objs {
		if err := tx.Delete("tombstones", obj); err != nil {
			return fmt.Errorf("failed deleting tombstone: %s", err)
		}
	}
	return nil
}
231604
func (lc *LightningChannel) getSignedCommitTx() (*wire.MsgTx, error) {
	// Fetch the current commitment transaction, along with their signature
	// for the transaction.
	localCommit := lc.channelState.LocalCommitment
	commitTx := localCommit.CommitTx
	theirSig := append(localCommit.CommitSig, byte(txscript.SigHashAll))

	// With this, we then generate the full witness so the caller can
	// broadcast a fully signed transaction.
	lc.signDesc.SigHashes = txscript.NewTxSigHashes(commitTx)
	ourSigRaw, err := lc.Signer.SignOutputRaw(commitTx, lc.signDesc)
	if err != nil {
		return nil, err
	}

	ourSig := append(ourSigRaw, byte(txscript.SigHashAll))

	// With the final signature generated, create the witness stack
	// required to spend from the multi-sig output.
	ourKey := lc.localChanCfg.MultiSigKey.PubKey.SerializeCompressed()
	theirKey := lc.remoteChanCfg.MultiSigKey.PubKey.SerializeCompressed()

	commitTx.TxIn[0].Witness = input.SpendMultiSig(
		lc.signDesc.WitnessScript, ourKey,
		ourSig, theirKey, theirSig,
	)

	return commitTx, nil
}
783030
func (h *QuarantineXattrHandler) Getxattr(ctx context.Context,
	req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) (err error) {
	ctx = h.folder.fs.config.MaybeStartTrace(ctx, "QuarantineXattrHandler.Getxattr",
		fmt.Sprintf("%s %s", h.node.GetBasename(), req.Name))
	defer func() { h.folder.fs.config.MaybeFinishTrace(ctx, err) }()

	h.folder.fs.log.CDebugf(ctx,
		"QuarantineXattrHandler Getxattr %s %s", h.node.GetBasename(), req.Name)
	defer func() { err = h.folder.processError(ctx, libkbfs.ReadMode, err) }()

	if req.Name != quarantineXattrName {
		// The request is not about quarantine. Let the OS fallback to ._ file
		// based method.
		return fuse.ENOTSUP
	}

	xattr, err := h.folder.fs.config.XattrStore().GetXattr(
		ctx, h.node.GetBlockID(), libkbfs.XattrAppleQuarantine)
	switch errors.Cause(err) {
	case nil:
		if len(xattr) == 0 {
			return fuse.ENOATTR
		}
		resp.Xattr = xattr
		return nil
	case ldberrors.ErrNotFound:
		// We don't have an Xattr value stored locally, so just use a
		// quarantine value.

		// Stat on the node to get the Mtime.

		// This fits in situation 1 as described in
		// libkbfs/delayed_cancellation.go
		if err = libcontext.EnableDelayedCancellationWithGracePeriod(ctx,
			h.folder.fs.config.DelayedCancellationGracePeriod()); err != nil {
			return err
		}
		de, err := h.folder.fs.config.KBFSOps().Stat(ctx, h.node)
		if err != nil {
			if _, ok := err.(idutil.NoSuchNameError); ok {
				// The node is not found, so just return ENOTSUP
				return fuse.ENOTSUP
			}
			return err
		}

		resp.Xattr = makeQuarantine(time.Unix(0, de.Mtime))
		return nil
	default:
		return err
	}
}
1272119
func getOrGenerateRootKeyAndInitRepo(notaryRepo client.Repository) error {
	rootKey, err := getOrGenerateNotaryKey(notaryRepo, data.CanonicalRootRole)
	if err != nil {
		return err
	}
	return notaryRepo.Initialize([]string{rootKey.ID()}, data.CanonicalSnapshotRole)
}
793354
func RetrieveSpendingTxByVinID(ctx context.Context, db *sql.DB, vinDbID uint64) (tx string,
	vinIndex uint32, tree int8, err error) {
	err = db.QueryRowContext(ctx, internal.SelectSpendingTxByVinID, vinDbID).
		Scan(&tx, &vinIndex, &tree)
	return
}
151540
func (t *Team) GetReposCount() int {
	if t == nil || t.ReposCount == nil {
		return 0
	}
	return *t.ReposCount
}
35397
func (s *Store) getNodeCheckTxn(tx *memdb.Txn, nodeName string, checkID types.CheckID) (uint64, *structs.HealthCheck, error) {
	// Get the table index.
	idx := maxIndexTxn(tx, "checks")

	// Return the check.
	check, err := tx.First("checks", "id", nodeName, string(checkID))
	if err != nil {
		return 0, nil, fmt.Errorf("failed check lookup: %s", err)
	}

	if check != nil {
		return idx, check.(*structs.HealthCheck), nil
	}
	return idx, nil, nil
}
229951
func (ctx *bytesKeyIterator) GetNext() (key string, rev int64, stop bool) {
	if ctx.index >= ctx.len {
		return "", 0, true
	}

	key = string(ctx.resp.Kvs[ctx.index].Key)
	rev = ctx.resp.Kvs[ctx.index].ModRevision
	ctx.index++

	return key, rev, false
}
463795
func (db *BytesConnectionRedis) NewTxn() keyval.BytesTxn {
	if db.closed {
		db.Error("NewTxn() called on a closed connection")
		return nil
	}
	db.Debug("NewTxn()")

	return &Txn{db: db, ops: []op{}, addPrefix: nil}
}