Write array

Purpose: Store key/value pair into an array.

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

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

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

If <key> already exists in the array table, 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 an <array> was created with "process-scope" clause (see new-array), 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-array with delete clause).
Examples
Writing data to array:
 new-array h
 write-array h key "mykey" value "some data"

Writing new value with the same key and obtaining the previous value (which is "some data"):
 write-array h key "mykey" value "new data" status st old-value od
 if-true st equal GG_INFO_EXIST
     @Previous value for this key is <<p-out od>>
 end-if

The following is an array key/value service, where a process-scoped array is created. It provides inserting, deleting and querying key/value pairs. Such a service process can run indefinitely:
 %% /arraysrv public
     do-once
     new-array 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 array, make a copy as input params are request-scoped
         write-array 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 [<<p-out key>>]
     else-if op equal "delete" // Delete data and obtain the value deleted
         read-array h key (key) value val delete status st
         if-true st equal GG_ERR_EXIST
             @Not found [<<p-out key>>]
         else-if
             // If found, then delete key and value
             @Deleted [<<p-out val>>]
             delete-string val
         end-if
     else-if op equal "query" // Query array based on key value
         read-array h key (key) value val status st
         if-true st equal GG_ERR_EXIST
             @Not found, queried [<<p-out key>>]
         else-if
             @Value [<<p-out val>>]
         end-if
     end-if
 %%

Create and make the application, then run it as service:
// Create application
sudo mgrg -i -u $(whoami) arr
// Make application
gg -q
// Start application (single process key service)
mgrg -w 1 arr

Try it from a command line client (see gg):
// Add data
gg -r --req="/arraysrv/op=add/key=15/data=15" --service --app="/arr" --exec
// Query data
gg -r --req="/arraysrv/op=query/key=15" --service --app="/arr" --exec
// Delete data
gg -r --req="/arraysrv/op=delete/key=15" --service --app="/arr" --exec

See read-array for more examples.
See also
Array
get-array  
new-array  
purge-array  
read-array  
resize-array  
write-array  
See all
documentation


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