File: write-array.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 (125 lines) | stat: -rw-r--r-- 3,213 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
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
write-array \-  (array)
.SH PURPOSE
Store value into an array.

.SH SYNTAX

.RS 4
.EX

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

.EE
.RE

.SH DESCRIPTION
write-array will store a <value> (in "value" clause) under index <key> (in "key" clause) into <array>, which must be created with \fBnew-array\fP. <key> is a number from 0 up to (excluding) the currently allocated array size (see \fBnew-array\fP). The type of <value> is determined when <array> is created (see "type" clause), and can be either a string, number or a boolean.

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

The old value associated under index <key> is returned in <old value> (in "old-value" clause) and <value> will replace the old value.

If an <array> was created with "process-scope" clause (see \fBnew-array\fP), then the element <value> will not be freed when the current request ends, rather it will persist while the process runs, unless deleted (see \fBread-array\fP with delete clause).

.SH EXAMPLES
Writing data to an array:

.RS 4
.EX

new-array arr
write-array arr key 100 value "some data"

.EE
.RE

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

.RS 4
.EX

write-array arr key 100 value "new data" old-value od
@Previous value for this key index is <<print-out od>>

.EE
.RE

The following is an array service, where a process-scoped array is created. It provides inserting, deleting and querying indexed keys. Maximum number of keys it holds is 10,000,000 (indexed from 0 to 9,999,999). Such a service process can run indefinitely. Create file arrsrv.golf:

.RS 4
.EX

%% /arrsrv public
    do-once
    new-array arr max-size 10000000 process-scope type string
    end-do-once

    // Get input parameters
    get-param op
    get-param key
    get-param data
    // Convert string keye to number
    string-number key to key_n

    if-true op equal "add" // Add data to array
        write-array arr key key_n value data old-value old_data 
        delete-string old_data
        @Added [<<print-out key>>]
    else-if op equal "delete" // Delete data and obtain the value deleted
        read-array arr key key_n value val delete 
        @Deleted [<<print-out val>>]
        delete-string val
    else-if op equal "query" // Query hash based on key value
        read-array arr key key_n value val 
        @Value [<<print-out val>>]
    end-if
%%

.EE
.RE

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

.RS 4
.EX

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

.EE
.RE

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

.RS 4
.EX

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

.EE
.RE

See \fBread-array\fP for more examples.
.SH SEE ALSO
 Array

\fBnew-array\fP  
\fBpurge-array\fP  
\fBread-array\fP  
\fBwrite-array\fP   
See all 
\fBdocumentation\fP