Skip to content

Commit

Permalink
refactor: redesign compilation process (#10)
Browse files Browse the repository at this point in the history
* feat: move linking to symbol table

* feat: change symbol table

* feat: add symbol

* refactor: symbol impliment node

* refactor: remove unload in loader and migrate

* fix: add filter load all

* test: add load exists

* refactor: remote -> storage

* docs: add comment in symbol

* docs: change comment in table

* docs: add comment in loader

* refactor: improve cohesion

* docs: fix comment

* fix: add hook free close

* refactor: expose free

* refactor: extract insert

* fix: unload when free related symbol

* fix: other namespace

* chore: remove unused comment

* feat: add index if name is not zero

* refactor: add test and remove unused

* fix: safe rollback linked

* test: add more table test

* test: add more test

* refactor: remove unused if
  • Loading branch information
siyul-park authored Nov 27, 2023
1 parent b391d5d commit b2de2a7
Show file tree
Hide file tree
Showing 19 changed files with 1,018 additions and 1,129 deletions.
84 changes: 19 additions & 65 deletions pkg/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,102 +10,56 @@ import (
type (
// Hook is a collection of hook functions.
Hook struct {
preLoadHooks []symbol.PreLoadHook
postLoadHooks []symbol.PostLoadHook
preUnloadHooks []symbol.PreUnloadHook
postUnloadHooks []symbol.PostUnloadHook
mu sync.RWMutex
loadHooks []symbol.LoadHook
unloadHooks []symbol.UnloadHook
mu sync.RWMutex
}
)

var _ symbol.PreLoadHook = &Hook{}
var _ symbol.PostLoadHook = &Hook{}
var _ symbol.PreUnloadHook = &Hook{}
var _ symbol.PostUnloadHook = &Hook{}
var _ symbol.LoadHook = &Hook{}
var _ symbol.UnloadHook = &Hook{}

// New returns a new Hooks.
func New() *Hook {
return &Hook{}
}

// AddPreLoadHook adds a PreLoadHook.
func (h *Hook) AddPreLoadHook(hook symbol.PreLoadHook) {
// AddLoadHook adds a LoadHook.
func (h *Hook) AddLoadHook(hook symbol.LoadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.preLoadHooks = append(h.preLoadHooks, hook)
h.loadHooks = append(h.loadHooks, hook)
}

// AddPostLoadHook adds a PostLoadHook.
func (h *Hook) AddPostLoadHook(hook symbol.PostLoadHook) {
// AddUnloadHook adds a UnloadHook.
func (h *Hook) AddUnloadHook(hook symbol.UnloadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.postLoadHooks = append(h.postLoadHooks, hook)
h.unloadHooks = append(h.unloadHooks, hook)
}

// AddPreUnloadHook adds a PreUnloadHook.
func (h *Hook) AddPreUnloadHook(hook symbol.PreUnloadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.preUnloadHooks = append(h.preUnloadHooks, hook)
}

// AddPostUnloadHook adds a PostUnloadHook.
func (h *Hook) AddPostUnloadHook(hook symbol.PostUnloadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.postUnloadHooks = append(h.postUnloadHooks, hook)
}

// PreLoad runs PreLoadHooks.
func (h *Hook) PreLoad(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()

for _, hook := range h.preLoadHooks {
if err := hook.PreLoad(n); err != nil {
return err
}
}
return nil
}

// PostLoad runs PostLoadHooks.
func (h *Hook) PostLoad(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()

for _, hook := range h.postLoadHooks {
if err := hook.PostLoad(n); err != nil {
return err
}
}
return nil
}

// PreUnload runs PreUnloadHooks.
func (h *Hook) PreUnload(n node.Node) error {
// Load runs LoadHooks.
func (h *Hook) Load(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()

for _, hook := range h.preUnloadHooks {
if err := hook.PreUnload(n); err != nil {
for _, hook := range h.loadHooks {
if err := hook.Load(n); err != nil {
return err
}
}
return nil
}

// PostUnload runs PostUnloadHooks.
func (h *Hook) PostUnload(n node.Node) error {
// Unload runs UnloadHooks.
func (h *Hook) Unload(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()

for _, hook := range h.postUnloadHooks {
if err := hook.PostUnload(n); err != nil {
for _, hook := range h.unloadHooks {
if err := hook.Unload(n); err != nil {
return err
}
}
Expand Down
52 changes: 14 additions & 38 deletions pkg/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,38 @@ import (
"github.com/stretchr/testify/assert"
)

func TestHook_PreLoadHook(t *testing.T) {
func TestHook_LoadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})

h := symbol.PreLoadHookFunc(func(_ node.Node) error {
count := 0
h := symbol.LoadHookFunc(func(_ node.Node) error {
count += 1
return nil
})

hooks.AddPreLoadHook(h)
hooks.AddLoadHook(h)

err := hooks.PreLoad(n)
err := hooks.Load(n)
assert.NoError(t, err)
assert.Equal(t, 1, count)
}

func TestHook_PostLoadHook(t *testing.T) {
func TestHook_UnloadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})

h := symbol.PostLoadHookFunc(func(_ node.Node) error {
count := 0
h := symbol.UnloadHookFunc(func(_ node.Node) error {
count += 1
return nil
})

hooks.AddPostLoadHook(h)
hooks.AddUnloadHook(h)

err := hooks.PostLoad(n)
assert.NoError(t, err)
}

func TestHook_PreUnloadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})

h := symbol.PreUnloadHookFunc(func(_ node.Node) error {
return nil
})

hooks.AddPreUnloadHook(h)

err := hooks.PreUnload(n)
assert.NoError(t, err)
}

func TestHook_PostUnloadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})

h := symbol.PostUnloadHookFunc(func(_ node.Node) error {
return nil
})

hooks.AddPostUnloadHook(h)

err := hooks.PostUnload(n)
err := hooks.Unload(n)
assert.NoError(t, err)
assert.Equal(t, 1, count)
}
Loading

0 comments on commit b2de2a7

Please sign in to comment.