Skip to content

Commit 11b0dd2

Browse files
committed
refactor: expose free
1 parent a0e7e06 commit 11b0dd2

File tree

1 file changed

+83
-104
lines changed

1 file changed

+83
-104
lines changed

pkg/symbol/table.go

Lines changed: 83 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -54,72 +54,16 @@ func (t *Table) Insert(sym *Symbol) error {
5454
t.mu.Lock()
5555
defer t.mu.Unlock()
5656

57-
prev := t.symbols[sym.ID()]
58-
59-
if prev != nil {
60-
if err := t.unload(prev); err != nil {
61-
return err
62-
}
63-
64-
if sym.Node != prev.Node {
65-
if err := prev.Close(); err != nil {
66-
return err
67-
}
68-
}
69-
70-
if prev.Name() != "" {
71-
if namespace, ok := t.index[prev.Namespace()]; ok {
72-
delete(namespace, prev.Name())
73-
if len(namespace) == 0 {
74-
delete(t.index, prev.Namespace())
75-
}
76-
}
77-
}
57+
if _, err := t.free(sym.ID()); err != nil {
58+
return err
7859
}
7960

8061
t.symbols[sym.ID()] = sym
8162
t.index[sym.Namespace()] = lo.Assign(t.index[sym.Namespace()], map[string]ulid.ULID{sym.Name(): sym.ID()})
8263

83-
var deletions map[string][]scheme.PortLocation
84-
if prev != nil {
85-
deletions = prev.Links()
86-
}
87-
additions := sym.Links()
8864
unlinks := map[string][]scheme.PortLocation{}
8965

90-
for name, locations := range deletions {
91-
for _, location := range locations {
92-
id := location.ID
93-
if id == (ulid.ULID{}) {
94-
if location.Name != "" {
95-
if namespace, ok := t.index[prev.Namespace()]; ok {
96-
id = namespace[location.Name]
97-
}
98-
}
99-
}
100-
101-
if id == (ulid.ULID{}) {
102-
continue
103-
}
104-
105-
linked := t.linked[id]
106-
var locations []scheme.PortLocation
107-
for _, location := range linked[location.Port] {
108-
if location.ID != sym.ID() && location.Port != name {
109-
locations = append(locations, location)
110-
}
111-
}
112-
if len(locations) > 0 {
113-
linked[location.Port] = locations
114-
t.linked[id] = linked
115-
} else if len(linked) > 0 {
116-
delete(linked, location.Port)
117-
t.linked[id] = linked
118-
}
119-
}
120-
}
121-
122-
for name, locations := range additions {
66+
for name, locations := range sym.Links() {
12367
p1, ok := sym.Port(name)
12468
if !ok {
12569
unlinks[name] = locations
@@ -246,44 +190,11 @@ func (t *Table) Free(id ulid.ULID) (bool, error) {
246190
t.mu.Lock()
247191
defer t.mu.Unlock()
248192

249-
if sym, ok := t.symbols[id]; ok {
250-
if err := t.unload(sym); err != nil {
251-
return false, err
252-
}
253-
if err := sym.Close(); err != nil {
254-
return false, err
255-
}
256-
257-
if namespace, ok := t.index[sym.Namespace()]; ok {
258-
delete(namespace, sym.Name())
259-
if len(namespace) == 0 {
260-
delete(t.index, sym.Namespace())
261-
}
262-
}
263-
264-
for name, locations := range t.linked[id] {
265-
for _, location := range locations {
266-
unlinks := t.unlinks[location.ID]
267-
if unlinks == nil {
268-
unlinks = make(map[string][]scheme.PortLocation)
269-
}
270-
unlinks[location.Port] = append(unlinks[location.Port], scheme.PortLocation{
271-
ID: id,
272-
Port: name,
273-
})
274-
t.unlinks[location.ID] = unlinks
275-
}
276-
}
277-
278-
delete(t.symbols, id)
279-
delete(t.unlinks, id)
280-
delete(t.linked, id)
281-
282-
t.unload(sym)
283-
193+
if sym, err := t.free(id); err != nil {
194+
return false, err
195+
} else if sym != nil {
284196
return true, nil
285197
}
286-
287198
return false, nil
288199
}
289200

@@ -315,22 +226,90 @@ func (t *Table) Close() error {
315226
t.mu.Lock()
316227
defer t.mu.Unlock()
317228

318-
for id, sym := range t.symbols {
319-
if err := t.unload(sym); err != nil {
320-
return err
321-
}
322-
if err := sym.Close(); err != nil {
229+
for id := range t.symbols {
230+
if _, err := t.free(id); err != nil {
323231
return err
324232
}
325-
delete(t.symbols, id)
326233
}
327-
t.unlinks = make(map[ulid.ULID]map[string][]scheme.PortLocation)
328-
t.linked = make(map[ulid.ULID]map[string][]scheme.PortLocation)
329-
t.index = make(map[string]map[string]ulid.ULID)
330234

331235
return nil
332236
}
333237

238+
func (t *Table) free(id ulid.ULID) (*Symbol, error) {
239+
sym, ok := t.symbols[id]
240+
if !ok {
241+
return nil, nil
242+
}
243+
244+
if err := t.unload(sym); err != nil {
245+
return nil, err
246+
}
247+
if err := sym.Close(); err != nil {
248+
return nil, err
249+
}
250+
251+
if sym.Name() != "" {
252+
if namespace, ok := t.index[sym.Namespace()]; ok {
253+
delete(namespace, sym.Name())
254+
if len(namespace) == 0 {
255+
delete(t.index, sym.Namespace())
256+
}
257+
}
258+
}
259+
260+
for name, locations := range sym.Links() {
261+
for _, location := range locations {
262+
id := location.ID
263+
if id == (ulid.ULID{}) {
264+
if location.Name != "" {
265+
if namespace, ok := t.index[sym.Namespace()]; ok {
266+
id = namespace[location.Name]
267+
}
268+
}
269+
}
270+
271+
if id == (ulid.ULID{}) {
272+
continue
273+
}
274+
275+
linked := t.linked[id]
276+
var locations []scheme.PortLocation
277+
for _, location := range linked[location.Port] {
278+
if location.ID != sym.ID() && location.Port != name {
279+
locations = append(locations, location)
280+
}
281+
}
282+
if len(locations) > 0 {
283+
linked[location.Port] = locations
284+
t.linked[id] = linked
285+
} else if len(linked) > 0 {
286+
delete(linked, location.Port)
287+
t.linked[id] = linked
288+
}
289+
}
290+
}
291+
292+
for name, locations := range t.linked[id] {
293+
for _, location := range locations {
294+
unlinks := t.unlinks[location.ID]
295+
if unlinks == nil {
296+
unlinks = make(map[string][]scheme.PortLocation)
297+
}
298+
unlinks[location.Port] = append(unlinks[location.Port], scheme.PortLocation{
299+
ID: id,
300+
Port: name,
301+
})
302+
t.unlinks[location.ID] = unlinks
303+
}
304+
}
305+
306+
delete(t.symbols, id)
307+
delete(t.unlinks, id)
308+
delete(t.linked, id)
309+
310+
return sym, nil
311+
}
312+
334313
func (t *Table) load(sym *Symbol) error {
335314
if len(t.unlinks[sym.ID()]) > 0 {
336315
return nil

0 commit comments

Comments
 (0)