File: references.go

package info (click to toggle)
golang-github-ovn-org-libovsdb 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,440 kB
  • sloc: makefile: 52; sh: 14
file content (71 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package database

// References tracks the references to rows from other rows at specific
// locations in the schema.
type References map[ReferenceSpec]Reference

// ReferenceSpec specifies details about where in the schema a reference occurs.
type ReferenceSpec struct {
	// ToTable is the table of the row to which the reference is made
	ToTable string

	// FromTable is the table of the row from which the reference is made
	FromTable string

	// FromColumn is the column of the row from which the reference is made
	FromColumn string

	// FromValue flags if the reference is made on a map key or map value when
	// the column is a map
	FromValue bool
}

// Reference maps the UUIDs of rows to which the reference is made to the
// rows it is made from
type Reference map[string][]string

// GetReferences gets references to a row
func (rs References) GetReferences(table, uuid string) References {
	refs := References{}
	for spec, values := range rs {
		if spec.ToTable != table {
			continue
		}
		if _, ok := values[uuid]; ok {
			refs[spec] = Reference{uuid: values[uuid]}
		}
	}
	return refs
}

// UpdateReferences updates the references with the provided ones. Dangling
// references, that is, the references of rows that are no longer referenced
// from anywhere, are cleaned up.
func (rs References) UpdateReferences(other References) {
	for spec, otherRefs := range other {
		for to, from := range otherRefs {
			rs.updateReference(spec, to, from)
		}
	}
}

// updateReference updates the references to a row at a specific location in the
// schema
func (rs References) updateReference(spec ReferenceSpec, to string, from []string) {
	thisRefs, ok := rs[spec]
	if !ok && len(from) > 0 {
		// add references from a previously untracked location
		rs[spec] = Reference{to: from}
		return
	}
	if len(from) > 0 {
		// replace references to this row at this specific location
		thisRefs[to] = from
		return
	}
	// otherwise remove previously tracked references
	delete(thisRefs, to)
	if len(thisRefs) == 0 {
		delete(rs, spec)
	}
}