Erl_Interface

Reference Manual

Version 3.11

Table of Contents

registry

C Library

registry

Library Summary

Store and back up key-value pairs.

Description

This module provides support for storing key-value pairs in a table known as a registry, backing up registries to Mnesia in an atomic manner, and later restoring the contents of a registry from Mnesia.

Exports

int ei_reg_close(
reg)

Types

ei_reg *reg;

A registry that has previously been created with ei_reg_open() is closed, and all the objects it contains are freed.

reg is the registry to close.

Returns 0.

int ei_reg_delete(
reg,
key)

Types

ei_reg *reg;
const char *key;

Deletes an object from the registry. The object is not removed from the registry, it is only marked for later removal so that on later backups to Mnesia, the corresponding object can be removed from the Mnesia table as well. If another object is later created with the same key, the object will be reused.

The object is removed from the registry after a call to ei_reg_dump() or ei_reg_purge().

  • reg is the registry containing key.
  • key is the object to remove.

Returns 0 on success, otherwise -1.

int ei_reg_dump(
fd,
reg,
mntab,
flags)

Types

int fd;
ei_reg *reg;
const char *mntab;
int flags;

Dumps the contents of a registry to a Mnesia table in an atomic manner, that is, either all data or no data is updated. If any errors are encountered while backing up the data, the entire operation is aborted.

  • fd is an open connection to Erlang. Mnesia 3.0 or later must be running on the Erlang node.
  • reg is the registry to back up.
  • mntab is the name of the Mnesia table where the backed up data is to be placed. If the table does not exist, it is created automatically using configurable defaults. For information about configuring this behavior, see Mnesia.

If flags is 0, the backup includes only those objects that have been created, modified, or deleted since the last backup or restore (that is, an incremental backup). After the backup, any objects that were marked dirty are now clean, and any objects that had been marked for deletion are deleted.

Alternatively, setting flags to EI_FORCE causes a full backup to be done, and EI_NOPURGE causes the deleted objects to be left in the registry afterwards. These can be bitwise OR'ed together if both behaviors are desired. If EI_NOPURGE was specified, ei_reg_purge() can be used to explicitly remove the deleted items from the registry later.

Returns 0 on success, otherwise -1.

double ei_reg_getfval(
reg,
key)

Types

ei_reg *reg;
const char *key;

Gets the value associated with key in the registry. The value must be a floating point type.

  • reg is the registry where the object will be looked up.
  • key is the name of the object to look up.

On success, the function returns the value associated with key. If the object is not found or if it is not a floating point object, -1.0 is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between -1.0 and a valid result), use the more general function ei_reg_getval() instead.

int ei_reg_getival(
reg,
key)

Types

ei_reg *reg;
const char *key;

Gets the value associated with key in the registry. The value must be an integer.

  • reg is the registry where the object will be looked up.
  • key is the name of the object to look up.

On success, the function returns the value associated with key. If the object is not found or if it is not an integer object, -1 is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between -1 and a valid result), use the more general function ei_reg_getval() instead.

const void *ei_reg_getpval(
reg,
key,
size)

Types

ei_reg *reg;
const char *key;
int size;

Gets the value associated with key in the registry. The value must be a binary (pointer) type.

  • reg is the registry where the object will be looked up.
  • key is the name of the object to look up.
  • size is initialized to contain the length in bytes of the object, if it is found.

On success, the function returns the value associated with key and indicates its length in size. If the object is not found or if it is not a binary object, NULL is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between NULL and a valid result), use the more general function ei_reg_getval() instead.

const char *ei_reg_getsval(
reg,
key)

Types

ei_reg *reg;
const char *key;

Gets the value associated with key in the registry. The value must be a string.

  • reg is the registry where the object will be looked up.
  • key is the name of the object to look up.

On success, the function returns the value associated with key. If the object is not found or if it is not a string, NULL is returned. To avoid problems with in-band error reporting (that is, if you cannot distinguish between NULL and a valid result), use the more general function ei_reg_getval() instead.

int ei_reg_getval(
reg,
key,
flags,
v,
...)

Types

ei_reg *reg;
const char *key;
int flags;
void *v (see below)

A general function for retrieving any kind of object from the registry.

  • reg is the registry where the object will be looked up.

  • key is the name of the object to look up.

  • flags indicates the type of object that you are looking for. If flags is 0, any kind of object is returned. If flags is EI_INT, EI_FLT, EI_STR, or EI_BIN, then only values of that kind are returned.

    The buffer pointed to by v must be large enough to hold the return data, that is, it must be a pointer to one of int, double, char*, or void*, respectively.

    If flags is EI_BIN, a fifth argument int *size is required, so that the size of the object can be returned.

On success, v (and size if the object is binary) is initialized with the value associated with key, and the function returns EI_INT, EI_FLT, EI_STR, or EI_BIN, indicating the type of object. On failure, -1 is returned and the arguments are not updated.

int ei_reg_markdirty(
reg,
key)

Types

ei_reg *reg;
const char *key;

Marks a registry object as dirty. This ensures that it is included in the next backup to Mnesia. Normally this operation is not necessary, as all of the normal registry 'set' functions do this automatically. However, if you have retrieved the value of a string or binary object from the registry and modified the contents, then the change is invisible to the registry and the object is assumed to be unmodified. This function allows you to make such modifications and then let the registry know about them.

  • reg is the registry containing the object.
  • key is the name of the object to mark.

Returns 0 on success, otherwise -1.

ei_reg *ei_reg_open(
size)

Types

int size;

Opens (creates) a registry, which initially is empty. To close the registry later, use ei_reg_close().

size is the approximate number of objects you intend to store in the registry. As the registry uses a hash table with collision chaining, no absolute upper limit exists on the number of objects that can be stored in it. However, for reasons of efficiency, it is a good idea to choose a number that is appropriate for your needs. To change the size later, use ei_reg_resize(). Notice that the number you provide is increased to the nearest larger prime number.

Returns an empty registry on success, otherwise NULL.

int ei_reg_purge(
reg)

Types

ei_reg *reg;

Removes all objects marked for deletion. When objects are deleted with ei_reg_delete() they are not removed from the registry, only marked for later removal. On a later backup to Mnesia, the objects can also be removed from the Mnesia table. If you are not backing up to Mnesia, you may wish to remove the objects manually with this function.

reg is a registry containing objects marked for deletion.

Returns 0 on success, otherwise -1.

int ei_reg_resize(
reg,
newsize)

Types

ei_reg *reg;
int newsize;

Changes the size of a registry.

newsize is the new size to make the registry. The number is increased to the nearest larger prime number.

On success, the registry is resized, all contents rehashed, and 0 is returned. On failure, the registry is left unchanged and -1 is returned.

int ei_reg_restore(
fd,
reg,
mntab)

Types

int fd;
ei_reg *reg;
const char *mntab;

The contents of a Mnesia table are read into the registry.

  • fd is an open connection to Erlang. Mnesia 3.0 or later must be running on the Erlang node.
  • reg is the registry where the data is to be placed.
  • mntab is the name of the Mnesia table to read data from.

Notice that only tables of a certain format can be restored, that is, those that have been created and backed up to with ei_reg_dump(). If the registry was not empty before the operation, the contents of the table are added to the contents of the registry. If the table contains objects with the same keys as those already in the registry, the registry objects are overwritten with the new values. If the registry contains objects that were not in the table, they are unchanged by this operation.

After the restore operation, the entire contents of the registry is marked as unmodified. Notice that this includes any objects that were modified before the restore and not overwritten by the restore.

Returns 0 on success, otherwise -1.

int ei_reg_setfval(
reg,
key,
f)

Types

ei_reg *reg;
const char *key;
double f;

Creates a key-value pair with the specified key and floating point value f. If an object already exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().

  • reg is the registry where the object is to be placed.
  • key is the object name.
  • f is the floating point value to assign.

Returns 0 on success, otherwise -1.

int ei_reg_setival(
reg,
key,
i)

Types

ei_reg *reg;
const char *key;
int i;

Creates a key-value pair with the specified key and integer value i. If an object already exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().

  • reg is the registry where the object is to be placed.
  • key is the object name.
  • i is the integer value to assign.

Returns 0 on success, otherwise -1.

int ei_reg_setpval(
reg,
key,
p,
size)

Types

ei_reg *reg;
const char *key;
const void *p;
int size;

Creates a key-value pair with the specified key whose "value" is the binary object pointed to by p. If an object already exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().

  • reg is the registry where the object is to be placed.
  • key is the object name.
  • p is a pointer to the binary object. The object itself must have been created through a single call to malloc() or a similar function, so that the registry can later delete it if necessary by calling free().
  • size is the length in bytes of the binary object.

Returns 0 on success, otherwise -1.

int ei_reg_setsval(
reg,
key,
s)

Types

ei_reg *reg;
const char *key;
const char *s;

Creates a key-value pair with the specified key whose "value" is the specified string s. If an object already exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().

  • reg is the registry where the object is to be placed.
  • key is the object name.
  • s is the string to assign. The string itself must have been created through a single call to malloc() or similar a function, so that the registry can later delete it if necessary by calling free().

Returns 0 on success, otherwise -1.

int ei_reg_setval(
reg,
key,
flags,
v,
...)

Types

ei_reg *reg;
const char *key;
int flags;
v (see below)

Creates a key-value pair with the specified key whose value is specified by v. If an object already exists with the same key, the new value replaces the old one. If the previous value was a binary or string, it is freed with free().

  • reg is the registry where the object is to be placed.

  • key is the object name.

  • flags indicates the type of the object specified by v. Flags must be one of EI_INT, EI_FLT, EI_STR, and EI_BIN, indicating whether v is int, double, char*, or void*.

    If flags is EI_BIN, a fifth argument size is required, indicating the size in bytes of the object pointed to by v.

If you wish to store an arbitrary pointer in the registry, specify a size of 0. In this case, the object itself is not transferred by an ei_reg_dump() operation, only the pointer value.

Returns 0 on success, otherwise -1.

int ei_reg_stat(
reg,
key,
obuf)

Types

ei_reg *reg;
const char *key;
struct ei_reg_stat *obuf;

Returns information about an object.

  • reg is the registry containing the object.
  • key is the object name.
  • obuf is a pointer to an ei_reg_stat structure, defined as follows:
struct ei_reg_stat {
  int attr;
  int size;
};

In attr the attributes of the object are stored as the logical OR of its type (one of EI_INT, EI_FLT, EI_BIN, and EI_STR), whether it is marked for deletion (EI_DELET), and whether it has been modified since the last backup to Mnesia (EI_DIRTY).

Field size indicates the size in bytes required to store EI_STR (including the terminating 0) and EI_BIN objects, or 0 for EI_INT and EI_FLT.

Returns 0 and initializes obuf on success, otherwise -1.

int ei_reg_tabstat(
reg,
obuf)

Types

ei_reg *reg;
struct ei_reg_tabstat *obuf;

Returns information about a registry. Using information returned by this function, you can see whether the size of the registry is suitable for the amount of data it contains.

  • reg is the registry to return information about.
  • obuf is a pointer to an ei_reg_tabstat structure, defined as follows:
struct ei_reg_tabstat {
  int size;  
  int nelem; 
  int npos;  
  int collisions; 
};

Field size indicates the number of hash positions in the registry. This is the number you provided when you created or last resized the registry, rounded up to the nearest prime number.

  • nelem indicates the number of elements stored in the registry. It includes objects that are deleted but not purged.
  • npos indicates the number of unique positions that are occupied in the registry.
  • collisions indicates how many elements are sharing positions in the registry.

On success, 0 is returned and obuf is initialized to contain table statistics, otherwise -1 is returned.