00001
00002 #include "drms.h"
00003 #include "drms_priv.h"
00004 #include "SUM.h"
00005 #include "sum_rpc.h"
00006 #include "xmem.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 int drms_cache_init(DRMS_Env_t *env) {
00018
00019 hcon_init (&env->storageunit_cache, sizeof(HContainer_t), DRMS_MAXHASHKEYLEN,
00020 (void (*)(const void *)) hcon_free,
00021 (void (*)(const void *, const void *)) hcon_copy);
00022
00023
00024
00025 #ifdef DEBUG
00026 printf("Building hashed container of series templates.\n");
00027 #endif
00028
00029 hcon_init (&env->series_cache, sizeof(DRMS_Record_t), DRMS_MAXHASHKEYLEN,
00030 (void (*)(const void *)) drms_free_template_record_struct,
00031 (void (*)(const void *, const void *)) drms_copy_record_struct);
00032
00033
00034 hcon_init (&env->record_cache, sizeof(DRMS_Record_t), DRMS_MAXHASHKEYLEN,
00035 (void (*)(const void *)) drms_free_record_struct,
00036 (void (*)(const void *, const void *)) drms_copy_record_struct);
00037
00038 return 0;
00039 }
00040
00041 DRMS_Env_t *drms_open (const char *host, const char *user, const char *password, const char *dbname,
00042 const char *sessionns) {
00043
00044
00045 DRMS_Env_t *env;
00046
00047 env = (DRMS_Env_t *)malloc(sizeof(DRMS_Env_t));
00048 XASSERT(env);
00049 memset(env, 0, sizeof(DRMS_Env_t));
00050
00051 #ifdef DRMS_CLIENT
00052 drms_client_initsdsem();
00053
00054 if (host) {
00055 if ((env->session = drms_connect (host)) == NULL)
00056 goto bailout;
00057 }
00058
00059 if (drms_cache_init (env)) goto bailout;
00060
00061
00062 env->drms_lock = malloc(sizeof(pthread_mutex_t));
00063 XASSERT(env->drms_lock);
00064 pthread_mutex_init(env->drms_lock, NULL);
00065 #else
00066 drms_server_initsdsem();
00067
00068 if (host) {
00069 if ((env->session = drms_connect_direct (host, user,
00070 password, dbname, sessionns)) == NULL) goto bailout;
00071 }
00072
00073
00074
00075
00076 env->templist = NULL;
00077 env->retention = -1;
00078 env->query_mem = 512;
00079 env->verbose = 0;
00080 env->sumssafe = 1;
00081 #endif
00082 return env;
00083
00084 bailout:
00085 drms_free_env(env, 1);
00086 return NULL;
00087 }
00088
00089 #ifdef DRMS_CLIENT
00090
00091
00092
00093
00094 int drms_close (DRMS_Env_t *env, int action) {
00095 int status;
00096
00097 if ((status = drms_closeall_records(env,action)))
00098 fprintf (stderr, "ERROR in drms_close: failed to close records in cache.\n");
00099
00100 drms_disconnect (env, 0);
00101 drms_free_env (env, 1);
00102 return status;
00103 }
00104
00105 void drms_abort (DRMS_Env_t *env) {
00106 if (drms_closeall_records(env, DRMS_FREE_RECORD))
00107 fprintf (stderr, "ERROR in drms_close: failed to close records in cache.\n");
00108
00109
00110 drms_disconnect (env, 1);
00111 drms_free_env (env, 1);
00112 }
00113
00114 void drms_abort_now (DRMS_Env_t *env) {
00115 drms_lock_client(env);
00116 if (drms_closeall_records(env, DRMS_FREE_RECORD))
00117 {
00118 fprintf (stderr, "ERROR in drms_close: failed to close records in cache.\n");
00119 }
00120
00121 drms_disconnect_now (env, 1);
00122 drms_unlock_client(env);
00123
00124
00125 drms_free_env (env, 1);
00126 }
00127 #endif
00128
00129 void drms_free_env (DRMS_Env_t *env, int final) {
00130
00131
00132 #ifndef DRMS_CLIENT
00133 drms_lock_server(env);
00134 #endif
00135 hcon_free (&env->record_cache);
00136 hcon_free (&env->series_cache);
00137 hcon_free (&env->storageunit_cache);
00138 #ifndef DRMS_CLIENT
00139 drms_unlock_server(env);
00140 #endif
00141
00142 if (env->drms_lock)
00143 {
00144 pthread_mutex_destroy(env->drms_lock);
00145 }
00146 free (env->drms_lock);
00147 env->drms_lock = NULL;
00148 #ifndef DRMS_CLIENT
00149
00150
00151 if (env->clientlock)
00152 {
00153 pthread_mutex_destroy(env->clientlock);
00154 free (env->clientlock);
00155 env->clientlock = NULL;
00156 }
00157
00158
00159 if (env->sum_inbox) {
00160 tqueueDelete (env->sum_inbox);
00161 env->sum_inbox = NULL;
00162 }
00163
00164 if (env->sum_outbox) {
00165 tqueueDelete (env->sum_outbox);
00166 env->sum_outbox = NULL;
00167 }
00168
00169
00170 env->transinit = 0;
00171
00172 #endif
00173 if (env->session) {
00174
00175
00176
00177 free (env->session->sudir);
00178 env->session->sudir = NULL;
00179 }
00180
00181 if (env->cleaners)
00182 {
00183 list_llfree(&env->cleaners);
00184 }
00185
00186 if (final) {
00187 if (env->session) {
00188
00189 free (env->session->sessionns);
00190 env->session->sessionns = NULL;
00191
00192
00193 free (env->session);
00194 env->session = NULL;
00195 }
00196 free (env);
00197 }
00198
00199
00200 drms_term();
00201 }
00202
00203
00204 long long drms_su_size (DRMS_Env_t *env, char *series) {
00205 int status;
00206 DRMS_Record_t *rec;
00207
00208 rec = drms_template_record (env, series, &status);
00209 if (rec == NULL) {
00210 fprintf (stderr, "ERROR: Couldn't get template for series '%s'.\n"
00211 "drms_template_record returned status=%d\n",
00212 series, status);
00213 return -1;
00214 }
00215 return rec->seriesinfo->unitsize * drms_record_size (rec);
00216 }