GOLF(2gg)

NAME
write-tree - (tree)

PURPOSE
Insert a key/value pair into a tree.

SYNTAX

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

DESCRIPTION
write-tree inserts string <key> and associated string <value> into <tree> created by new-tree.

If <key> already exists in <tree>, <status> (in "status" clause) will be GG_ERR_EXIST and nothing is inserted into <tree>, otherwise it is GG_OKAY.

If "new-cursor" clause is used, then a <cursor> will be positioned on a newly inserted tree node. You can use use-cursor to iterate to nodes with lesser and greater key values.

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.

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.

SEE ALSO
 Tree

delete-tree get-tree new-tree purge-tree read-tree use-cursor write-tree See all documentation