// mungeBsonDUpdate modifies a txn.Op's Update field values expressed
// as a bson.D and attempts to make it multi-model safe.
//
// Currently, only $set operations are munged.
342725
func (r *multiModelRunner) mungeBsonDUpdate(updateDoc bson.D) (bson.D, error) {
	outDoc := make(bson.D, 0, len(updateDoc))
	for _, elem := range updateDoc {
		if elem.Name == "$set" {
			newSetDoc, err := mungeDocForMultiModel(elem.Value, r.modelUUID, 0)
			if err != nil {
				return nil, errors.Trace(err)
			}
			elem = bson.DocElem{elem.Name, newSetDoc}
		}
		outDoc = append(outDoc, elem)
	}
	return outDoc, nil
}
411653
func (s *Store) TxUpdateMatching(tx *bolt.Tx, dataType interface{}, query *Query, update func(record interface{}) error) error {
	return updateQuery(tx, dataType, query, update)
}
1007840
func (api *DatabaseAPI) FilterMultiBy(key string, value interface{}) *DatabaseAPI {
	api.filterBy(key, value, true)
	return api
}
1361846
func (c *Connector) MultiUpsert(ctx context.Context, ei *dosa.EntityInfo, values []map[string]dosa.FieldValue) ([]error, error) {
	// Note we do not lock here. This is representative of the behavior one would see in a deployed environment
	var errs []error
	for _, v := range values {
		errs = append(errs, c.Upsert(ctx, ei, v))
	}

	return errs, nil
}
450983
func (txops txOps) upsert(k compositeKey, val []byte) {
	keyops := txops.getOrCreateKeyEntry(k)
	keyops.flag += upsertVal
	keyops.value = val
}
935318
func (b *storageDBWrapper) txnOpBase(id string) txn.Op {
	op := txn.Op{
		C:  b.metaColl.Name,
		Id: id,
	}
	return op
}
94244
func (pool *TxPool) AddRemote(tx *types.Transaction) error {
	return pool.addTx(tx, false)
}
275011
func (c *client) RepoUpdate(owner, name string, in *RepoPatch) (*Repo, error) {
	out := new(Repo)
	uri := fmt.Sprintf(pathRepo, c.addr, owner, name)
	err := c.patch(uri, in, out)
	return out, err
}
689007
func (api *LicenseAPI) FilterMultiBy(key string, value interface{}) *LicenseAPI {
	api.filterBy(key, value, true)
	return api
}
443673
func mungeDocForMultiModel(doc interface{}, modelUUID string, modelUUIDFlags int) (bson.D, error) {
	var bDoc bson.D
	var err error
	if doc != nil {
		bDoc, err = toBsonD(doc)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	modelUUIDSeen := false
	for i, elem := range bDoc {
		switch elem.Name {
		case "_id":
			if id, ok := elem.Value.(string); ok {
				bDoc[i].Value = ensureModelUUID(modelUUID, id)
			} else if subquery, ok := elem.Value.(bson.D); ok {
				munged, err := mungeIDSubQueryForMultiModel(subquery, modelUUID)
				if err != nil {
					return nil, errors.Trace(err)
				}
				bDoc[i].Value = munged
			}
		case "model-uuid":
			if modelUUIDFlags&noModelUUIDInInput > 0 {
				return nil, errors.New("model-uuid is added automatically and should not be provided")
			}
			modelUUIDSeen = true
			if elem.Value == "" {
				bDoc[i].Value = modelUUID
			} else if elem.Value != modelUUID {
				return nil, errors.Errorf(`bad "model-uuid" value: expected %s, got %s`, modelUUID, elem.Value)
			}
		}
	}
	if modelUUIDFlags&modelUUIDRequired > 0 && !modelUUIDSeen {
		bDoc = append(bDoc, bson.DocElem{"model-uuid", modelUUID})
	}
	return bDoc, nil
}
270810
func (k *tsmBatchKeyIterator) mergeBoolean() {
	// No blocks left, or pending merged values, we're done
	if len(k.blocks) == 0 && len(k.merged) == 0 && k.mergedBooleanValues.Len() == 0 {
		return
	}

	sort.Stable(k.blocks)

	dedup := k.mergedBooleanValues.Len() != 0
	if len(k.blocks) > 0 && !dedup {
		// If we have more than one block or any partially tombstoned blocks, we many need to dedup
		dedup = len(k.blocks[0].tombstones) > 0 || k.blocks[0].partiallyRead()

		// Quickly scan each block to see if any overlap with the prior block, if they overlap then
		// we need to dedup as there may be duplicate points now
		for i := 1; !dedup && i < len(k.blocks); i++ {
			dedup = k.blocks[i].partiallyRead() ||
				k.blocks[i].overlapsTimeRange(k.blocks[i-1].minTime, k.blocks[i-1].maxTime) ||
				len(k.blocks[i].tombstones) > 0
		}

	}

	k.merged = k.combineBoolean(dedup)
}
173058
func (b *storageDBWrapper) txnOpUpdate(id string, updates ...bson.DocElem) txn.Op {
	op := b.txnOpBase(id)
	op.Assert = txn.DocExists
	op.Update = bson.D{{"$set", bson.D(updates)}}
	return op
}
868736
func (api *ServerAPI) FilterMultiBy(key string, value interface{}) *ServerAPI {
	api.filterBy(key, value, true)
	return api
}
249058
func (s *ApplicationSnapshotConfigurationUpdate) SetSnapshotsEnabledUpdate(v bool) *ApplicationSnapshotConfigurationUpdate {
	s.SnapshotsEnabledUpdate = &v
	return s
}
736842
func (m *memDbBuffer) Set(k Key, v []byte) error {
	if len(v) == 0 {
		return errors.Trace(ErrCannotSetNilValue)
	}
	if len(k)+len(v) > m.entrySizeLimit {
		return ErrEntryTooLarge.GenWithStackByArgs(m.entrySizeLimit, len(k)+len(v))
	}

	err := m.db.Put(k, v)
	if m.Size() > m.bufferSizeLimit {
		return ErrTxnTooLarge.GenWithStack("transaction too large, size:%d", m.Size())
	}
	if m.Len() > int(m.bufferLenLimit) {
		return ErrTxnTooLarge.GenWithStack("transaction too large, len:%d", m.Len())
	}
	return errors.Trace(err)
}
147964
func (s *diskBlockMetadataStore) UpdateMetadata(ctx context.Context,
	blockID kbfsblock.ID, updater BlockMetadataUpdater) error {
	bid := blockID.Bytes()

	s.lock.Lock()
	defer s.lock.Unlock()

	select {
	case <-s.shutdownCh:
		return ErrBlockMetadataStoreShutdown{}
	default:
	}

	var value BlockMetadataValue
	encoded, err := s.db.Get(bid, nil)
	switch errors.Cause(err) {
	case ldberrors.ErrNotFound:
	case nil:
		if err = s.config.Codec().Decode(encoded, &value); err != nil {
			s.log.CWarningf(ctx, "decoding block metadata error: %v", err)
		}
	default:
		s.log.CWarningf(ctx, "GetMetadata error: %v", err)
	}

	if err = updater(&value); err != nil {
		return err
	}

	if encoded, err = s.config.Codec().Encode(value); err != nil {
		return err
	}
	return s.db.PutWithMeter(bid, encoded, s.putMeter)
}
551804
func (m *MongoDB) Upsert(key string, val Model) (bool, error) {
	sess := m.sess.Copy()
	defer sess.Close()
	coll := sess.DB(dbName).C(candiesColl)
	cInfo, err := coll.Upsert(bson.M{"key": key}, objWrapper{Key: key, Data: val})
	if err != nil {
		return false, err
	}
	// the Updated field is set when already existed, otherwise the UpsertedID field is set.
	// see the func at https://bazaar.launchpad.net/+branch/mgo/v2/view/head:/session.go#L1896
	return cInfo.UpsertedId != nil, nil
}