GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” write-hash πŸ’–

okay bestie, welcome to the 2gg manual, this one's the write-hash page and it's literally the moment you've been waiting for πŸŽ€


✨ NAME ✨

write-hash β€” (hash)


🌸 PURPOSE 🌸

Store a key/value pair into a hash. that's it, that's the whole vibe πŸ’…


πŸŽ€ SYNTAX πŸŽ€

write-hash <hash> \
    key <key> \
    value <value> \
    [ status <status> ] \
    [ old-value <old value> ]

πŸ¦‹ DESCRIPTION πŸ¦‹

write-hash will store the string <key> (in the key clause) and <value> (in the value clause) into <hash>, and like… that hash must have been created with new-hash first, no cap. πŸ”₯

<key> and <value> are collectively called an "element". iconic duo, honestly.

If <key> already exists in the hash, then the old value associated with it gets returned in the string <old value> (in the old-value clause) and <value> will replace the old value β€” and in that case the <status> number (in the status clause) has a value of GG_INFO_EXIST. πŸ’€ she said out with the old.

If <key> did not exist, <status> will be GG_OKAY and <old value> is unchanged. nothing moved, nothing changed, we love a chill outcome 🌈

If a <hash> was created with the "process-scope" clause (see new-hash), then the element (including <key> and <value>) will not be freed when the current request ends β€” rather it will persist while the process runs, unless deleted (see read-hash with the delete clause). main character energy: it stays for the whole arc πŸ‘‘


πŸ’œ EXAMPLES πŸ’œ

Writing data to a hash:

new-hash h
write-hash h key "mykey" value "some data"

Writing a new value with the same key and obtaining the previous value (which is "some data"):

write-hash h key "mykey" value "new data" status st old-value od
if-true st equal GG_INFO_EXIST
    @Previous value for this key is <<print-out od>>
end-if

The following is a key/value service, where a process-scoped hash is created. It provides inserting, deleting and querying key/value pairs. Such a service process can run indefinitely. Create file keysrv.golf:

%% /keysrv public
    do-once
    new-hash h hash-size 1024 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 hash
        write-hash h key key value data old-value old_data status st
        if-true st equal GG_INFO_EXIST
            delete-string old_data
        end-if
        @Added [<<print-out key>>]
    else-if op equal "delete" // Delete data and obtain the value deleted
        read-hash h key (key) value val delete 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 hash based on key value
        read-hash h key (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 and make the application, then run it as service:

// Create application
gg -k hash
// Make application
gg -q
// Start application (single process key service)
mgrg -w 1 hash

Try it from a command line client (see gg):

// Add data
gg -r --req="/keysrv/op=add/key=15/data=15" --service --app="/hash" --exec
// Query data
gg -r --req="/keysrv/op=query/key=15" --service --app="/hash" --exec
// Delete data
gg -r --req="/keysrv/op=delete/key=15" --service --app="/hash" --exec

See read-hash for more examples. go look, she's got receipts 🧚


πŸ‘‘ SEE ALSO πŸ‘‘

Hash

get-hash new-hash purge-hash read-hash resize-hash write-hash See all documentation


that's a wrap, bestie πŸ’– this page came from the $VERSION of the docs, dated $DATE, and yeah it's filed under GOLF(2gg) 🫢 go write some hashes, you're gonna slay ⭐🌸
πŸ“¦ where this page comes from