File: write-hash.2gg

package info (click to toggle)
golf 601.4.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,824 kB
  • sloc: ansic: 20,020; sh: 1,171; makefile: 292
file content (141 lines) | stat: -rw-r--r-- 3,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
write-hash \-  (hash)
.SH PURPOSE
Store key/value pair into a hash.

.SH SYNTAX

.RS 4
.EX

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

.EE
.RE

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

<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 \fBnew-hash\fP), 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 \fBread-hash\fP with delete clause).

.SH EXAMPLES
Writing data to a hash:

.RS 4
.EX

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

.EE
.RE

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

.RS 4
.EX

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

.EE
.RE

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:

.RS 4
.EX

%% /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
%%

.EE
.RE

Create and make the application, then run it as service:

.RS 4
.EX

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

.EE
.RE

Try it from a command line client (see \fBgg\fP):

.RS 4
.EX

// 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

.EE
.RE

See \fBread-hash\fP for more examples.
.SH SEE ALSO
 Hash

\fBget-hash\fP  
\fBnew-hash\fP  
\fBpurge-hash\fP  
\fBread-hash\fP  
\fBresize-hash\fP  
\fBwrite-hash\fP   
See all 
\fBdocumentation\fP