Repository: etcd-io/etcd
File: tests/robustness/validate/patch_history.go (Go)

Code before:

func countPuts(counter map[keyValue]int64, request model.EtcdRequest) {
	switch request.Type {
	case model.Txn:
		for _, operation := range append(request.Txn.OperationsOnSuccess, request.Txn.OperationsOnFailure...) {
			switch operation.Type {
			case model.PutOperation:
				kv := keyValue{Key: operation.Put.Key, Value: operation.Put.Value}
				counter[kv]++
			case model.DeleteOperation:
			case model.RangeOperation:
			default:
				panic(fmt.Sprintf("unknown operation type %q", operation.Type))
			}
		}
	case model.LeaseGrant:
	case model.LeaseRevoke:
	case model.Compact:
	case model.Defragment:
	case model.Range:
	default:
		panic(fmt.Sprintf("unknown request type %q", request.Type))
	}
}

func uniqueDeleteReturnTime(allOps []porcupine.Operation, persisted []model.EtcdRequest, clientDeleteCount map[string]int64) map[string]int64 {
	times := map[string]int64{}
	for _, op := range allOps {
		req := op.Input.(model.EtcdRequest)
		if req.Type == model.Txn {
			for _, e := range append(req.Txn.OperationsOnSuccess, req.Txn.OperationsOnFailure...) {
				if e.Type != model.DeleteOperation {
					continue
				}
				k := e.Delete.Key
				if clientDeleteCount[k] == 1 {
					if t, ok := times[k]; !ok || op.Return < t {
						times[k] = op.Return
					}
				}
			}
		}
	}
	return times
}

func countClientDeletes(reports []report.ClientReport) map[string]int64 {
	counter := map[string]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)

Review comment (suggestion):
Can you merge this function with uniquePutReturnTime? 

A resulting funtion prototype could be someting like this:
```
func uniqueOperationReturnTime(allOps []porcupine.Operation, persisted []model.EtcdRequest, clientDeleteCount map[DeleteOptions]int64, clientPutCount map[PutOptions]int64) (map[DeleteOptions]int64, map[PutOptions]int64)
```

Code after:
					}
				case model.DeleteOperation:
					kv := model.DeleteOptions{Key: op.Delete.Key}
					if clientDeleteCount[kv] > 1 {
						continue
					}
					if returnTime, ok := delTimes[kv]; ok {
						lastReturnTime = min(returnTime, lastReturnTime)
						delTimes[kv] = lastReturnTime
					}
				}
			}
		case model.LeaseGrant:
		case model.LeaseRevoke:
		case model.Compact:
		default:
			panic(fmt.Sprintf("Unknown request type: %q", request.Type))
		}
	}
	return putTimes, delTimes
}

func countClientPuts(reports []report.ClientReport) map[model.PutOptions]int64 {
	counter := map[model.PutOptions]int64{}
	for _, client := range reports {
		for _, op := range client.KeyValue {
			request := op.Input.(model.EtcdRequest)
			countPuts(counter, request)
		}
	}
	return counter
}

func countPersistedPuts(requests []model.EtcdRequest) map[model.PutOptions]int64 {
	counter := map[model.PutOptions]int64{}
	for _, request := range requests {
		countPuts(counter, request)
	}
	return counter
}

func countPuts(counter map[model.PutOptions]int64, request model.EtcdRequest) {
	switch request.Type {
	case model.Txn:
		for _, operation := range append(request.Txn.OperationsOnSuccess, request.Txn.OperationsOnFailure...) {
			switch operation.Type {
			case model.PutOperation:
				kv := model.PutOptions{Key: operation.Put.Key, Value: operation.Put.Value}
				counter[kv]++
			case model.DeleteOperation:
			case model.RangeOperation: