nork

package module
v0.0.0-...-1f5827a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 10 Imported by: 3 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nork is Nodes with ORdered Kids - a building block for hierarchical trees of nodes, where sibling nodes are ordered and keep their order.

A Nork stores bidirectional parent/kid relationships, plus other useful hierarchy-related info like levels and paths. A Nork does not need or use Go generics.

Node order is of course important for markup languages in general and XML mixed content in particular, while unimportant when XML is used purely for data records.

Child nodes ("Kid"s) have an externally-defined order, implying that child nodes are not directly [Comparable] amongst themselves. This specific externally-provided order of child nodes is essential for representing content.

(Child ordering can help in other contexts too, such as filesystem operations, but it turns out that the Go stdlib generally returns directory items in an order - lexical order - and walks directories in lexical order.)

The ordering lets us define funcs like FirstKid, NextPeer, PrevPeer, LastKid. They are defined in interface Norker, which is implemented not by struct Nork but rather by the pointer *Nork. This makes nodes writable and also sharable.

Advanced Usage: Directions, Use Cases, Func Aliasing

Nork can become a "QuadraNode", handling four directions of connections for four distinct node user cases (UCs). Nork will at first be kind of a maximalist implementation, subject later to redefinition and getting slimmed down.

Paths need not be in the filesystem, and so paths are IAW package path rather than package path/filepath.

Structs that embed struct Nork can use funcs to alias the names and usage of fields in Nork. For example, a Nork embedded in a [Cnty] (Content Entity) used for a file-or-dir can provide access to non-public field `relPath` via `func RelFP()` (where FP is "filepath").

Quadranode Discussion

Directions in GUI:

  1. Left is ParentDir and Incoming Links
  2. Right is Kids and Outgoing Links
  3. Above is Incomiong Facets and maybe other metadata
  4. Below is Versions

Fields and use cases:

  • The field Prnt is for a "Parent" singleton of some type (whenever applicable).
  • The fields Tags and Vers are used pretty much the same in all UC's.
  • UC.FSO: File System Object: represent a dir-or-file-or-softlink: Here ordering is less important. Paths are valid FS paths. Prnt:dir, Kids:contents(!dir-v-file), Usrs:incoming-symlinks
  • UC.CMS: CMS Usage: Table of Contents line item (transclusion in CMS) This should be an ideal use case. Prnt:?TBD, Kids:sub-ToC's/outrefs, Usrs:inrefs+transcluders
  • UC.XML: XML text: tag/element in markup (XML, or other w AST) This has complexity handling same-named siblings, such as multiple <p> tags. Paths are tricky (mult. same tags). Prnt:parent-elm(or file @root), Kids:kid-elms, Usrs:?entity-stuf
  • UC.GST: GolangAST: Go code AST node (https://pkg.go.dev/go/ast#Node) Pnt:node(or root's file), Kids:AST, Usrs:callers/refs

Memory and Nodes (NOTE: Obsolete discussion)

There are two distinct memory management nodes for allocating and linking nodes:

  • The "traditional method" of allocating nodes individually, and linking them using pointers. Using this method, both deletions and insertions are relatively simple.
  • Such nodes can also be loaded into a map, for random access to nodes based on path.
  • The "new-fangled way" called an "arena", where we put all our nodes in a big slice, and link them using indices. This method is much kinder on memory management, but might becomes clumsy when we need dynamic node management.
  • Deletions are easy if we just zero out the slice entry; we cannot then do compaction because it would require updating all indices past the first point of compaction.
  • Insertions are costly. However note that in this implementation, for a node's kids, we use a linked list rather than a slice, so this makes it easier to append a new node at the end of the arena-slice and then update indices, wherever they may be elsewhere in the slice.
  • In any case, if an arena-slice has to grow (because of a call to append), it might be moved elsewhere in memory, which would invalidate all ptrs to other Norks! If this happens, we should trust only the "traditional method".

There are multiple ways to represent node trees in our SQLite DBMS, and multiple ways to walk a node tree, so there is a unavoidable complexity wherever we look.

NOTE: DOM markup exhibits name duplication: In UC.1 we never have two same-named files in the same directory, but in UC.2 we might have (for example) multiple sibling <p> tags. So when representing markup, a map from paths to Norks would fail unless the tags are made unique with subscript indices (such as "[1]", "[2]").

Link fields are lower-cased so that other packages cannot damage links.

NOTE: This implementation stores pointers to child nodes in a doubly linked list, not a slice. Therefore a Nork does not have a complete set of pointers to all of its kids. Therefore (a) it is not simple to get a kid count, because it requires a list traversal, and (b) it is not feasible to modify this code to define a simpler, more efficient variant of Nork that has unordered kids.

OID, Based on UUID version 7

UUID (version 7, featuring time ordering) has outsized importance, because it can be used to reference all manner of other DB tables and data structures and external (to the system) targets. This is used in package oid in type OID.

This could work nicely with the Quadranode concept.

A OID should be absolutely unique within a particular database, and quite possible between databases too.

Therefore it should be used as the main indexing field for and and all objects in SQLite.

It will be necessary to add prefixes to UUIDs to specify (e.g.) the DB tables of targets.

This requires the use of a custom UUID v7 implementation, in package oid.

When a tree of items is loaded into memory, a OID is a very inefficient way to follow a link between nodes, so instead, there should be pointers to other nodes, which (of course) are session-specific and should not be persisted to database.

It is straightforward to store 1-to-1 links in a database. But there won't be many of those, if any. But there will be many cases of a parent with many kids, and for a Quadranode there will be many nodes with multiple parents. (Altho we do not expect to see N-to-N situations.)

So for these two situations - 1-to-N, and in the future N-to-1 - there can be two database tables "Links1toN" and later "LinksNto1". And because every OID will contain a table name corresponding to an item type, use cases where the items are a mix of types can simply store them all together. .

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleSomething

func ExampleSomething()

Types

type FSONork

type FSONork struct {
	Nork
	FSO *FU.FSObject
}

FSONork embeds an FSO with a Nork (node with ordered kids). The FSO includes a [Filepaths] struct and flags like IsDir and DoesNotExist.

  • For a non-FS use case, IsDir() might be a "CanKids"
  • For any non-FS use case, IsDirlike should be considered TBD

The UC (Use Case) for FSONork is:

  • UC.FSI: File System Item: represent a dir-or-file-or-softlink: Here ordering is less important. Paths are valid FS paths. prnt-link is a dir, kid links are dir-or-file-or-symlink, w content; usrs-linnks (if used) can be incoming-symlinks.

Using Norks for files & dirs is asking too much of them. For one thing, symlnks add a lot of complexity. This complexity is instead handler by this struct, FSONork, an outer struct type that embeds a Nork and an [FSObject].

Implementation node: If we build up a tree of Norks when processing an os.DirFS, the strict ordering provided by DirFS is not strictly needed, BUT it can anyways be used (and relied upon) because the three flavors of WalkDir are deterministic (using lexical order). WalkDir does tho at least promise that a given Nork will always appear only after the Nork for its directory has appeared, which makes it "easy" to build a tree. .

func NewFSOLoneNork

func NewFSOLoneNork(aRelPath string) *FSONork

NewFSOLoneNork prefers a relative path. It does not load file content, which is an expensive operation done elsewhere. But it loads the [Filepaths], which does checks specific to filesystem objects. Then the content can be loaded lazily at any time.

Do NOT use this for a path that is not a filesystem path. .

func (*FSONork) AbsFP

func (p *FSONork) AbsFP() string

AbsFP is an absolute filepath. Discussion: It is the same as rel.path, but expanded to be rooted in - i.e. traced back to the root of - a filesystem.

func (*FSONork) IsDirlike

func (p *FSONork) IsDirlike() bool

func (*FSONork) IsRoot

func (p *FSONork) IsRoot() bool

Why is this commented out ? Name collision ? func (p *FSONork) IsDir() bool { return p.Nork.IsDir() } This should check level instead.

func (*FSONork) RelFP

func (p *FSONork) RelFP() string

RelPath is a relative filepath. Discussion: it is the relative path of this Nork, relative to its tree's root Nork, which is the "local root" shared with other Norks in the same interconnected tree. (That is to say, a local root is the highest/topmost node of a directory tree imported in a single batch.) The last element of the relFP is this Nork's own name/label, analagous to FP.Base(Path).

type InspectorFunc

type InspectorFunc func(pNode *Nork) error

type Nork

type Nork struct {

	// Name is the only field that we know works across
	// all Use Cases, and any rules are specific to UC.
	Name string
	// UC is Use Case is QuadraMode UC selector
	UC string
	// OID includes the DB table, so that [OuterOwner]
	// (the embedding super-struct) can be fetched by
	// OID directly from the DB, rather than going
	// thru some kind of in-memory router / map /
	// OID registry that figures out which table.
	oid.OID

	// Errer is here because a Nork that was created
	// successfully and linked into a tree might later
	// acquire an error due to an external change.
	FU.Errer

	// -----------
	//  GUI Stuff
	// -----------
	IsFocused, IsSelected, IsExpanded, IsVisible, IsMarked bool
	// contains filtered or unexported fields
}

Nork is an all-weather, all-terrain tree node. It adds kid node prdering on top of [Treelinks], and it provides for multiple sets of Treelinks for support advanced "Quadranode" funtionality like incoming references, attached metadata facets, and versioning.

Child nodes ("Kid"s) have an externally-defined order, implying that child nodes should not be directly compared (via [Comparable]) amongst themselves.

The ability to impose a specific specified order on child nodes is essential for representation of content markup.

In addition to a basic set of Treelinks, several additional fields are provided, and documented here to make them visible, because many of these fields are not public (i.e. capitalised).

  • Name string: the typical field for a tree node
  • links Treelinks: links built & kept in-memory
  • oidLinks Treelinks: links as persisted
  • OID oid.OID: includes the DB table, so that [OuterOwner] (the embedding super-struct) can be fetched by OID directly from the DB, rather than going thru some kind of in-memory router / map / OID registry that figures out which table.
  • Errer: collection point for errors associated with this tree node
  • UC string: Use Case, when using as a QuadraMode
  • creatPath string: bookkeeping
  • IsFocused, IsSelected, IsExpanded, IsVisible, IsMafked bool: GUI-related stuff
  • level int: this is actually stored with Treelinks, and is not appropriate here; note that zero marks a root node; note that we don't also/alternatively test on whether Prnt is nil, because we might find other uses for Prnt, such as the file that contains a document tree.) Discussion of level: It is equal to the number of "/" path separators *separating* path elements (i.e. not including any leading or trailing separators). Therefore it is 0 for an XML document root node or the local root of a file & dir tree (where in both cases, isRoot() is true and parent() is [probably] nil)), and it is >0 for others. Reserve negative numbers for future (ab)use.

.

func NewNork

func NewNork(aRelPath string) *Nork

NewNork does NOT assume an FS-type environment, and assigns an OID. OIDs are cheap, so e don't worry about assigning them to (e.g.) DOM nodes or markup nodes, which might actually need them as ToC entries. NOTE: Interaction with os.Root is TBD!

  • It expects a relative path, not absolute; "." and "./" are OK but not ""; an absolute path does NOT return an error
  • The path need not be in a filesystem, but paths are still expected to conform to package path (rather than package path/filepath): "The path package should only be used for paths separated by forward slashes, such as the paths in URLs. This package does not deal with Windows paths with drive letters or backslashes; to manipulate operating system paths, use the path/filepath package."
  • It does not attempt to resolve the path to an absolute path: this capability is not part of the `path` package
  • Its only concept is tree, not filesystem, so it mainly just records its own node name and acquires an OID
  • It implements interface [Errer], so errors can be acquired dynamically
  • There is no distinction between a "tree" Nork and a "lone" Nork

.

func (*Nork) CreatPath

func (p *Nork) CreatPath() string

func (*Nork) Debug

func (p *Nork) Debug() string

func (*Nork) Echo

func (p *Nork) Echo() string

func (*Nork) FirstKid

func (p *Nork) FirstKid() TL.Treelinkser

func (*Nork) Infos

func (p *Nork) Infos() string

func (*Nork) LinePrefixString

func (p *Nork) LinePrefixString() string

LinePrefixString provides indentation and should start a line of display/debug.

It does not end the string with (white)space. .

func (*Nork) LinksPtr

func (p *Nork) LinksPtr() *TL.Treelinks

LinksPtr overrides the same-named func of the *Treelinks, aktho it does the same thing.

func (*Nork) PrintTree

func (p *Nork) PrintTree(w io.Writer) error

type Norker

type Norker interface {
	TL.Stringser
	TL.Treelinkser
	// =====================
	//  PATHS (Rel. & Abs.)
	// =====================
	// RelPath is rel.filepath for a file/dir, and for a DOM
	// node, it is meaningless except for a [RootNorker],
	// where it is the rel.path to the containing document.
	RelPath() string
	SetRelPath(string)
	// AbsPath is abs.filepath for a file/dir, and for a DOM
	// node, the (abs.)path of the node w.r.t. the document
	// root, except for a [RootNorker], for which it is
	// the abs.path to the containing document.
	AbsPath() string
	SetAbsPath(string)
	// ======================
	//  PATH CHARACTERISTICS
	// ======================
	Root() TL.Treelinkser
	IsRoot() bool
	IsDir() bool
	IsDirlike() bool
	// ============
	//  MISCELLANY
	// ============
	LinePrefixString() string
	PrintTree(io.Writer) error
}

type StringFunc

type StringFunc func(Norker) string

StringFunc is used by interface Norker, so a method signature actually (MAYBE!) looks like:

func (*Nork) FuncName() string

type StringerFunc

type StringerFunc func(pNode *Nork) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址