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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# -*- tcl -*-
# (C) 2006 Andreas Kupries <andreas_kupries@users.sourceforge.net>
##
# ###
package require Tcl 8.3
package require snit
package require tie
# ###
snit::type pregistry {
# API
# delete key ?attribute?
# mtime key ?attribute?
# get key attribute
# keys key ?pattern?/*
# set key ?attribute value?
# attrs key ?pattern?
option -tie -default {} -configuremethod TIE ; # Persistence
constructor {args} {
$self configurelist $args
$self INIT
return
}
# ###
method delete {key args} {
#puts DEL|$key|
if {[llength $args] > 1} {return -code error "wrong\#args"}
if {[catch {NODE $key} n]} return
if {[llength $args]} {
# Delete attribute
set attr [lindex $args 0]
set pattern [list A $n $attr *]
set km [list N $n M]
array unset data $pattern
set data($km) [clock seconds]
} else {
# Delete key and children.
#puts N|$n|
if {![llength $key]} {
return -code error "cannot delete root"
}
# Children first
foreach c [array names data [list C $n *]] {
set c [lindex $c end]
#puts _|$c|
$self delete [linsert $key end $c]
}
# And now the node itself. Modify the parent as well,
# remove this node as a child.
set self [lindex $key end]
set pidx [list N $n P]
set npat [list N $n *]
set apat [list A $n * *]
set pid $data($pidx)
set cidx [list C $pid $self]
set midx [list N $pid M]
array unset data $apat
array unset data $npat
unset -nocomplain data($cidx)
set data($midx) [clock seconds]
unset -nocomplain ncache($key)
}
return
}
method mtime {key args} {
if {[llength $args] > 1} {return -code error "wrong\#args"}
set n [NODE $key]
if {[llength $args]} {
set attr [lindex $args 0]
set idx [list A $n $attr M]
if {![info exists data($idx)]} {
return -code error "Unknown attribute \"$attr\" in key \"$key\""
}
} else {
set idx [list N $n M]
}
return $data($idx)
}
method exists {key args} {
if {[llength $args] > 1} {
return -code error "wrong\#args"
} elseif {[catch {NODE $key} n]} {
return 0
} elseif {![llength $args]} {
return 1
}
set attr [lindex $args 0]
set idx [list A $n $attr V]
return [info exist data($idx)]
}
method get {key attr} {
set n [NODE $key]
set idx [list A $n $attr V]
if {![info exists data($idx)]} {
return -code error "Unknown attribute \"$attr\" in key \"$key\""
}
return $data($idx)
}
method get||default {key attr default} {
if {[catch {NODE $key} n]} {
return $default
}
set idx [list A $n $attr V]
if {![info exists data($idx)]} {
return $default
}
return $data($idx)
}
method keys {key {pattern *}} {
set n [NODE $key]
set pattern [list C $n $pattern]
set res {}
foreach c [array names data $pattern] {
lappend res [linsert $key end $c]
}
return $res
}
method attrs {key {pattern *}} {
set n [NODE $key]
set pattern [list A $n $pattern V]
set res {}
foreach c [array names data $pattern] {
lappend res [lindex $c end-1]
}
return $res
}
method lappend {key attr value} {
set list [$self get||default $key $attr {}]
lappend list $value
$self set $key $attr $list
return
}
method set {key args} {
set n [NODE $key 1]
if {![llength $args]} return
if {[llength $args] != 2} {return -code error "wrong\#args"}
foreach {attr value} $args break
# Ignore calls which do not change the contents of the
# database.
set aidx [list A $n $attr V]
if {
[info exists data($aidx)] &&
[string equal $data($aidx) $value]
} return ; # {}
#puts stderr "$n $attr | $key | ($value)"
set aids [list A $n $attr M]
set data($aidx) $value
set data($aids) [clock seconds]
return
}
# ### state
variable data -array {}
# Tree of keys. Each keys can have multiple attributes.
# Each key, and attribute, have a modification timestamp.
# Each node in the tree is identified by a numeric id. Children
# refer to their parents. Parent id + name refers to unique child.
# Array contents
# (I) -> number id counter
# (C id name) -> id parent id x name => child id
# (N id P) -> id node id => parent id, empty for root
# (N id M) -> timestamp node id => last modification
# (A id name V) -> string node id x attribute name => value
# (A id name M) -> timestamp s.a => last modification
# This structure is less memory/space intensive than the setup of
# 1registry. It is also more difficult to query as it is less
# tabular, less redundant.
# Another thing becoming more complex is the deletion of a
# subtree. It is now necessary to walk the the tree, instead of
# just deleting all keys in the array matching a certain
# pattern. That at least can be done at the C level (array unset).
# The conversion from key list to node is also linear in key
# length, and an operation done often. Better cache it. However
# only internally, or the space savingsare gone too as the space
# is then taken by the conversion cache. Hm. Still less than
# before, as each key is listed at most once. In 1registry it was
# repeated for each of its attributes as well. This would regain
# speed for searches, as the conversion cache now is a tabular
# representation of the tree, and easily globbed.
# ### configure -tie (persistence)
method TIE {option value} {
if {[string equal $options(-tie) $value]} return
tie::untie [myvar data]
# 8.5 - tie::tie [myvar data] {expand}$value
eval [linsert $value 0 tie::tie [myvar data]]
set options(-tie) $value
return
}
method INIT {} {
if {![info exists data(I)]} {
set anchor {C {} {}}
set rootp {N 0 P}
set roots {N 0 M}
set data(I) 0
set data($anchor) 0
set data($rootp) {}
set data($roots) [clock seconds]
}
return
}
variable ncache -array {}
proc NODE {key {create 0}} {
upvar 1 ncache ncache data data
if {[info exist ncache($key)]} {
# Cached, shortcut
return $ncache($key)
}
if {![llength $key]} {
# Root, shortcut
set id 0
} else {
# Recursively convert, possibly create
set parent [lrange $key 0 end-1]
set self [lindex $key end]
set pid [NODE $parent $create]
set idx [list C $pid $self]
if {[info exists data($idx)]} {
set id $data($idx)
} elseif {!$create} {
return -code error "Unknown key \"$key\""
} else {
set id [incr data(I)]
set idxp [list N $id P]
set idxm [list N $id M]
set data($idx) $id
set data($idxp) $pid
set data($idxm) [clock seconds]
}
}
set ncache($key) $id
return $id
}
# ###
}
##
# ###
package provide pregistry 0.1
|