@@ -54,72 +54,16 @@ func (t *Table) Insert(sym *Symbol) error {
54
54
t .mu .Lock ()
55
55
defer t .mu .Unlock ()
56
56
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
78
59
}
79
60
80
61
t .symbols [sym .ID ()] = sym
81
62
t .index [sym .Namespace ()] = lo .Assign (t .index [sym .Namespace ()], map [string ]ulid.ULID {sym .Name (): sym .ID ()})
82
63
83
- var deletions map [string ][]scheme.PortLocation
84
- if prev != nil {
85
- deletions = prev .Links ()
86
- }
87
- additions := sym .Links ()
88
64
unlinks := map [string ][]scheme.PortLocation {}
89
65
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 () {
123
67
p1 , ok := sym .Port (name )
124
68
if ! ok {
125
69
unlinks [name ] = locations
@@ -246,44 +190,11 @@ func (t *Table) Free(id ulid.ULID) (bool, error) {
246
190
t .mu .Lock ()
247
191
defer t .mu .Unlock ()
248
192
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 {
284
196
return true , nil
285
197
}
286
-
287
198
return false , nil
288
199
}
289
200
@@ -315,22 +226,90 @@ func (t *Table) Close() error {
315
226
t .mu .Lock ()
316
227
defer t .mu .Unlock ()
317
228
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 {
323
231
return err
324
232
}
325
- delete (t .symbols , id )
326
233
}
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 )
330
234
331
235
return nil
332
236
}
333
237
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
+
334
313
func (t * Table ) load (sym * Symbol ) error {
335
314
if len (t .unlinks [sym .ID ()]) > 0 {
336
315
return nil
0 commit comments