Write index

Purpose: Insert a key/value pair into an index.

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

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

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

If "new-cursor" clause is used, then a <cursor> will be positioned on a newly inserted index node. You can use use-cursor to iterate to nodes with lesser and greater key values.
Examples
Insert key "k" with value "d" into "myindex", and obtain status in "st":
 write-index myindex key k value d status st

The following is an example of a process-scoped index. Such a index keeps its data across the requests, for as long as the process is alive.

In a new directory, create file indexsrv.gliim and copy to it:
 %% /indexsrv public
     do-once
     new-index 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 index
         write-index t key (key) value data status st
         if-true st equal GG_OKAY
             @Added [<<p-out key>>]
         else-if
             @Key exists
         end-if
     else-if op equal "delete" // Delete data and obtain the value deleted
         delete-index t key (key) value val 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 index based on key value
         read-index t equal (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 new application ("pi" for "process index"):
sudo mgrg -i -u $(whoami) pi

Build application:
gg -q

Run the index service:
mgrg -w 1 pi

Try it out, add key/value pairs, query, delete, query again:
# Add key=1 and data=d1
$ gg -r --req="/indexsrv/op=add/key=1/data=d1" --service --exec --silent-header
Added [1]

# Add key=2 and data=d2
$ gg -r --req="/indexsrv/op=add/key=2/data=d2" --service --exec --silent-header
Added [2]

# Query key=1
$ gg -r --req="/indexsrv/op=query/key=1" --service --exec --silent-header
Value [d1]

# Query key=2
i$ gg -r --req="/indexsrv/op=query/key=2" --service --exec --silent-header
Value [d2]

# Delete key=2
$ gg -r --req="/indexsrv/op=delete/key=2" --service --exec --silent-header
Deleted [d2]

# Query key=2
$ gg -r --req="/indexsrv/op=query/key=2" --service --exec --silent-header
Not found, queried [2]

See read-index for more examples.
See also
Index
delete-index  
get-index  
new-index  
purge-index  
read-index  
use-cursor  
write-index  
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.