File: write-tree.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 (157 lines) | stat: -rw-r--r-- 3,311 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
write-tree \-  (tree)
.SH PURPOSE
Insert a key/value pair into a tree.

.SH SYNTAX

.RS 4
.EX

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

.EE
.RE

.SH DESCRIPTION
write-tree inserts string <key> and associated string <value> into <tree> created by \fBnew-tree\fP.

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

If "new-cursor" clause is used, then a <cursor> will be positioned on a newly inserted tree node. You can use \fBuse-cursor\fP to iterate to nodes with lesser and greater key values.

.SH EXAMPLES
Insert key "k" with value "d" into "mytree", and obtain status in "st":

.RS 4
.EX

write-tree mytree key k value d status st

.EE
.RE

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

In a new directory, create file treesrv.golf and copy to it:

.RS 4
.EX

%% /treesrv public
    do-once
    new-tree 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 tree
        write-tree t key (key) value data status st
        if-true st equal GG_OKAY
            @Added [<<print-out key>>]
        else-if
            @Key exists
        end-if
    else-if op equal "delete" // Delete data and obtain the value deleted
        delete-tree t key (key) value val 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 tree based on key value
        read-tree t equal (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 new application ("pt" for "process tree"):

.RS 4
.EX

sudo mgrg -i -u $(whoami) pt

.EE
.RE

Build application:

.RS 4
.EX

gg -q

.EE
.RE

Run the tree service:

.RS 4
.EX

mgrg -w 1 pt

.EE
.RE

Try it out, add key/value pairs, query, delete, query again:

.RS 4
.EX

\[char35] Add key=1 and data=d1
$ gg -r --req="/treesrv/op=add/key=1/data=d1" --service --exec --silent-header
Added [1]

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

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

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

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

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

.EE
.RE

See \fBread-tree\fP for more examples.
.SH SEE ALSO
 Tree

\fBdelete-tree\fP  
\fBget-tree\fP  
\fBnew-tree\fP  
\fBpurge-tree\fP  
\fBread-tree\fP  
\fBuse-cursor\fP  
\fBwrite-tree\fP   
See all 
\fBdocumentation\fP