Skip to content

Commit

Permalink
refactor: extract insert
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 26, 2023
1 parent a0d7a7d commit 5cac347
Showing 1 changed file with 57 additions and 50 deletions.
107 changes: 57 additions & 50 deletions pkg/symbol/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,64 @@ func (t *Table) Insert(sym *Symbol) error {
if _, err := t.free(sym.ID()); err != nil {
return err

Check warning on line 58 in pkg/symbol/table.go

View check run for this annotation

Codecov / codecov/patch

pkg/symbol/table.go#L58

Added line #L58 was not covered by tests
}
if err := t.insert(sym); err != nil {
return err

Check warning on line 61 in pkg/symbol/table.go

View check run for this annotation

Codecov / codecov/patch

pkg/symbol/table.go#L61

Added line #L61 was not covered by tests
}

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

Check warning on line 73 in pkg/symbol/table.go

View check run for this annotation

Codecov / codecov/patch

pkg/symbol/table.go#L73

Added line #L73 was not covered by tests
} 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
}

Check warning on line 98 in pkg/symbol/table.go

View check run for this annotation

Codecov / codecov/patch

pkg/symbol/table.go#L90-L98

Added lines #L90 - L98 were not covered by tests
}
return nil, false

Check warning on line 100 in pkg/symbol/table.go

View check run for this annotation

Codecov / codecov/patch

pkg/symbol/table.go#L100

Added line #L100 was not covered by tests
}

// 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()})

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5cac347

Please sign in to comment.