code: func (m *Memory) CompareAndSwap(ctx context.Context, expected backend.Item, replaceWith backend.Item) (*backend.Lease, error) {
  	if len(expected.Key) == 0 {
  		return nil, trace.BadParameter("missing parameter Key")
  	}
  	if len(replaceWith.Key) == 0 {
  		return nil, trace.BadParameter("missing parameter Key")
  	}
  	if bytes.Compare(expected.Key, replaceWith.Key) != 0 {
  		return nil, trace.BadParameter("expected and replaceWith keys should match")
  	}
  	m.Lock()
  	defer m.Unlock()
  	m.removeExpired()
  	i := m.tree.Get(&btreeItem{Item: expected})
  	if i == nil {
  		return nil, trace.CompareFailed("key %q is not found", string(expected.Key))
  	}
  	existingItem := i.(*btreeItem).Item
  	if bytes.Compare(existingItem.Value, expected.Value) != 0 {
  		return nil, trace.CompareFailed("current value does not match expected for %v", string(expected.Key))
  	}
  	event := backend.Event{
  		Type: backend.OpPut,
  		Item: replaceWith,
  	}
  	m.processEvent(event)
  	if !m.EventsOff {
  		m.buf.Push(event)
  	}
  	return m.newLease(replaceWith), nil
  }
docs: // CompareAndSwap compares item with existing item and replaces it with replaceWith item