1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE cref SYSTEM "cref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>registry</title>
27
<prepared>Gordon Beaton</prepared>
28
<responsible>Gordon Beaton</responsible>
30
<approved>Gordon Beaton</approved>
31
<checked>Gordon Beaton</checked>
34
<file>registry.sgml</file>
37
<libsummary>Store and backup key-value pairs</libsummary>
39
<p>This module provides support for storing key-value
40
pairs in a table known as a registry, backing up registries to
41
Mnesia in an atomic manner, and later restoring the contents of a
42
registry from Mnesia.</p>
46
<name><ret>ei_reg *</ret><nametext>ei_reg_open(size)</nametext></name>
47
<fsummary>Create and open a registry</fsummary>
52
<p>Open (create) a registry. The registry will be
53
initially empty. Use <c><![CDATA[ei_reg_close()]]></c> to close the registry
56
<p><c><![CDATA[size]]></c> is the approximate number of objects you intend
57
to store in the registry. Since the registry uses a hash table
58
with collision chaining, there is no absolute upper limit on the
59
number of objects that can be stored in it. However for reasons
60
of efficiency, it is a good idea to choose a number that is
61
appropriate for your needs. It is possible to use
62
<c><![CDATA[ei_reg_resize()]]></c> to change the size later. Note that the
63
number you provide will be increased to the nearest larger prime
66
<p>On success, an empty registry will be returned. On failure, NULL
71
<name><ret>int</ret><nametext>ei_reg_resize(reg,newsize)</nametext></name>
72
<fsummary>Resize a registry</fsummary>
78
<p>Change the size of a registry.
80
<p><c><![CDATA[newsize]]></c> is the new size to make the registry. The
81
number will be increased to the nearest larger prime number.
83
<p>On success, the registry will be resized, all contents
84
rehashed, and the function will return 0. On failure, the
85
registry will be left unchanged and the function will return -1.</p>
89
<name><ret>int</ret><nametext>ei_reg_close(reg)</nametext></name>
90
<fsummary>Close a registry </fsummary>
95
<p>A registry that has previously been created with
96
<c><![CDATA[ei_reg_open()]]></c> is closed, and all the objects it contains
99
<p><c><![CDATA[reg]]></c> is the registry to close.
101
<p>The function returns 0.</p>
105
<name><ret>int</ret><nametext>ei_reg_setival(reg,key,i)</nametext></name>
106
<fsummary>Assign an integer object</fsummary>
109
<v>const char *key;</v>
113
<p>Create a key-value pair with the specified <c><![CDATA[key]]></c> and integer
114
value <c><![CDATA[i]]></c>. If an object already existed with the same
115
<c><![CDATA[key]]></c>, the new value replaces the old one. If the previous
116
value was a binary or string, it is freed with <c><![CDATA[free()]]></c>.
118
<p><c><![CDATA[reg]]></c> is the registry where the object should be placed.
120
<p><c><![CDATA[key]]></c> is the name of the object.
122
<p><c><![CDATA[i]]></c> is the integer value to assign.
124
<p>The function returns 0 on success, or -1 on failure.</p>
128
<name><ret>int</ret><nametext>ei_reg_setfval(reg,key,f)</nametext></name>
129
<fsummary>Assign a floating point object</fsummary>
132
<v>const char *key;</v>
136
<p>Create a key-value pair with the specified <c><![CDATA[key]]></c> and
137
floating point value <c><![CDATA[f]]></c>. If an object already existed with
138
the same <c><![CDATA[key]]></c>, the new value replaces the old one. If the
139
previous value was a binary or string, it is freed with <c><![CDATA[free()]]></c>.
141
<p><c><![CDATA[reg]]></c> is the registry where the object should be placed.
143
<p><c><![CDATA[key]]></c> is the name of the object.
145
<p><c><![CDATA[f]]></c> is the floating point value to assign.
147
<p>The function returns 0 on success, or -1 on failure.</p>
151
<name><ret>int</ret><nametext>ei_reg_setsval(reg,key,s)</nametext></name>
152
<fsummary>Assign a string object</fsummary>
155
<v>const char *key;</v>
156
<v>const char *s;</v>
159
<p>Create a key-value pair with the specified <c><![CDATA[key]]></c> whose
160
"value" is the specified string <c><![CDATA[s]]></c>. If an object already
161
existed with the same <c><![CDATA[key]]></c>, the new value replaces the old
162
one. If the previous value was a binary or string, it is freed
163
with <c><![CDATA[free()]]></c>.
165
<p><c><![CDATA[reg]]></c> is the registry where the object should be placed.
167
<p><c><![CDATA[key]]></c> is the name of the object.
169
<p><c><![CDATA[s]]></c> is the string to assign. The string itself
170
must have been created through a single call to <c><![CDATA[malloc()]]></c> or
171
similar function, so that the registry can later delete it if
172
necessary by calling <c><![CDATA[free()]]></c>.
174
<p>The function returns 0 on success, or -1 on failure.</p>
178
<name><ret>int</ret><nametext>ei_reg_setpval(reg,key,p,size)</nametext></name>
179
<fsummary>Assign a binary object</fsummary>
182
<v>const char *key;</v>
183
<v>const void *p;</v>
187
<p>Create a key-value pair with the specified <c><![CDATA[key]]></c> whose
188
"value" is the binary object pointed to by <c><![CDATA[p]]></c>. If an
189
object already existed with the same <c><![CDATA[key]]></c>, the new value
190
replaces the old one. If the previous value was a binary or
191
string, it is freed with <c><![CDATA[free()]]></c>.
193
<p><c><![CDATA[reg]]></c> is the registry where the object should be placed.
195
<p><c><![CDATA[key]]></c> is the name of the object.
197
<p><c><![CDATA[p]]></c> is a pointer to the binary object. The object itself
198
must have been created through a single call to <c><![CDATA[malloc()]]></c> or
199
similar function, so that the registry can later delete it if
200
necessary by calling <c><![CDATA[free()]]></c>.
202
<p><c><![CDATA[size]]></c> is the length in bytes of the binary object.
204
<p>The function returns 0 on success, or -1 on failure.</p>
208
<name><ret>int</ret><nametext>ei_reg_setval(reg,key,flags,v,...)</nametext></name>
209
<fsummary>Assign a value to any object type</fsummary>
212
<v>const char *key;</v>
217
<p>Create a key-value pair with the specified <c><![CDATA[key]]></c> whose
218
value is specified by <c><![CDATA[v]]></c>. If an object already
219
existed with the same <c><![CDATA[key]]></c>, the new value replaces the old
220
one. If the previous value was a binary or string, it is freed
221
with <c><![CDATA[free()]]></c>.
223
<p><c><![CDATA[reg]]></c> is the registry where the object should be placed.
225
<p><c><![CDATA[key]]></c> is the name of the object.
227
<p><c><![CDATA[flags]]></c> indicates the type of the object specified by
228
<c><![CDATA[v]]></c>. Flags must be one of EI_INT, EI_FLT, EI_STR and
229
EI_BIN, indicating whether <c><![CDATA[v]]></c> is <c><![CDATA[int]]></c>, <c><![CDATA[double]]></c>,
230
<c><![CDATA[char*]]></c> or <c><![CDATA[void*]]></c>. If <c><![CDATA[flags]]></c> is EI_BIN, then a
231
fifth argument <c><![CDATA[size]]></c> is required, indicating the size
232
in bytes of the object pointed to by <c><![CDATA[v]]></c>.
234
<p>If you wish to store an arbitrary pointer in the registry,
235
specify a <c><![CDATA[size]]></c> of 0. In this case, the object itself will
236
not be transferred by an <c><![CDATA[ei_reg_dump()]]></c> operation, just
239
<p>The function returns 0 on success, or -1 on failure.</p>
243
<name><ret>int</ret><nametext>ei_reg_getival(reg,key)</nametext></name>
244
<fsummary>Get an integer object</fsummary>
247
<v>const char *key;</v>
250
<p>Get the value associated with <c><![CDATA[key]]></c> in the
251
registry. The value must be an integer.
253
<p><c><![CDATA[reg]]></c> is the registry where the object will be looked
256
<p><c><![CDATA[key]]></c> is the name of the object to look up.
258
<p>On success, the function returns the value associated with <c><![CDATA[key]]></c>.
259
If the object was not found or it was not an integer
260
object, -1 is returned. To avoid problems with in-band error
261
reporting (i.e. if you cannot distinguish between -1 and a
262
valid result) use the more general function <c><![CDATA[ei_reg_getval()]]></c>
267
<name><ret>double</ret><nametext>ei_reg_getfval(reg,key)</nametext></name>
268
<fsummary>Get a floating point object</fsummary>
271
<v>const char *key;</v>
274
<p>Get the value associated with <c><![CDATA[key]]></c> in the
275
registry. The value must be a floating point type.
277
<p><c><![CDATA[reg]]></c> is the registry where the object will be looked
280
<p><c><![CDATA[key]]></c> is the name of the object to look up.
282
<p>On success, the function returns the value associated with <c><![CDATA[key]]></c>.
283
If the object was not found or it was not a floating point
284
object, -1.0 is returned. To avoid problems with in-band error
285
reporting (i.e. if you cannot distinguish between -1.0 and a
286
valid result) use the more general function <c><![CDATA[ei_reg_getval()]]></c>
291
<name><ret>const char *</ret><nametext>ei_reg_getsval(reg,key)</nametext></name>
292
<fsummary>Get a string object</fsummary>
295
<v>const char *key;</v>
298
<p>Get the value associated with <c><![CDATA[key]]></c> in the
299
registry. The value must be a string.
301
<p><c><![CDATA[reg]]></c> is the registry where the object will be looked
304
<p><c><![CDATA[key]]></c> is the name of the object to look up.
306
<p>On success, the function returns the value associated with
307
<c><![CDATA[key]]></c>. If the object was not found or it was not a string,
308
NULL is returned. To avoid problems with in-band error
309
reporting (i.e. if you cannot distinguish between NULL and a
310
valid result) use the more general function <c><![CDATA[ei_reg_getval()]]></c>
315
<name><ret>const void *</ret><nametext>ei_reg_getpval(reg,key,size)</nametext></name>
316
<fsummary>Get a binary object</fsummary>
319
<v>const char *key;</v>
323
<p>Get the value associated with <c><![CDATA[key]]></c> in the
324
registry. The value must be a binary (pointer) type.
326
<p><c><![CDATA[reg]]></c> is the registry where the object will be looked
329
<p><c><![CDATA[key]]></c> is the name of the object to look up.
331
<p><c><![CDATA[size]]></c> will be initialized to contain the length in
332
bytes of the object, if it is found.
334
<p>On success, the function returns the value associated with
335
<c><![CDATA[key]]></c> and indicates its length in <c><![CDATA[size]]></c>.
336
If the object was not found or it was not a binary object,
337
NULL is returned. To avoid problems with in-band error
338
reporting (i.e. if you cannot distinguish between NULL and a
339
valid result) use the more general function <c><![CDATA[ei_reg_getval()]]></c>
344
<name><ret>int</ret><nametext>ei_reg_getval(reg,key,flags,v,...)</nametext></name>
345
<fsummary>Get any object</fsummary>
348
<v>const char *key;</v>
350
<v>void *v (see below)</v>
353
<p>This is a general function for retrieving any kind of
354
object from the registry.
356
<p><c><![CDATA[reg]]></c> is the registry where the object will be looked
359
<p><c><![CDATA[key]]></c> is the name of the object to look up.
361
<p><c><![CDATA[flags]]></c> indicates the type of object that you are
362
looking for. If <c><![CDATA[flags]]></c> is 0, then any kind of object will
363
be returned. If <c><![CDATA[flags]]></c> is one of EI_INT, EI_FLT, EI_STR or
364
EI_BIN, then only values of that kind will be returned. The
365
buffer pointed to by <c><![CDATA[v]]></c> must be large enough to hold the return
366
data, i.e. it must be a pointer to one of <c><![CDATA[int]]></c>,
367
<c><![CDATA[double]]></c>, <c><![CDATA[char*]]></c> or <c><![CDATA[void*]]></c>, respectively. Also,
368
if <c><![CDATA[flags]]></c> is EI_BIN, then a fifth argument <c><![CDATA[int *size]]></c> is required, so that the size of the object can be
371
<p>If the function succeeds, <c><![CDATA[v]]></c> (and <c><![CDATA[size]]></c> if the
372
object is binary) will be initialized with the value associated
373
with <c><![CDATA[key]]></c>, and the function will return one of EI_INT,
374
EI_FLT, EI_STR or EI_BIN, indicating the type of object. On failure the
375
function will return -1 and the arguments will not be updated.</p>
379
<name><ret>int</ret><nametext>ei_reg_markdirty(reg,key)</nametext></name>
380
<fsummary>Mark an object as dirty </fsummary>
383
<v>const char *key;</v>
386
<p>Mark a registry object as dirty. This will ensure that
387
it is included in the next backup to Mnesia. Normally this
388
operation will not be necessary since all of the normal registry
389
'set' functions do this automatically. However if you have
390
retrieved the value of a string or binary object from the
391
registry and modified the contents, then the change will be
392
invisible to the registry and the object will be assumed to be
393
unmodified. This function allows you to make such modifications
394
and then let the registry know about them.
396
<p><c><![CDATA[reg]]></c> is the registry containing the object.
398
<p><c><![CDATA[key]]></c> is the name of the object to mark.
400
<p>The function returns 0 on success, or -1 on failure.</p>
404
<name><ret>int</ret><nametext>ei_reg_delete(reg,key)</nametext></name>
405
<fsummary>Delete an object from the registry</fsummary>
408
<v>const char *key;</v>
411
<p>Delete an object from the registry. The object is not
412
actually removed from the registry, it is only marked for later
413
removal so that on subsequent backups to Mnesia, the
414
corresponding object can be removed from the Mnesia table as
415
well. If another object is later created with the same key, the
416
object will be reused.
418
<p>The object will be removed from the registry after a call to
419
<c><![CDATA[ei_reg_dump()]]></c> or <c><![CDATA[ei_reg_purge()]]></c>.
421
<p><c><![CDATA[reg]]></c> is the registry containing <c><![CDATA[key]]></c>.
423
<p><c><![CDATA[key]]></c> is the object to remove.
425
<p>If the object was found, the function returns 0 indicating
426
success. Otherwise the function returns -1.</p>
430
<name><ret>int</ret><nametext>ei_reg_stat(reg,key,obuf)</nametext></name>
431
<fsummary>Get object information</fsummary>
434
<v>const char *key;</v>
435
<v>struct ei_reg_stat *obuf;</v>
438
<p>Return information about an object.
440
<p><c><![CDATA[reg]]></c> is the registry containing the object.
442
<p><c><![CDATA[key]]></c> is the name of the object.
444
<p><c><![CDATA[obuf]]></c> is a pointer to an <c><![CDATA[ei_reg_stat]]></c> structure,
447
<code type="none"><![CDATA[
453
<p>In <c><![CDATA[attr]]></c> the object's attributes are stored as the logical
454
OR of its type (one of EI_INT, EI_FLT, EI_BIN and EI_STR),
455
whether it is marked for deletion (EI_DELET) and whether it has
456
been modified since the last backup to Mnesia (EI_DIRTY).
458
<p>The <c><![CDATA[size]]></c> field indicates the size in bytes required to store
459
EI_STR (including the terminating 0) and EI_BIN objects, or 0
460
for EI_INT and EI_FLT.
462
<p>The function returns 0 and initializes <c><![CDATA[obuf]]></c> on
463
success, or returns -1 on failure.</p>
467
<name><ret>int</ret><nametext>ei_reg_tabstat(reg,obuf)</nametext></name>
468
<fsummary>Get registry information</fsummary>
471
<v>struct ei_reg_tabstat *obuf;</v>
474
<p>Return information about a registry. Using information
475
returned by this function, you can see whether the size of the
476
registry is suitable for the amount of data it contains.
478
<p><c><![CDATA[reg]]></c> is the registry to return information about.
480
<p><c><![CDATA[obuf]]></c> is a pointer to an <c><![CDATA[ei_reg_tabstat]]></c> structure,
483
<code type="none"><![CDATA[
484
struct ei_reg_tabstat {
491
<p>The <c><![CDATA[size]]></c> field indicates the number of hash positions
492
in the registry. This is the number you provided when you
493
created or last resized the registry, rounded up to the nearest
496
<p><c><![CDATA[nelem]]></c> indicates the number of elements stored in the
497
registry. It includes objects that are deleted but not purged.
499
<p><c><![CDATA[npos]]></c> indicates the number of unique positions that are
500
occupied in the registry.
502
<p><c><![CDATA[collisions]]></c> indicates how many elements are sharing
503
positions in the registry.
505
<p>On success, the function returns 0 and <c><![CDATA[obuf]]></c> is
506
initialized to contain table statistics. On failure, the function
511
<name><ret>int</ret><nametext>ei_reg_dump(fd,reg,mntab,flags)</nametext></name>
512
<fsummary>Back up a registry to Mnesia</fsummary>
516
<v>const char *mntab;</v>
520
<p>Dump the contents of a registry to a Mnesia table in an
521
atomic manner, i.e. either all data will be updated, or none of
522
it will. If any errors are encountered while backing up
523
the data, the entire operation is aborted.
525
<p><c><![CDATA[fd]]></c> is an open connection to Erlang.
526
Mnesia 3.0 or later must be running on the Erlang node.
528
<p><c><![CDATA[reg]]></c> is the registry to back up.
530
<p><c><![CDATA[mntab]]></c> is the name of the Mnesia table where the backed
531
up data should be placed. If the table does not exist, it will
532
be created automatically using configurable defaults. See your
533
Mnesia documentation for information about configuring this
536
<p>If <c><![CDATA[flags]]></c> is 0, the backup will include only those
537
objects which have been created, modified or deleted since the
538
last backup or restore (i.e. an incremental backup). After the
539
backup, any objects that were marked dirty are now clean, and any
540
objects that had been marked for deletion are deleted.
542
<p>Alternatively, setting flags to EI_FORCE will cause a full
543
backup to be done, and EI_NOPURGE will cause the deleted objects
544
to be left in the registry afterwards. These can be bitwise ORed
545
together if both behaviours are desired. If EI_NOPURGE was
546
specified, you can use <c><![CDATA[ei_reg_purge()]]></c> to explicitly remove
547
the deleted items from the registry later.
549
<p>The function returns 0 on success, or -1 on failure.</p>
553
<name><ret>int</ret><nametext>ei_reg_restore(fd,reg,mntab)</nametext></name>
554
<fsummary>Restore a registry from Mnesia</fsummary>
558
<v>const char *mntab;</v>
561
<p>The contents of a Mnesia table are read into the
564
<p><c><![CDATA[fd]]></c> is an open connection to Erlang.
565
Mnesia 3.0 or later must be running on the Erlang node.
567
<p><c><![CDATA[reg]]></c> is the registry where the data should be placed.
569
<p><c><![CDATA[mntab]]></c> is the name of the Mnesia table to read data
572
<p>Note that only tables of a certain format can be
573
restored, i.e. those that have been created and backed up to
574
with <c><![CDATA[ei_reg_dump()]]></c>. If the registry was not empty before
575
the operation, then the contents of the table are added to the
576
contents of the registry. If the table contains objects with the
577
same keys as those already in the registry, the registry objects
578
will be overwritten with the new values. If the registry
579
contains objects that were not in the table, they will be
580
unchanged by this operation.
582
<p>After the restore operation, the entire contents of the
583
registry is marked as unmodified. Note that this includes any
584
objects that were modified before the restore and not
585
overwritten by the restore.
587
<p>The function returns 0 on success, or -1 on failure.</p>
591
<name><ret>int</ret><nametext>ei_reg_purge(reg)</nametext></name>
592
<fsummary>Remove deleted objects</fsummary>
597
<p>Remove all objects marked for deletion. When objects
598
are deleted with <c><![CDATA[ei_reg_delete()]]></c> they are not actually
599
removed from the registry, only marked for later removal. This
600
is so that on a subsequent backup to Mnesia, the
601
objects can also be removed from the Mnesia table. If you are
602
not backing up to Mnesia then you may wish to remove the objects
603
manually with this function.
605
<p><c><![CDATA[reg]]></c> is a registry containing objects marked for
608
<p>The function returns 0 on success, or -1 on failure.</p>