Write tree

Purpose: Insert a key/value pair into a tree.

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

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 RIM_ERR_EXIST and nothing is inserted into <tree>, otherwise it is RIM_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.rim 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 default-value ""
    get-param data default-value ""

    if-true op equal "add" // Add data to tree
        write-tree t key (key) value data status st
        if-true st equal RIM_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 RIM_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 RIM_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"):
rim -k pt

Build application:
rim -q

Run the tree service:
mrim -w 1 pt

Try it out, add key/value pairs, query, delete, query again:
# Add key=1 and data=d1
rim -r --req="/treesrv/op=add/key=1/data=d1" --service --exec --silent-header

# Add key=2 and data=d2
rim -r --req="/treesrv/op=add/key=2/data=d2" --service --exec --silent-header

# Query key=1
rim -r --req="/treesrv/op=query/key=1" --service --exec --silent-header

# Query key=2
rim -r --req="/treesrv/op=query/key=2" --service --exec --silent-header

# Delete key=2
rim -r --req="/treesrv/op=delete/key=2" --service --exec --silent-header

# Query key=2
rim -r --req="/treesrv/op=query/key=2" --service --exec --silent-header

The result:
Added [1]
Added [2]
Value [d1]
Value [d2]
Deleted [d2]
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


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.