Repository: pingcap/tidb
File: pkg/meta/meta.go (Go)

Code before:
// LoadIfMissing == true, it loads if attr is missing.
// LoadIfMissing == false, it loads if attr is present.
type MustLoadFilterAttr struct {
	Attr          string
	LoadIfMissing bool
}

var checkAttributesInOrder = []MustLoadFilterAttr{
	{Attr: `"partition":null`, LoadIfMissing: true},
	{Attr: `"Lock":null`, LoadIfMissing: true},
	{Attr: `"tiflash_replica":null`, LoadIfMissing: true},
	{Attr: `"temp_table_type":0`, LoadIfMissing: true},
	{Attr: `"policy_ref_info":null`, LoadIfMissing: true},
	{Attr: `"ttl_info":null`, LoadIfMissing: true},
	{Attr: `"affinity":{`, LoadIfMissing: false},
	{Attr: `"softdelete_info":{`, LoadIfMissing: false},
}

// isTableInfoMustLoad checks whether the table info needs to be loaded.
// If the byte representation contains all the given attributes,
// then it does not need to be loaded and this function will return false.
// Otherwise, it will return true, indicating that the table info should be loaded.
// Since attributes are checked in sequence, it's important to choose the order carefully.
// isCheckForeignKeyAttrsInOrder check foreign key or not, since fk_info contains two null situations.
func isTableInfoMustLoad(json []byte, isCheckForeignKeyAttrsInOrder bool, filterAttrs ...MustLoadFilterAttr) bool {
	idx := 0
	if isCheckForeignKeyAttrsInOrder {
		idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesNil))
		if idx == -1 {
			idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesZero))
			if idx == -1 {
				return true
			}
		}
		json = json[idx:]
	}
	for _, filter := range filterAttrs {
		idx = bytes.Index(json, hack.Slice(filter.Attr))
		if idx == -1 {
			if filter.LoadIfMissing {
				return true
			}
			continue
		}
		if !filter.LoadIfMissing {
			return true
		}
		json = json[idx:]
	}
	return false
}

Review comment (suggestion):
The doc comment for `isTableInfoMustLoad` is now misleading: it says “contains all the given attributes => return false”, but with `MustLoadFilterAttr` some filters intentionally return true when an attribute is present (`LoadIfMissing == false`). Please update the comment to describe the new filter semantics (required-present vs required-absent) so future changes don’t break the intended behavior.

Code after:
// LoadIfMissing controls whether the absence or presence of the attribute
// should trigger loading: when true, load if the attribute is missing;
// when false, load if the attribute is present.
type MustLoadFilterAttr struct {
	Attr          string
	LoadIfMissing bool
}

var checkAttributesInOrder = []MustLoadFilterAttr{
	{Attr: `"partition":null`, LoadIfMissing: true},
	{Attr: `"Lock":null`, LoadIfMissing: true},
	{Attr: `"tiflash_replica":null`, LoadIfMissing: true},
	{Attr: `"temp_table_type":0`, LoadIfMissing: true},
	{Attr: `"policy_ref_info":null`, LoadIfMissing: true},
	{Attr: `"ttl_info":null`, LoadIfMissing: true},
	{Attr: `"affinity":{`, LoadIfMissing: false},
	{Attr: `"softdelete_info":{`, LoadIfMissing: false},
}

// isTableInfoMustLoad checks whether the table info needs to be loaded.
// If the byte representation follows filterAttrs, it returns true.
// Otherwise, it return false meaning that table is not neeeded to load.
// Since attributes are checked in sequence, it's important to choose the order carefully.
// isCheckForeignKeyAttrsInOrder check foreign key or not, since fk_info contains two null situations.
func isTableInfoMustLoad(json []byte, isCheckForeignKeyAttrsInOrder bool, filterAttrs ...MustLoadFilterAttr) bool {
	idx := 0
	if isCheckForeignKeyAttrsInOrder {
		idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesNil))
		if idx == -1 {
			idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesZero))
			if idx == -1 {
				return true
			}
		}
		json = json[idx:]
	}
	for _, filter := range filterAttrs {
		idx = bytes.Index(json, hack.Slice(filter.Attr))
		if idx == -1 {
			if filter.LoadIfMissing {
				return true
			}
			continue
		}
		if !filter.LoadIfMissing {
			return true
		}
		json = json[idx:]
	}
	return false
}