GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

✨ NAME ✨

write-tree β€” the one and only (tree) moment πŸ’…


πŸŽ€ PURPOSE πŸŽ€

Slip a key/value pair into a tree. That's it, that's the whole job, and she does it flawlessly. 🌸


πŸ¦‹ SYNTAX πŸ¦‹

write-tree <tree> key <key> value <value> \
    [ status <status> ] \
    [ new-cursor <cursor> ]

πŸ’– DESCRIPTION πŸ’–

write-tree inserts the string <key> and its associated string <value> into <tree> β€” and bestie, that tree has to be one you made with new-tree first, okay? No tree, no vibes. 🧚

If <key> is already in <tree>, then <status> (the one from the "status" clause) will be GG_ERR_EXIST and nothing gets inserted into <tree>. Otherwise it's GG_OKAY. She's telling you up front. Not a surprise, not a plot twist, just the rules. πŸ’€

And if you use the "new-cursor" clause? Then a <cursor> gets positioned right on the newly inserted tree node. You can then use use-cursor to iterate to nodes with lesser and greater key values. It's giving main character cursor energy, fr fr. πŸ‘‘


🌈 EXAMPLES 🌈

Insert key "k" with value "d" into "mytree", and obtain status in "st":

write-tree mytree key k value d status st

The following is an example of a process-scoped tree. Such a tree keeps its data across the requests, for as long as the process is alive. No cap. 🫢

In a new directory, create file treesrv.golf and copy to it:

%% /treesrv public
    do-once
    new-tree t process-scope
    end-do-once

    // Get input parameters
    get-param op
    get-param key
    get-param data

    if-true op equal "add" // Add data to tree
        write-tree t key (key) value data status st
        if-true st equal GG_OKAY
            @Added [<<print-out key>>]
        else-if
            @Key exists
        end-if
    else-if op equal "delete" // Delete data and obtain the value deleted
        delete-tree t key (key) value val status st
        if-true st equal GG_ERR_EXIST
            @Not found [<<print-out key>>]
        else-if
            // If found, then delete key and value
            @Deleted [<<print-out val>>]
            delete-string val
        end-if
    else-if op equal "query" // Query tree based on key value
        read-tree t equal (key) value val status st
        if-true st equal GG_ERR_EXIST
            @Not found, queried [<<print-out key>>]
        else-if
            @Value [<<print-out val>>]
        end-if
    end-if
%%

Create new application ("pt" for "process tree"):

sudo mgrg -i -u $(whoami) pt

Build application:

gg -q

Run the tree service:

mgrg -w 1 pt

Try it out, add key/value pairs, query, delete, query again:

# Add key=1 and data=d1
$ gg -r --req="/treesrv/op=add/key=1/data=d1" --service --exec --silent-header
Added [1]

# Add key=2 and data=d2
$ gg -r --req="/treesrv/op=add/key=2/data=d2" --service --exec --silent-header
Added [2]

# Query key=1
$ gg -r --req="/treesrv/op=query/key=1" --service --exec --silent-header
Value [d1]

# Query key=2
i$ gg -r --req="/treesrv/op=query/key=2" --service --exec --silent-header
Value [d2]

# Delete key=2
$ gg -r --req="/treesrv/op=delete/key=2" --service --exec --silent-header
Deleted [d2]

# Query key=2
$ gg -r --req="/treesrv/op=query/key=2" --service --exec --silent-header
Not found, queried [2]

See read-tree for more examples. She's got so many more examples, go look. ✨


🌟 SEE ALSO 🌟

  • Tree

delete-tree get-tree new-tree purge-tree read-tree use-cursor write-tree

See all documentation πŸ“š


Okay bestie, that's the whole page β€” GOLF(2gg) from top to bottom, brought to you by the Development crew. This lil masterpiece was stamped $DATE and it's living at version $VERSION. Stan. πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from