Skip to content

Commit

Permalink
feat: wip on documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Nov 19, 2024
1 parent 2543856 commit 4da91d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (it *rawIterator) hasNext() bool {
// router or on the transaction from which the Iter is created.
type Iter struct {
tree *iTree
root root
root roots
maxDepth uint32
}

Expand Down
8 changes: 4 additions & 4 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"strings"
)

type root []*node
type roots []*node

func (r root) methodIndex(method string) int {
func (r roots) methodIndex(method string) int {
// Nodes for common http method are pre instantiated.
switch method {
case http.MethodGet:
Expand All @@ -36,7 +36,7 @@ func (r root) methodIndex(method string) int {
return -1
}

func (r root) search(rootNode *node, path string, write bool, maxDepth uint32) searchResult {
func (r roots) search(rootNode *node, path string, write bool, maxDepth uint32) searchResult {
current := rootNode

var (
Expand Down Expand Up @@ -98,7 +98,7 @@ STOP:

// lookup returns the node matching the host and/or path. If lazy is false, it parses and record into c, path segment according to
// the route definition. In case of indirect match, tsr is true and n != nil.
func (r root) lookup(t *iTree, method, hostPort, path string, c *cTx, lazy bool) (n *node, tsr bool) {
func (r roots) lookup(t *iTree, method, hostPort, path string, c *cTx, lazy bool) (n *node, tsr bool) {
index := r.methodIndex(method)
if index < 0 || len(r[index].children) == 0 {
return nil, false
Expand Down
46 changes: 23 additions & 23 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const defaultModifiedCache = 4096
// without any coordination.
type iTree struct {
ctx sync.Pool
root root
root roots
fox *Router
maxParams uint32
maxDepth uint32
}

func (t *iTree) txn() *txn {
return &txn{
func (t *iTree) txn() *tXn {
return &tXn{
tree: t,
root: t.root,
maxParams: t.maxParams,
Expand All @@ -38,18 +38,18 @@ func (t *iTree) lookup(method, hostPort, path string, c *cTx, lazy bool) (n *nod
return t.root.lookup(t, method, hostPort, path, c, lazy)
}

// txn is a transaction on the tree. This transaction is applied
// tXn is a transaction on the tree. This transaction is applied
// atomically and returns a new tree when committed. A transaction
// is not thread safe, and should only be used by a single goroutine.
type txn struct {
type tXn struct {
tree *iTree
writable *simplelru.LRU[*node, any]
root root
root roots
maxParams uint32
maxDepth uint32
}

func (t *txn) commit() *iTree {
func (t *tXn) commit() *iTree {
nt := &iTree{
root: t.root,
fox: t.tree.fox,
Expand All @@ -68,10 +68,10 @@ func (t *txn) commit() *iTree {
// clone capture a point-in-time clone of the transaction. The cloned transaction will contain
// any uncommited writes in the original transaction but further mutations to either will be independent and result
// in different tree on commit.
func (t *txn) clone() *txn {
func (t *tXn) clone() *tXn {

Check failure on line 71 in tree.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

func `(*tXn).clone` is unused (unused)
// reset the writable node cache to avoid leaking future writes into the clone
t.writable = nil
tx := &txn{
tx := &tXn{
tree: t.tree,
root: t.root,
maxParams: t.maxParams,
Expand All @@ -80,15 +80,15 @@ func (t *txn) clone() *txn {
return tx
}

// snapshot capture a point-in-time snapshot of the root tree. Further mutation to txn
// snapshot capture a point-in-time snapshot of the roots tree. Further mutation to txn
// will not be reflected on the snapshot.
func (t *txn) snapshot() root {
func (t *tXn) snapshot() roots {
t.writable = nil
return t.root
}

// insert is not safe for concurrent use
func (t *txn) insert(method string, route *Route, paramsN uint32) error {
func (t *tXn) insert(method string, route *Route, paramsN uint32) error {
if t.writable == nil {
lru, err := simplelru.NewLRU[*node, any](defaultModifiedCache, nil)
if err != nil {
Expand Down Expand Up @@ -315,7 +315,7 @@ func (t *txn) insert(method string, route *Route, paramsN uint32) error {
}

// update is not safe for concurrent use
func (t *txn) update(method string, route *Route) error {
func (t *tXn) update(method string, route *Route) error {
if t.writable == nil {
lru, err := simplelru.NewLRU[*node, any](defaultModifiedCache, nil)
if err != nil {
Expand Down Expand Up @@ -352,7 +352,7 @@ func (t *txn) update(method string, route *Route) error {
}

// remove is not safe for concurrent use.
func (t *txn) remove(method, path string) bool {
func (t *tXn) remove(method, path string) bool {
if t.writable == nil {
lru, err := simplelru.NewLRU[*node, any](defaultModifiedCache, nil)
if err != nil {
Expand Down Expand Up @@ -493,15 +493,15 @@ func (t *txn) remove(method, path string) bool {
}

// addRoot append a new root node to the tree.
func (t *txn) addRoot(n *node) {
func (t *tXn) addRoot(n *node) {
nr := make([]*node, 0, len(t.root)+1)
nr = append(nr, t.root...)
nr = append(nr, n)
t.root = nr
}

// updateRoot replaces a root node in the tree.
func (t *txn) updateRoot(n *node) bool {
func (t *tXn) updateRoot(n *node) bool {
// for root node, the key contains the HTTP verb.
index := t.root.methodIndex(n.key)
if index < 0 {
Expand All @@ -516,7 +516,7 @@ func (t *txn) updateRoot(n *node) bool {
}

// removeRoot remove a root node from the tree.
func (t *txn) removeRoot(method string) bool {
func (t *tXn) removeRoot(method string) bool {
index := t.root.methodIndex(method)
if index < 0 {
return false
Expand All @@ -530,10 +530,10 @@ func (t *txn) removeRoot(method string) bool {

// truncate truncates the tree for the provided methods. If not methods are provided,
// all methods are truncated.
func (t *txn) truncate(methods []string) {
func (t *tXn) truncate(methods []string) {
if len(methods) == 0 {
// Pre instantiate nodes for common http verb
nr := make(root, len(commonVerbs))
nr := make(roots, len(commonVerbs))
for i := range commonVerbs {
nr[i] = new(node)
nr[i].key = commonVerbs[i]
Expand All @@ -545,7 +545,7 @@ func (t *txn) truncate(methods []string) {
}

oldlen := len(t.root)
nr := make(root, len(t.root))
nr := make(roots, len(t.root))
copy(nr, t.root)

for _, method := range methods {
Expand All @@ -572,7 +572,7 @@ func (t *txn) truncate(methods []string) {
// updateToRoot propagate update to the root by cloning any visited node that have not been cloned previously.
// This effectively allow to create a fully isolated snapshot of the tree.
// Note: This function should be guarded by mutex.
func (t *txn) updateToRoot(p, pp, ppp *node, visited []*node, n *node) {
func (t *tXn) updateToRoot(p, pp, ppp *node, visited []*node, n *node) {
last := n
if p != nil {
if _, ok := t.writable.Get(p); !ok {
Expand Down Expand Up @@ -652,14 +652,14 @@ func (t *txn) updateToRoot(p, pp, ppp *node, visited []*node, n *node) {
}

// updateMaxParams perform an update only if max is greater than the current
func (t *txn) updateMaxParams(max uint32) {
func (t *tXn) updateMaxParams(max uint32) {
if max > t.maxParams {
t.maxParams = max
}
}

// updateMaxDepth perform an update only if max is greater than the current
func (t *txn) updateMaxDepth(max uint32) {
func (t *tXn) updateMaxDepth(max uint32) {
if max > t.maxDepth {
t.maxDepth = max
}
Expand Down
2 changes: 1 addition & 1 deletion txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type Txn struct {
fox *Router
rootTxn *txn
rootTxn *tXn
write bool
}

Expand Down

0 comments on commit 4da91d7

Please sign in to comment.