From 5cac34792a6b4f683f80e680749b689ba3c0842e Mon Sep 17 00:00:00 2001 From: siyul-park Date: Sun, 26 Nov 2023 06:09:07 -0500 Subject: [PATCH] refactor: extract insert --- pkg/symbol/table.go | 107 +++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/pkg/symbol/table.go b/pkg/symbol/table.go index 644f71e6..3e1b1a01 100644 --- a/pkg/symbol/table.go +++ b/pkg/symbol/table.go @@ -57,7 +57,64 @@ func (t *Table) Insert(sym *Symbol) error { if _, err := t.free(sym.ID()); err != nil { return err } + if err := t.insert(sym); err != nil { + return err + } + + return nil +} + +// Free removes a Symbol from the table. +func (t *Table) Free(id ulid.ULID) (bool, error) { + t.mu.Lock() + defer t.mu.Unlock() + + if sym, err := t.free(id); err != nil { + return false, err + } else if sym != nil { + return true, nil + } + return false, nil +} + +// LookupByID retrieves a Symbol by its ID. +func (t *Table) LookupByID(id ulid.ULID) (*Symbol, bool) { + t.mu.RLock() + defer t.mu.RUnlock() + + sym, ok := t.symbols[id] + return sym, ok +} +// LookupByName retrieves a Symbol by its namespace and name. +func (t *Table) LookupByName(namespace, name string) (*Symbol, bool) { + t.mu.RLock() + defer t.mu.RUnlock() + + if namespace, ok := t.index[namespace]; ok { + if id, ok := namespace[name]; ok { + sym, ok := t.symbols[id] + return sym, ok + } + } + return nil, false +} + +// Close closes the SymbolTable, closing all associated symbols. +func (t *Table) Close() error { + t.mu.Lock() + defer t.mu.Unlock() + + for id := range t.symbols { + if _, err := t.free(id); err != nil { + return err + } + } + + return nil +} + +func (t *Table) insert(sym *Symbol) error { t.symbols[sym.ID()] = sym t.index[sym.Namespace()] = lo.Assign(t.index[sym.Namespace()], map[string]ulid.ULID{sym.Name(): sym.ID()}) @@ -183,56 +240,6 @@ func (t *Table) Insert(sym *Symbol) error { return nil } -// Free removes a Symbol from the table. -func (t *Table) Free(id ulid.ULID) (bool, error) { - t.mu.Lock() - defer t.mu.Unlock() - - if sym, err := t.free(id); err != nil { - return false, err - } else if sym != nil { - return true, nil - } - return false, nil -} - -// LookupByID retrieves a Symbol by its ID. -func (t *Table) LookupByID(id ulid.ULID) (*Symbol, bool) { - t.mu.RLock() - defer t.mu.RUnlock() - - sym, ok := t.symbols[id] - return sym, ok -} - -// LookupByName retrieves a Symbol by its namespace and name. -func (t *Table) LookupByName(namespace, name string) (*Symbol, bool) { - t.mu.RLock() - defer t.mu.RUnlock() - - if namespace, ok := t.index[namespace]; ok { - if id, ok := namespace[name]; ok { - sym, ok := t.symbols[id] - return sym, ok - } - } - return nil, false -} - -// Close closes the SymbolTable, closing all associated symbols. -func (t *Table) Close() error { - t.mu.Lock() - defer t.mu.Unlock() - - for id := range t.symbols { - if _, err := t.free(id); err != nil { - return err - } - } - - return nil -} - func (t *Table) free(id ulid.ULID) (*Symbol, error) { sym, ok := t.symbols[id] if !ok {