1
/* mongo-utils.h - libmongo-client utility functions
2
* Copyright 2011 Gergely Nagy <algernon@balabit.hu>
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
/** @file src/mongo-utils.h
18
* Public header for various libmongo-client helper functions.
21
#ifndef LIBMONGO_CLIENT_UTILS_H
22
#define LIBMONGO_CLIENT_UTILS_H 1
28
/** @defgroup mongo_util Mongo Utils
30
* Various utility functions related to MongoDB.
32
* @addtogroup mongo_util
36
/** Intitialize the static ObjectID components.
38
* @param machine_id is the machine id to use, or zero to generate one
41
* This function needs to be called once, before any OIDs are
42
* generated. It is also a good idea to call it whenever the calling
43
* program's PID might change.
45
void mongo_util_oid_init (gint32 machine_id);
47
/** Generate a new ObjectID.
49
* Based on the current time, the pre-determined pid and machine ID
50
* and a supplied sequence number, generate a new ObjectID.
52
* The machine id and the PID are updated whenever
53
* mongo_util_oid_init() is called.
55
* @param seq is the sequence number to use.
57
* @note The ObjectID has space for only 24 bits of sequence bytes, so
58
* it should be noted that while @a seq is 32 bits wide, only 24 of
61
* @returns A newly allocated ObjectID or NULL on error. Freeing it is
62
* the responsibility of the caller.
64
guint8 *mongo_util_oid_new (gint32 seq);
66
/** Generate a new ObjectID, with a predefined timestamp.
68
* Based on the suppiled time and sequence number, and the
69
* pre-determined pid and machine ID, generate a new ObjectID.
71
* The machine id and the PID are updated whenever
72
* mongo_util_oid_init() is called.
74
* @param time is the timestamp to use.
75
* @param seq is the sequence number to use.
78
* @note The ObjectID has space for only 24 bits of sequence bytes, so
79
* it should be noted that while @a seq is 32 bits wide, only 24 of
82
* @returns A newly allocated ObjectID or NULL on error. Freeing it is
83
* the responsibility of the caller.
85
guint8 *mongo_util_oid_new_with_time (gint32 time, gint32 seq);
87
/** Convert an ObjectID to its string representation.
89
* Turns a binary ObjectID into a hexadecimal string.
91
* @param oid is the binary ObjectID.
93
* @returns A newly allocated string representation of the ObjectID,
94
* or NULL on error. It is the responsibility of the caller to free it
95
* once it is no longer needed.
97
gchar *mongo_util_oid_as_string (const guint8 *oid);
99
/** Parse a HOST:IP pair.
101
* Given a HOST:IP pair, split it up into a host and a port. IPv6
102
* addresses supported, the function cuts at the last ":".
104
* @param addr is the address to split.
105
* @param host is a pointer to a string where the host part will be
107
* @param port is a pointer to an integer, where the port part will be
110
* @returns TRUE on success, FALSE otherwise. The @a host parameter
111
* will contain a newly allocated string on succes. On failiure, host
112
* will be set to NULL, and port to -1.
114
gboolean mongo_util_parse_addr (const gchar *addr, gchar **host,