flatstore.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <string.h>
00031 #include <ctype.h>
00032 #include "../../mem/mem.h"
00033 #include "../../dprint.h"
00034 #include "flat_pool.h"
00035 #include "flat_con.h"
00036 #include "flatstore_mod.h"
00037 #include "flatstore.h"
00038
00039
00040 static int parse_flat_url(const str* url, str* path)
00041 {
00042 if (!url || !url->s || !path) {
00043 LM_ERR("invalid parameter value\n");
00044 return -1;
00045 }
00046 path->s = strchr(url->s, ':') + 1;
00047 path->len = strlen(path->s);
00048 return 0;
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 db_con_t* flat_db_init(const str* url)
00058 {
00059 db_con_t* res;
00060 str* path;
00061
00062 if (!url || !url->s) {
00063 LM_ERR("invalid parameter value\n");
00064 return 0;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 res = pkg_malloc(sizeof(db_con_t)+sizeof(struct flat_con*)+sizeof(str));
00076 if (!res) {
00077 LM_ERR("no pkg memory left\n");
00078 return 0;
00079 }
00080 memset(res, 0, sizeof(db_con_t) + sizeof(struct flat_con*) + sizeof(str));
00081 path = (str*)(((char*)res) + sizeof(db_con_t) + sizeof(struct flat_con*));
00082
00083 if (parse_flat_url(url, path) < 0) {
00084 pkg_free(res);
00085 return 0;
00086 }
00087 res->table = path;
00088
00089 return res;
00090 }
00091
00092
00093
00094
00095
00096
00097 int flat_use_table(db_con_t* h, const str* t)
00098 {
00099 struct flat_con* con;
00100
00101 if (!h || !t || !t->s) {
00102 LM_ERR("invalid parameter value\n");
00103 return -1;
00104 }
00105
00106 if (CON_TABLE(h)->s != t->s) {
00107 if (CON_TAIL(h)) {
00108
00109
00110
00111
00112 con = (struct flat_con*)CON_TAIL(h);
00113 con->ref--;
00114 }
00115
00116 CON_TAIL(h) = (unsigned long)
00117 flat_get_connection((char*)CON_TABLE(h)->s, (char*)t->s);
00118 if (!CON_TAIL(h)) {
00119 return -1;
00120 }
00121 }
00122
00123 return 0;
00124 }
00125
00126
00127 void flat_db_close(db_con_t* h)
00128 {
00129 struct flat_con* con;
00130
00131 if (!h) {
00132 LM_ERR("invalid parameter value\n");
00133 return;
00134 }
00135
00136 con = (struct flat_con*)CON_TAIL(h);
00137
00138 if (con) {
00139 flat_release_connection(con);
00140 }
00141 pkg_free(h);
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 int flat_db_insert(const db_con_t* h, const db_key_t* k, const db_val_t* v,
00153 const int n)
00154 {
00155 FILE* f;
00156 int i;
00157 int l;
00158 char *s, *p;
00159
00160 if (local_timestamp < *flat_rotate) {
00161 flat_rotate_logs();
00162 local_timestamp = *flat_rotate;
00163 }
00164
00165 f = CON_FILE(h);
00166 if (!f) {
00167 LM_ERR("uninitialized connection\n");
00168 return -1;
00169 }
00170
00171 for(i = 0; i < n; i++) {
00172 switch(VAL_TYPE(v + i)) {
00173 case DB_INT:
00174 fprintf(f, "%d", VAL_INT(v + i));
00175 break;
00176
00177 case DB_BIGINT:
00178 LM_ERR("BIGINT not supported");
00179 return -1;
00180
00181 case DB_DOUBLE:
00182 fprintf(f, "%f", VAL_DOUBLE(v + i));
00183 break;
00184
00185 case DB_STRING:
00186 fprintf(f, "%s", VAL_STRING(v + i));
00187 break;
00188
00189 case DB_STR:
00190 fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s);
00191 break;
00192
00193 case DB_DATETIME:
00194 fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
00195 break;
00196
00197 case DB_BLOB:
00198 l = VAL_BLOB(v+i).len;
00199 s = p = VAL_BLOB(v+i).s;
00200 while (l--) {
00201 if ( !(isprint((int)*s) && *s != '\\' && *s != '|')) {
00202 fprintf(f,"%.*s\\x%02X",(int)(s-p),p,(*s & 0xff));
00203 p = s+1;
00204 }
00205 ++s;
00206 }
00207 if (p!=s)
00208 fprintf(f,"%.*s",(int)(s-p),p);
00209 break;
00210
00211 case DB_BITMAP:
00212 fprintf(f, "%u", VAL_BITMAP(v + i));
00213 break;
00214 }
00215
00216 if (i < (n - 1)) {
00217 fprintf(f, "%c", *flat_delimiter);
00218 }
00219 }
00220
00221 fprintf(f, "\n");
00222
00223 if (flat_flush) {
00224 fflush(f);
00225 }
00226
00227 return 0;
00228 }