Repository: etcd-io/etcd
File: server/storage/mvcc/key_index.go (Go)

Code before:
		keysGauge.Inc()
		g.created = rev
	}
	g.revs = append(g.revs, rev)
	g.ver++
	ki.modified = rev
}

func (ki *keyIndex) restore(lg *zap.Logger, created, modified Revision, ver int64) {
	if len(ki.generations) != 0 {
		lg.Panic(
			"'restore' got an unexpected non-empty generations",
			zap.Int("generations-size", len(ki.generations)),
		)
	}

	ki.modified = modified
	g := generation{created: created, ver: ver, revs: []Revision{modified}}
	ki.generations = append(ki.generations, g)
	keysGauge.Inc()
}

// restoreTombstone is only used to build index when there is no previous revision.
// Since there is no historical information, CreateRevision always is empty.
func (ki *keyIndex) restoreTombstone(lg *zap.Logger, main, sub int64) {
	if len(ki.generations) != 0 {
		lg.Panic(
			"'restore' got an unexpected non-empty generations",
			zap.Int("generations-size", len(ki.generations)),
		)
	}

	rev := Revision{Main: main, Sub: sub}

	ki.modified = rev
	ki.generations = append(ki.generations,
		generation{
			ver:     1,
			created: Revision{}, // unknown for first revision as tombstone
			revs:    []Revision{rev},
		},
		generation{},
	)
}

// tombstone puts a revision, pointing to a tombstone, to the keyIndex.
// It also creates a new empty generation in the keyIndex.
// It returns ErrRevisionNotFound when tombstone on an empty generation.
func (ki *keyIndex) tombstone(lg *zap.Logger, main int64, sub int64) error {
	if ki.isEmpty() {
		lg.Panic(

Review comment (suggestion):
Apart of not increasing `keyGauge` this function doesn't differs from `restore`. Maybe we could combine them as they represent the same process just with different parameters? 

Think it should be equivalent to `restore(lg, 0, Revision{main, sub}, 1)`  

 

Code after:
		keysGauge.Inc()
		g.created = rev
	}
	g.revs = append(g.revs, rev)
	g.ver++
	ki.modified = rev
}

func (ki *keyIndex) restore(lg *zap.Logger, created, modified Revision, ver int64) {
	if len(ki.generations) != 0 {
		lg.Panic(
			"'restore' got an unexpected non-empty generations",
			zap.Int("generations-size", len(ki.generations)),
		)
	}

	ki.modified = modified
	g := generation{created: created, ver: ver, revs: []Revision{modified}}
	ki.generations = append(ki.generations, g)
	keysGauge.Inc()
}

// restoreTombstone is used to restore a tombstone revision, which is the only
// revision so far for a key. We don't know the creating revision (i.e. already
// compacted) of the key, so set it empty.
func (ki *keyIndex) restoreTombstone(lg *zap.Logger, main, sub int64) {
	ki.restore(lg, Revision{}, Revision{main, sub}, 1)
	ki.generations = append(ki.generations, generation{})
	keysGauge.Dec()
}

// tombstone puts a revision, pointing to a tombstone, to the keyIndex.
// It also creates a new empty generation in the keyIndex.
// It returns ErrRevisionNotFound when tombstone on an empty generation.
func (ki *keyIndex) tombstone(lg *zap.Logger, main int64, sub int64) error {
	if ki.isEmpty() {
		lg.Panic(
			"'tombstone' got an unexpected empty keyIndex",
			zap.String("key", string(ki.key)),
		)
	}
	if ki.generations[len(ki.generations)-1].isEmpty() {
		return ErrRevisionNotFound
	}
	ki.put(lg, main, sub)
	ki.generations = append(ki.generations, generation{})
	keysGauge.Dec()
	return nil
}

// get gets the modified, created revision and version of the key that satisfies the given atRev.