1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff#ifndef _OSM_DB_H_
37219820Sjeff#define _OSM_DB_H_
38219820Sjeff
39219820Sjeff/*
40219820Sjeff * Abstract:
41219820Sjeff * Declaration of the DB interface.
42219820Sjeff */
43219820Sjeff
44219820Sjeff#include <complib/cl_list.h>
45219820Sjeff#include <complib/cl_spinlock.h>
46219820Sjeff#include <opensm/osm_log.h>
47219820Sjeff
48219820Sjeff#ifdef __cplusplus
49219820Sjeff#  define BEGIN_C_DECLS extern "C" {
50219820Sjeff#  define END_C_DECLS   }
51219820Sjeff#else				/* !__cplusplus */
52219820Sjeff#  define BEGIN_C_DECLS
53219820Sjeff#  define END_C_DECLS
54219820Sjeff#endif				/* __cplusplus */
55219820Sjeff
56219820SjeffBEGIN_C_DECLS
57219820Sjeff/****h* OpenSM/Database
58219820Sjeff* NAME
59219820Sjeff*	Database
60219820Sjeff*
61219820Sjeff* DESCRIPTION
62219820Sjeff*	The OpenSM database interface provide the means to restore persistent
63219820Sjeff*  data, query, modify, delete and eventually commit it back to the
64219820Sjeff*  persistent media.
65219820Sjeff*
66219820Sjeff*  The interface is defined such that it can is not "data dependent":
67219820Sjeff*  All keys and data items are texts.
68219820Sjeff*
69219820Sjeff*	The DB implementation should be thread safe, thus callers do not need to
70219820Sjeff*  provide serialization.
71219820Sjeff*
72219820Sjeff*	This object should be treated as opaque and should be
73219820Sjeff*	manipulated only through the provided functions.
74219820Sjeff*
75219820Sjeff* AUTHOR
76219820Sjeff*	Eitan Zahavi, Mellanox Technologies LTD
77219820Sjeff*
78219820Sjeff*********/
79219820Sjeff/****s* OpenSM: Database/osm_db_domain_t
80219820Sjeff* NAME
81219820Sjeff*	osm_db_domain_t
82219820Sjeff*
83219820Sjeff* DESCRIPTION
84219820Sjeff*	A domain of the database. Can be viewed as a database table.
85219820Sjeff*
86219820Sjeff*	The osm_db_domain_t object should be treated as opaque and should
87219820Sjeff*	be manipulated only through the provided functions.
88219820Sjeff*
89219820Sjeff* SYNOPSIS
90219820Sjeff*/
91219820Sjefftypedef struct osm_db_domain {
92219820Sjeff	struct osm_db *p_db;
93219820Sjeff	void *p_domain_imp;
94219820Sjeff} osm_db_domain_t;
95219820Sjeff/*
96219820Sjeff* FIELDS
97219820Sjeff*	p_db
98219820Sjeff*		Pointer to the parent database object.
99219820Sjeff*
100219820Sjeff*	p_domain_imp
101219820Sjeff*		Pointer to the db implementation object
102219820Sjeff*
103219820Sjeff* SEE ALSO
104219820Sjeff* osm_db_t
105219820Sjeff*********/
106219820Sjeff
107219820Sjeff/****s* OpenSM: Database/osm_db_t
108219820Sjeff* NAME
109219820Sjeff*	osm_db_t
110219820Sjeff*
111219820Sjeff* DESCRIPTION
112219820Sjeff*	The main database object.
113219820Sjeff*
114219820Sjeff*	The osm_db_t object should be treated as opaque and should
115219820Sjeff*	be manipulated only through the provided functions.
116219820Sjeff*
117219820Sjeff* SYNOPSIS
118219820Sjeff*/
119219820Sjefftypedef struct osm_db {
120219820Sjeff	void *p_db_imp;
121219820Sjeff	osm_log_t *p_log;
122219820Sjeff	cl_list_t domains;
123219820Sjeff} osm_db_t;
124219820Sjeff/*
125219820Sjeff* FIELDS
126219820Sjeff*	p_db_imp
127219820Sjeff*		Pointer to the database implementation object
128219820Sjeff*
129219820Sjeff*	p_log
130219820Sjeff*		Pointer to the OSM logging facility
131219820Sjeff*
132219820Sjeff*  domains
133219820Sjeff*     List of initialize domains
134219820Sjeff*
135219820Sjeff* SEE ALSO
136219820Sjeff*********/
137219820Sjeff
138219820Sjeff/****f* OpenSM: Database/osm_db_construct
139219820Sjeff* NAME
140219820Sjeff*	osm_db_construct
141219820Sjeff*
142219820Sjeff* DESCRIPTION
143219820Sjeff*	Construct a database.
144219820Sjeff*
145219820Sjeff* SYNOPSIS
146219820Sjeff*/
147219820Sjeffvoid osm_db_construct(IN osm_db_t * const p_db);
148219820Sjeff/*
149219820Sjeff* PARAMETERS
150219820Sjeff*	p_db
151219820Sjeff*		[in] Pointer to the database object to construct
152219820Sjeff*
153219820Sjeff* RETURN VALUES
154219820Sjeff*	NONE
155219820Sjeff*
156219820Sjeff* SEE ALSO
157219820Sjeff*	Database, osm_db_init, osm_db_destroy
158219820Sjeff*********/
159219820Sjeff
160219820Sjeff/****f* OpenSM: Database/osm_db_destroy
161219820Sjeff* NAME
162219820Sjeff*	osm_db_destroy
163219820Sjeff*
164219820Sjeff* DESCRIPTION
165219820Sjeff*	Destroys the osm_db_t structure.
166219820Sjeff*
167219820Sjeff* SYNOPSIS
168219820Sjeff*/
169219820Sjeffvoid osm_db_destroy(IN osm_db_t * const p_db);
170219820Sjeff/*
171219820Sjeff* PARAMETERS
172219820Sjeff*	p_db
173219820Sjeff*		[in] Pointer to osm_db_t structure to destroy
174219820Sjeff*
175219820Sjeff* SEE ALSO
176219820Sjeff*	Database, osm_db_construct, osm_db_init
177219820Sjeff*********/
178219820Sjeff
179219820Sjeff/****f* OpenSM: Database/osm_db_init
180219820Sjeff* NAME
181219820Sjeff*	osm_db_init
182219820Sjeff*
183219820Sjeff* DESCRIPTION
184219820Sjeff*	Initializes the osm_db_t structure.
185219820Sjeff*
186219820Sjeff* SYNOPSIS
187219820Sjeff*/
188219820Sjeffint osm_db_init(IN osm_db_t * const p_db, IN osm_log_t * p_log);
189219820Sjeff/*
190219820Sjeff* PARAMETERS
191219820Sjeff*
192219820Sjeff*	p_db
193219820Sjeff*		[in] Pointer to the database object to initialize
194219820Sjeff*
195219820Sjeff*	p_log
196219820Sjeff*		[in] Pointer to the OSM logging facility
197219820Sjeff*
198219820Sjeff* RETURN VALUES
199219820Sjeff*	0 on success 1 otherwise
200219820Sjeff*
201219820Sjeff* SEE ALSO
202219820Sjeff*	Database, osm_db_construct, osm_db_destroy
203219820Sjeff*********/
204219820Sjeff
205219820Sjeff/****f* OpenSM: Database/osm_db_domain_init
206219820Sjeff* NAME
207219820Sjeff*	osm_db_domain_init
208219820Sjeff*
209219820Sjeff* DESCRIPTION
210219820Sjeff*	Initializes the osm_db_domain_t structure.
211219820Sjeff*
212219820Sjeff* SYNOPSIS
213219820Sjeff*/
214219820Sjeffosm_db_domain_t *osm_db_domain_init(IN osm_db_t * const p_db,
215219820Sjeff				    IN char *domain_name);
216219820Sjeff/*
217219820Sjeff* PARAMETERS
218219820Sjeff*
219219820Sjeff*	p_db
220219820Sjeff*		[in] Pointer to the database object to initialize
221219820Sjeff*
222219820Sjeff*	domain_name
223219820Sjeff*		[in] a char array with the domain name.
224219820Sjeff*
225219820Sjeff* RETURN VALUES
226219820Sjeff*	pointer to the new domain object or NULL if failed.
227219820Sjeff*
228219820Sjeff* SEE ALSO
229219820Sjeff*	Database, osm_db_construct, osm_db_destroy
230219820Sjeff*********/
231219820Sjeff
232219820Sjeff/****f* OpenSM: Database/osm_db_restore
233219820Sjeff* NAME
234219820Sjeff*	osm_db_restore
235219820Sjeff*
236219820Sjeff* DESCRIPTION
237219820Sjeff*	Reads the entire domain from persistent storage - overrides all
238219820Sjeff*  existing cached data (if any).
239219820Sjeff*
240219820Sjeff* SYNOPSIS
241219820Sjeff*/
242219820Sjeffint osm_db_restore(IN osm_db_domain_t * p_domain);
243219820Sjeff/*
244219820Sjeff* PARAMETERS
245219820Sjeff*
246219820Sjeff*	p_domain
247219820Sjeff*		[in] Pointer to the database domain object to restore
248219820Sjeff*		     from persistent db
249219820Sjeff*
250219820Sjeff* RETURN VALUES
251219820Sjeff*	0 if successful 1 otherwize
252219820Sjeff*
253219820Sjeff* SEE ALSO
254219820Sjeff*	Database, osm_db_domain_init, osm_db_clear, osm_db_store,
255219820Sjeff*  osm_db_keys, osm_db_lookup, osm_db_update, osm_db_delete
256219820Sjeff*********/
257219820Sjeff
258219820Sjeff/****f* OpenSM: Database/osm_db_clear
259219820Sjeff* NAME
260219820Sjeff*	osm_db_clear
261219820Sjeff*
262219820Sjeff* DESCRIPTION
263219820Sjeff*	Clears the entire domain values from/in the cache
264219820Sjeff*
265219820Sjeff* SYNOPSIS
266219820Sjeff*/
267219820Sjeffint osm_db_clear(IN osm_db_domain_t * p_domain);
268219820Sjeff/*
269219820Sjeff* PARAMETERS
270219820Sjeff*
271219820Sjeff*	p_domain
272219820Sjeff*		[in] Pointer to the database domain object to clear
273219820Sjeff*
274219820Sjeff* RETURN VALUES
275219820Sjeff*	0 if successful 1 otherwize
276219820Sjeff*
277219820Sjeff* SEE ALSO
278219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_store,
279219820Sjeff*  osm_db_keys, osm_db_lookup, osm_db_update, osm_db_delete
280219820Sjeff*********/
281219820Sjeff
282219820Sjeff/****f* OpenSM: Database/osm_db_store
283219820Sjeff* NAME
284219820Sjeff*	osm_db_store
285219820Sjeff*
286219820Sjeff* DESCRIPTION
287219820Sjeff*	Store the domain cache back to the database (commit)
288219820Sjeff*
289219820Sjeff* SYNOPSIS
290219820Sjeff*/
291219820Sjeffint osm_db_store(IN osm_db_domain_t * p_domain);
292219820Sjeff/*
293219820Sjeff* PARAMETERS
294219820Sjeff*
295219820Sjeff*	p_domain
296219820Sjeff*		[in] Pointer to the database domain object to restore from
297219820Sjeff*		     persistent db
298219820Sjeff*
299219820Sjeff* RETURN VALUES
300219820Sjeff*	0 if successful 1 otherwize
301219820Sjeff*
302219820Sjeff* SEE ALSO
303219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_clear,
304219820Sjeff*  osm_db_keys, osm_db_lookup, osm_db_update, osm_db_delete
305219820Sjeff*********/
306219820Sjeff
307219820Sjeff/****f* OpenSM: Database/osm_db_keys
308219820Sjeff* NAME
309219820Sjeff*	osm_db_keys
310219820Sjeff*
311219820Sjeff* DESCRIPTION
312219820Sjeff*	Retrive all keys of the domain
313219820Sjeff*
314219820Sjeff* SYNOPSIS
315219820Sjeff*/
316219820Sjeffint osm_db_keys(IN osm_db_domain_t * p_domain, OUT cl_list_t * p_key_list);
317219820Sjeff/*
318219820Sjeff* PARAMETERS
319219820Sjeff*
320219820Sjeff* p_domain
321219820Sjeff*    [in] Pointer to the database domain object
322219820Sjeff*
323219820Sjeff* p_key_list
324219820Sjeff*    [out] List of key values. It should be PRE constructed and initialized.
325219820Sjeff*
326219820Sjeff* RETURN VALUES
327219820Sjeff*	0 if successful 1 otherwize
328219820Sjeff*
329219820Sjeff* NOTE: the caller needs to free and destruct the list,
330219820Sjeff*       the keys returned are intrnal to the hash and should NOT be free'ed
331219820Sjeff*
332219820Sjeff* SEE ALSO
333219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_clear, osm_db_store,
334219820Sjeff*  osm_db_lookup, osm_db_update, osm_db_delete
335219820Sjeff*********/
336219820Sjeff
337219820Sjeff/****f* OpenSM: Database/osm_db_lookup
338219820Sjeff* NAME
339219820Sjeff*	osm_db_lookup
340219820Sjeff*
341219820Sjeff* DESCRIPTION
342219820Sjeff*	Lookup an entry in the domain by the given key
343219820Sjeff*
344219820Sjeff* SYNOPSIS
345219820Sjeff*/
346219820Sjeff/* lookup value by key */
347219820Sjeffchar *osm_db_lookup(IN osm_db_domain_t * p_domain, IN char *const p_key);
348219820Sjeff/*
349219820Sjeff* PARAMETERS
350219820Sjeff*
351219820Sjeff*  p_domain
352219820Sjeff*    [in] Pointer to the database domain object
353219820Sjeff*
354219820Sjeff*	key
355219820Sjeff*		[in] The key to look for
356219820Sjeff*
357219820Sjeff* RETURN VALUES
358219820Sjeff*  the value as char * or NULL if not found
359219820Sjeff*
360219820Sjeff* SEE ALSO
361219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_clear, osm_db_store,
362219820Sjeff*  osm_db_keys, osm_db_update, osm_db_delete
363219820Sjeff*********/
364219820Sjeff
365219820Sjeff/****f* OpenSM: Database/osm_db_update
366219820Sjeff* NAME
367219820Sjeff*	osm_db_update
368219820Sjeff*
369219820Sjeff* DESCRIPTION
370219820Sjeff*	Set the value of the given key
371219820Sjeff*
372219820Sjeff* SYNOPSIS
373219820Sjeff*/
374219820Sjeffint
375219820Sjeffosm_db_update(IN osm_db_domain_t * p_domain,
376219820Sjeff	      IN char *const p_key, IN char *const p_val);
377219820Sjeff/*
378219820Sjeff* PARAMETERS
379219820Sjeff*
380219820Sjeff*  p_domain
381219820Sjeff*    [in] Pointer to the database domain object
382219820Sjeff*
383219820Sjeff*	p_key
384219820Sjeff*		[in] The key to update
385219820Sjeff*
386219820Sjeff*	p_val
387219820Sjeff*		[in] The value to update
388219820Sjeff*
389219820Sjeff* RETURN VALUES
390219820Sjeff*  0 on success
391219820Sjeff*
392219820Sjeff* NOTE: the value will be duplicated so can be free'ed
393219820Sjeff*
394219820Sjeff* SEE ALSO
395219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_clear, osm_db_store,
396219820Sjeff*  osm_db_keys, osm_db_lookup, osm_db_delete
397219820Sjeff*********/
398219820Sjeff
399219820Sjeff/****f* OpenSM: Database/osm_db_delete
400219820Sjeff* NAME
401219820Sjeff*	osm_db_delete
402219820Sjeff*
403219820Sjeff* DESCRIPTION
404219820Sjeff*	Delete an entry by the given key
405219820Sjeff*
406219820Sjeff* SYNOPSIS
407219820Sjeff*/
408219820Sjeffint osm_db_delete(IN osm_db_domain_t * p_domain, IN char *const p_key);
409219820Sjeff/*
410219820Sjeff* PARAMETERS
411219820Sjeff*
412219820Sjeff*  p_domain
413219820Sjeff*    [in] Pointer to the database domain object
414219820Sjeff*
415219820Sjeff*	p_key
416219820Sjeff*		[in] The key to look for
417219820Sjeff*
418219820Sjeff* RETURN VALUES
419219820Sjeff*  0 on success
420219820Sjeff*
421219820Sjeff* SEE ALSO
422219820Sjeff*	Database, osm_db_domain_init, osm_db_restore, osm_db_clear, osm_db_store,
423219820Sjeff*  osm_db_keys, osm_db_lookup, osm_db_update
424219820Sjeff*********/
425219820Sjeff
426219820SjeffEND_C_DECLS
427219820Sjeff#endif				/* _OSM_DB_H_ */
428