GOLF(2gg)

NAME
write-hash - (hash)

PURPOSE
Store key/value pair into a hash.

SYNTAX

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

DESCRIPTION
write-hash will store string <key> (in "key" clause) and <value> (in "value" clause) into <hash>, which must be created with new-hash.

<key> and <value> are collectively called an "element".

If <key> already exists in the hash, then the old value associated with it is returned in string <old value> (in "old-value" clause) and <value> will replace the old value - in this case <status> number (in "status" clause) has a value of GG_INFO_EXIST.

If <key> did not exist, <status> will be GG_OKAY and <old value> is unchanged.

If a <hash> was created with "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 delete clause).

EXAMPLES
Writing data to a hash:

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

Writing 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.

SEE ALSO
 Hash

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