diff --git a/pkg/symbol/table.go b/pkg/symbol/table.go index a9514c59..69cadea5 100644 --- a/pkg/symbol/table.go +++ b/pkg/symbol/table.go @@ -9,29 +9,30 @@ import ( ) type ( - // TableOptions is a options for Table. + // TableOptions holds options for configuring a Table. TableOptions struct { - LoadHooks []LoadHook - UnloadHooks []UnloadHook + LoadHooks []LoadHook // LoadHooks define functions to be executed on symbol loading. + UnloadHooks []UnloadHook // UnloadHooks define functions to be executed on symbol unloading. } - // Table is the storage that manages Symbol. + // Table manages the storage and operations for Symbols. Table struct { - symbols map[ulid.ULID]*Symbol - unlinks map[ulid.ULID]map[string][]scheme.PortLocation + symbols map[ulid.ULID]*Symbol + unlinks map[ulid.ULID]map[string][]scheme.PortLocation linked map[ulid.ULID]map[string][]scheme.PortLocation index map[string]map[string]ulid.ULID - loadHooks []LoadHook - unloadHooks []UnloadHook - mu sync.RWMutex + loadHooks []LoadHook + unloadHooks []UnloadHook + mu sync.RWMutex } ) -// NewTable returns a new SymbolTable +// NewTable creates a new SymbolTable with the specified options. func NewTable(opts ...TableOptions) *Table { var loadHooks []LoadHook var unloadHooks []UnloadHook + // Collect load and unload hooks from the provided options. for _, opt := range opts { loadHooks = append(loadHooks, opt.LoadHooks...) unloadHooks = append(unloadHooks, opt.UnloadHooks...) @@ -47,8 +48,9 @@ func NewTable(opts ...TableOptions) *Table { } } -// Insert inserts a node.Node. +// Insert adds a Symbol to the table. func (t *Table) Insert(sym *Symbol) error { + // Lock the table to ensure atomicity. t.mu.Lock() defer t.mu.Unlock() @@ -60,6 +62,7 @@ func (t *Table) Insert(sym *Symbol) error { return err } } + if sym.Node != prev.Node { if err := prev.Close(); err != nil { return err @@ -68,6 +71,7 @@ func (t *Table) Insert(sym *Symbol) error { } t.symbols[sym.ID()] = sym + if prev != nil && prev.Name() != "" { if namespace, ok := t.index[prev.Namespace()]; ok { delete(namespace, prev.Name()) @@ -76,6 +80,7 @@ func (t *Table) Insert(sym *Symbol) error { } } } + t.index[sym.Namespace()] = lo.Assign(t.index[sym.Namespace()], map[string]ulid.ULID{sym.Name(): sym.ID()}) var deletions map[string][]scheme.PortLocation @@ -237,7 +242,7 @@ func (t *Table) Insert(sym *Symbol) error { return nil } -// Free removes a Symbol. +// Free removes a Symbol from the table. func (t *Table) Free(id ulid.ULID) (bool, error) { t.mu.Lock() defer t.mu.Unlock() @@ -280,7 +285,7 @@ func (t *Table) Free(id ulid.ULID) (bool, error) { return false, nil } -// LookupByID returns a Symbol. +// LookupByID retrieves a Symbol by its ID. func (t *Table) LookupByID(id ulid.ULID) (*Symbol, bool) { t.mu.RLock() defer t.mu.RUnlock() @@ -289,7 +294,7 @@ func (t *Table) LookupByID(id ulid.ULID) (*Symbol, bool) { return sym, ok } -// LookupByID returns a Symbol. +// LookupByID retrieves a Symbol by its namespace and name. func (t *Table) LookupByName(namespace, name string) (*Symbol, bool) { t.mu.RLock() defer t.mu.RUnlock() @@ -303,7 +308,7 @@ func (t *Table) LookupByName(namespace, name string) (*Symbol, bool) { return nil, false } -// Close closes the SymbolTable. +// Close closes the SymbolTable, closing all associated symbols. func (t *Table) Close() error { t.mu.Lock() defer t.mu.Unlock()