bdb_con.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * $Id$
  3. *
  4. * BDB Database Driver for SER
  5. *
  6. * Copyright (C) 2008 iptelorg GmbH
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SIP-router is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /** \addtogroup bdb
  25. * @{
  26. */
  27. /*! \file
  28. * Functions related to connections to BDB.
  29. *
  30. * \ingroup database
  31. */
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "../../mem/mem.h"
  35. #include "../../dprint.h"
  36. #include "../../ut.h"
  37. #include "bdb_con.h"
  38. #include "bdb_uri.h"
  39. #include "bdb_lib.h"
  40. /** Free all memory allocated for a bdb_con structure.
  41. * This function function frees all memory that is in use by
  42. * a bdb_con structure.
  43. * @param con A generic db_con connection structure.
  44. * @param payload BDB specific payload to be freed.
  45. */
  46. static void bdb_con_free(db_con_t* con, bdb_con_t *payload)
  47. {
  48. bdb_uri_t *buri;
  49. if (!payload)
  50. return;
  51. buri = DB_GET_PAYLOAD(con->uri);
  52. /* Delete the structure only if there are no more references
  53. * to it in the connection pool
  54. */
  55. if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
  56. db_pool_entry_free(&payload->gen);
  57. /* destroy and free BDB env */
  58. pkg_free(payload);
  59. }
  60. int bdb_con(db_con_t* con)
  61. {
  62. bdb_con_t* bcon;
  63. bdb_uri_t* buri;
  64. buri = DB_GET_PAYLOAD(con->uri);
  65. /* First try to lookup the connection in the connection pool and
  66. * re-use it if a match is found
  67. */
  68. bcon = (bdb_con_t*)db_pool_get(con->uri);
  69. if (bcon) {
  70. DBG("bdb: Connection to %s found in connection pool\n",
  71. buri->uri);
  72. goto found;
  73. }
  74. bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t));
  75. if (!bcon) {
  76. ERR("bdb: No memory left\n");
  77. goto error;
  78. }
  79. memset(bcon, '\0', sizeof(bdb_con_t));
  80. if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error;
  81. DBG("bdb: Preparing new connection to %s\n", buri->uri);
  82. if(bdb_is_database(buri->path.s)!=0)
  83. {
  84. ERR("bdb: database [%.*s] does not exists!\n",
  85. buri->path.len, buri->path.s);
  86. goto error;
  87. }
  88. /* Put the newly created BDB connection into the pool */
  89. db_pool_put((struct db_pool_entry*)bcon);
  90. DBG("bdb: Connection stored in connection pool\n");
  91. found:
  92. /* Attach driver payload to the db_con structure and set connect and
  93. * disconnect functions
  94. */
  95. DB_SET_PAYLOAD(con, bcon);
  96. con->connect = bdb_con_connect;
  97. con->disconnect = bdb_con_disconnect;
  98. return 0;
  99. error:
  100. if (bcon) {
  101. db_pool_entry_free(&bcon->gen);
  102. pkg_free(bcon);
  103. }
  104. return -1;
  105. }
  106. int bdb_con_connect(db_con_t* con)
  107. {
  108. bdb_con_t *bcon;
  109. bdb_uri_t *buri;
  110. bcon = DB_GET_PAYLOAD(con);
  111. buri = DB_GET_PAYLOAD(con->uri);
  112. /* Do not reconnect already connected connections */
  113. if (bcon->flags & BDB_CONNECTED) return 0;
  114. DBG("bdb: Connecting to %s\n", buri->uri);
  115. /* create BDB environment */
  116. bcon->dbp = bdblib_get_db(&buri->path);
  117. if(bcon->dbp == NULL)
  118. {
  119. ERR("bdb: error binding to DB %s\n", buri->uri);
  120. return -1;
  121. }
  122. DBG("bdb: Successfully bound to %s\n", buri->uri);
  123. bcon->flags |= BDB_CONNECTED;
  124. return 0;
  125. }
  126. void bdb_con_disconnect(db_con_t* con)
  127. {
  128. bdb_con_t *bcon;
  129. bdb_uri_t *buri;
  130. bcon = DB_GET_PAYLOAD(con);
  131. buri = DB_GET_PAYLOAD(con->uri);
  132. if ((bcon->flags & BDB_CONNECTED) == 0) return;
  133. DBG("bdb: Unbinding from %s\n", buri->uri);
  134. if(bcon->dbp==NULL)
  135. {
  136. bcon->flags &= ~BDB_CONNECTED;
  137. return;
  138. }
  139. bdblib_close(bcon->dbp, &buri->path);
  140. bcon->dbp = 0;
  141. bcon->flags &= ~BDB_CONNECTED;
  142. }
  143. /** @} */