_metcd/etcd_store.go
func applyCompare(kv mvcc.KV, c *etcdserverpb.Compare) (int64, bool) {
	ckvs, rev, err := kv.Range(c.Key, nil, 1, 0)
	if err != nil {
		if err == mvcc.ErrTxnIDMismatch {
			panic("unexpected txn ID mismatch error")
		}
		return rev, false
	}
	var ckv mvccpb.KeyValue
	if len(ckvs) != 0 {
		ckv = ckvs[0]
	} else {
		// Use the zero value of ckv normally. However...
		if c.Target == etcdserverpb.Compare_VALUE {
			// Always fail if we're comparing a value on a key that doesn't exist.
			// We can treat non-existence as the empty set explicitly, such that
			// even a key with a value of length 0 bytes is still a real key
			// that was written that way
			return rev, false
		}
	}

	// -1 is less, 0 is equal, 1 is greater
	var result int
	switch c.Target {
	case etcdserverpb.Compare_VALUE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Value)
		if tv != nil {
			result = bytes.Compare(ckv.Value, tv.Value)
		}
	case etcdserverpb.Compare_CREATE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_CreateRevision)
		if tv != nil {
			result = compareInt64(ckv.CreateRevision, tv.CreateRevision)
		}

	case etcdserverpb.Compare_MOD:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_ModRevision)
		if tv != nil {
			result = compareInt64(ckv.ModRevision, tv.ModRevision)
		}
	case etcdserverpb.Compare_VERSION:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Version)
		if tv != nil {
			result = compareInt64(ckv.Version, tv.Version)
		}
	}

	switch c.Result {
	case etcdserverpb.Compare_EQUAL:
		if result != 0 {
			return rev, false
		}
	case etcdserverpb.Compare_GREATER:
		if result != 1 {
			return rev, false
		}
	case etcdserverpb.Compare_LESS:
		if result != -1 {
			return rev, false
		}
	}
	return rev, true
}
func applyCompare(kv mvcc.KV, c *etcdserverpb.Compare) (int64, bool) {
	ckvs, rev, err := kv.Range(c.Key, nil, 1, 0)
	if err != nil {
		if err == mvcc.ErrTxnIDMismatch {
			panic("unexpected txn ID mismatch error")
		}
		return rev, false
	}
	var ckv mvccpb.KeyValue
	if len(ckvs) != 0 {
		ckv = ckvs[0]
	} else {
		// Use the zero value of ckv normally. However...
		if c.Target == etcdserverpb.Compare_VALUE {
			// Always fail if we're comparing a value on a key that doesn't exist.
			// We can treat non-existence as the empty set explicitly, such that
			// even a key with a value of length 0 bytes is still a real key
			// that was written that way
			return rev, false
		}
	}

	// -1 is less, 0 is equal, 1 is greater
	var result int
	switch c.Target {
	case etcdserverpb.Compare_VALUE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Value)
		if tv != nil {
			result = bytes.Compare(ckv.Value, tv.Value)
		}
	case etcdserverpb.Compare_CREATE:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_CreateRevision)
		if tv != nil {
			result = compareInt64(ckv.CreateRevision, tv.CreateRevision)
		}

	case etcdserverpb.Compare_MOD:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_ModRevision)
		if tv != nil {
			result = compareInt64(ckv.ModRevision, tv.ModRevision)
		}
	case etcdserverpb.Compare_VERSION:
		tv, _ := c.TargetUnion.(*etcdserverpb.Compare_Version)
		if tv != nil {
			result = compareInt64(ckv.Version, tv.Version)
		}
	}

	switch c.Result {
	case etcdserverpb.Compare_EQUAL:
		if result != 0 {
			return rev, false
		}
	case etcdserverpb.Compare_GREATER:
		if result != 1 {
			return rev, false
		}
	case etcdserverpb.Compare_LESS:
		if result != -1 {
			return rev, false
		}
	}
	return rev, true
}
// applyCompare applies the compare request.
// It returns the revision at which the comparison happens. If the comparison
// succeeds, the it returns true. Otherwise it returns false.