Added new features to the MyDDAS Interface, including some statistical support

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1483 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
tiagosoares
2005-12-19 13:48:42 +00:00
parent fa03c540ce
commit 53b4a0e8f6
6 changed files with 597 additions and 135 deletions

View File

@@ -5,36 +5,17 @@
#include <stdlib.h>
#include "cut_c.h"
#include "myddas_util.h"
#include "myddas_structs.h"
#ifdef MYDDAS_ODBC
#include <sql.h>
#endif /*MYDDAS_ODBC*/
#include "Yap.h"
static MYDDAS_GLOBAL MYDDAS_GLOBAL_STRUCT = NULL;
static MYDDAS_UTIL_CONNECTION MYDDAS_TOP = NULL;
struct list_preds {
char *pred_module;
char *pred_name;
short pred_arity;
struct list_preds *next;
};
struct list_connection {
void *connection;
/*If variable env is NULL, then it's a
MySQL connection, if not then it as the pointer
to the ODBC enviromment variable*/
void *odbc_enviromment;
#ifdef MYDDAS_STATS
/* Time spent by the DataBase Server */
unsigned long totalTimeofDBServer;
unsigned long totalNumberOfRows;
#endif
MYDDAS_UTIL_PREDICATE predicates;
struct list_connection *next;
};
/* Get's a new number for the temporary tables */
static
unsigned int myddas_util_get_table_number(MYDDAS_UTIL_CONNECTION);
/* Prints a error message */
static void
myddas_util_error_message(char *,int,char *);
@@ -63,7 +44,7 @@ myddas_util_delete_predicate_list(MYDDAS_UTIL_PREDICATE);
void check_int(){
int i;
MYDDAS_UTIL_PREDICATE pred = NULL;
MYDDAS_UTIL_CONNECTION top = MYDDAS_TOP;
MYDDAS_UTIL_CONNECTION top = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
for (i=1 ; top!=NULL ; top=top->next)
{
printf ("***************\n");
@@ -115,14 +96,55 @@ myddas_util_set_conn_total_time_DBServer(MYDDAS_UTIL_CONNECTION node ,
node->totalTimeofDBServer = totaltime;
}
unsigned long
myddas_util_get_conn_last_time_DBServer(MYDDAS_UTIL_CONNECTION node){
return node->lastTimeofDBServer;
}
void
myddas_util_set_conn_last_time_DBServer(MYDDAS_UTIL_CONNECTION node ,
unsigned long lasttime){
node->lastTimeofDBServer = lasttime;
}
unsigned long
myddas_util_get_conn_last_transfering_from_DBServer(MYDDAS_UTIL_CONNECTION node){
return node->lastFromDBServer;
}
void
myddas_util_set_conn_last_transfering_from_DBServer(MYDDAS_UTIL_CONNECTION node ,
unsigned long lasttime){
node->lastFromDBServer = lasttime;
}
unsigned long
myddas_util_get_conn_total_transfering_from_DBServer(MYDDAS_UTIL_CONNECTION node){
return node->totalFromDBServer;
}
void
myddas_util_set_conn_total_transfering_from_DBServer(MYDDAS_UTIL_CONNECTION node ,
unsigned long totaltime){
node->totalFromDBServer = totaltime;
}
unsigned long
myddas_util_get_total_db_row_function(void){
return MYDDAS_GLOBAL_STRUCT->myddas_statistics->total_db_row;
}
void
myddas_util_set_total_db_row_function(unsigned long time){
MYDDAS_GLOBAL_STRUCT->myddas_statistics->total_db_row = time;
}
unsigned long
myddas_current_time(void) {
/* to get time as Yap */
/*
double now, interval;
cputime_interval(&now, &interval);
return ((realtime)now);
*/
Int now, interval;
Yap_cputime_interval(&now, &interval);
//return ((realtime)now);
return (now);
/*Fine grained time
tv_usec -> microseconds [0-999999]
*/
@@ -133,15 +155,32 @@ myddas_current_time(void) {
milliseconds -> s/1000
microseconds -> s/1000000
*/
struct timeval tempo;
if (!gettimeofday(&tempo, NULL))
//returns time in microseconds
return (tempo.tv_sec %1000)*1000000+tempo.tv_usec;
/* struct timeval tempo; */
/* if (!gettimeofday(&tempo, NULL)) */
/* //returns time in microseconds */
/* return (tempo.tv_sec %1000)*1000000+tempo.tv_usec; */
/* //return (tempo.tv_sec %1000)*1000+tempo.tv_usec; */
return 0;
}
#endif
#endif /* MYDDAS_STATS */
/* Type: MYSQL->1 ODBC->2*/
short int
myddas_util_connection_type(void *con){
MYDDAS_UTIL_CONNECTION con_node =
myddas_util_search_connection(con);
if (con_node == NULL)
return 0;
if (con_node->odbc_enviromment != NULL) /* ODBC */
return 2;
else
return 1;
}
void *
myddas_util_get_pred_next(void *pointer){
@@ -161,6 +200,12 @@ myddas_util_get_pred_name(void *pointer){
return temp->pred_name;
}
char *
myddas_util_get_pred_module(void *pointer){
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE) pointer;
return temp->pred_module;
}
void *
myddas_util_get_list_pred(MYDDAS_UTIL_CONNECTION node){
return (void *)(node->predicates);
@@ -170,7 +215,7 @@ MYDDAS_UTIL_PREDICATE
myddas_util_search_predicate(char *pred_name, int pred_arity,
char *pred_module){
MYDDAS_UTIL_PREDICATE pred=NULL;
MYDDAS_UTIL_CONNECTION top = MYDDAS_TOP;
MYDDAS_UTIL_CONNECTION top = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
for (;top!=NULL;top=top->next)
{
@@ -199,7 +244,7 @@ myddas_util_add_predicate(char *pred_name, int pred_arity,
}
myddas_util_add_predicate_node(new,&(node_conn->predicates));
return node_conn;
}
@@ -217,9 +262,9 @@ myddas_util_delete_connection(void *conn){
/*Removes the predicates list*/
myddas_util_delete_predicate_list(to_delete->predicates);
if (to_delete == MYDDAS_TOP)
if (to_delete == MYDDAS_GLOBAL_STRUCT->myddas_top_connections)
{
MYDDAS_TOP= to_delete->next;
MYDDAS_GLOBAL_STRUCT->myddas_top_connections= to_delete->next;
free(to_delete);
return;
}
@@ -236,8 +281,13 @@ myddas_util_delete_connection(void *conn){
MYDDAS_UTIL_CONNECTION
myddas_util_search_connection(void *conn){
MYDDAS_UTIL_CONNECTION list = MYDDAS_TOP;
MYDDAS_UTIL_CONNECTION list = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
if (conn == 0) { /* We want all the statistics */
return list;
}
for (;list!=NULL;list=list->next)
if (list->connection == conn)
return list;
@@ -250,33 +300,50 @@ myddas_util_add_connection(void *conn, void *enviromment){
MYDDAS_UTIL_CONNECTION node=NULL;
MYDDAS_UTIL_CONNECTION temp=NULL;
if (MYDDAS_GLOBAL_STRUCT == NULL)
{
MYDDAS_GLOBAL_STRUCT = (MYDDAS_GLOBAL) malloc (sizeof(struct myddas_global));
MYDDAS_GLOBAL_STRUCT->myddas_top_connections = NULL;
#ifdef MYDDAS_STATS
MYDDAS_GLOBAL_STRUCT->myddas_statistics = NULL;
#endif
}
if ((node = myddas_util_search_connection(conn)) != NULL)
{
return node;
}
if (MYDDAS_TOP!=NULL)
if (MYDDAS_GLOBAL_STRUCT->myddas_top_connections!=NULL)
{
//put the new connection node on the top of the list
temp = myddas_util_initialize_connection(conn,enviromment,MYDDAS_TOP);
temp = myddas_util_initialize_connection(conn,enviromment,MYDDAS_GLOBAL_STRUCT->myddas_top_connections);
if (temp == NULL)
{
#ifdef DEBUG
myddas_util_error_message("Could not initialize connection node",__LINE__,__FILE__);
#endif
return NULL;
}
MYDDAS_TOP = temp;
return MYDDAS_TOP;
MYDDAS_GLOBAL_STRUCT->myddas_top_connections = temp;
return MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
}
else //The MYDDAS list is empty
{
#ifdef MYDDAS_STATS
/* Allocates the MYDDAS stats
global structure */
MYDDAS_GLOBAL_STRUCT->myddas_statistics = (MYDDAS_GLOBAL_STATS) malloc (sizeof(struct myddas_global_stats));
MYDDAS_GLOBAL_STRUCT->myddas_statistics->total_db_row = 0;
#endif /* MYDDAS_STATS */
temp = myddas_util_initialize_connection(conn,enviromment,NULL);
if (temp == NULL)
{
myddas_util_error_message("Could not initialize connection node",__LINE__,__FILE__);
return NULL;
}
MYDDAS_TOP = temp;
return MYDDAS_TOP;
MYDDAS_GLOBAL_STRUCT->myddas_top_connections = temp;
return MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
}
}
@@ -286,7 +353,7 @@ myddas_util_add_connection(void *conn, void *enviromment){
if there is any odbc connections left on the list*/
SQLHENV
myddas_util_get_odbc_enviromment(SQLHDBC connection){
MYDDAS_UTIL_CONNECTION top = MYDDAS_TOP;
MYDDAS_UTIL_CONNECTION top = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
for (;top != NULL;top=top->next)
if (top->connection == ((void *)connection))
@@ -296,6 +363,116 @@ myddas_util_get_odbc_enviromment(SQLHDBC connection){
}
#endif
/* Dont forget to free the memory on the other side*/
char *
myddas_util_delete_all_temp_table(){
if (MYDDAS_GLOBAL_STRUCT == NULL)
return NULL;
MYDDAS_UTIL_CONNECTION node = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
if (node->temporary_tables == NULL){
return NULL;
}
MYDDAS_TEMP_TABLES tables = node->temporary_tables;
if (tables->table_numbers == NULL){
free(tables->default_table_name);
free(tables);
return NULL; /* No more tables*/
}
else {
int table_number = tables->table_numbers->number;
struct myddas_table_integers *temporary = tables->table_numbers;
tables->table_numbers = tables->table_numbers->next;
if (tables->table_numbers == NULL)
tables->last_number = NULL;
free(temporary);
int i=0;
/* Number of Digits of table_number */
for (i=1;;i++)
if ((table_number / (10*i)) < 10)
break;
int size = strlen(node->temporary_tables->default_table_name);
char *table_name = (char *) malloc (sizeof(char) * (i + size + 1));
sprintf(table_name,"%s%d",
node->temporary_tables->default_table_name,table_number);
return table_name;
}
}
/* Dont forget to free the memory on the other side*/
char *
myddas_util_get_table_name(void *con){
int table_number;
int i=0;
MYDDAS_UTIL_CONNECTION node = myddas_util_search_connection(con);
if (node->temporary_tables == NULL) {
node->temporary_tables =
(MYDDAS_TEMP_TABLES) malloc (sizeof(struct myddas_temp_tables));
node->temporary_tables->last_number = NULL;
node->temporary_tables->table_numbers = NULL;
/* Number of Digits of the connnection pointer */
for (i=1;;i++)
if ((((int)con) / (10*i)) < 10)
break;
node->temporary_tables->default_table_name =
(char *) malloc (sizeof(char *) + i + 1 + 23);
sprintf (node->temporary_tables->default_table_name,"$myddas_temporary_table%d",(int)con);
}
table_number = myddas_util_get_table_number(node);
/* Number of Digits of table_number */
for (i=1;;i++)
if ((table_number / (10*i)) < 10)
break;
int size = strlen(node->temporary_tables->default_table_name);
char *table_name = (char *) malloc (sizeof(char) * (i + size + 1));
sprintf(table_name,"%s%d",
node->temporary_tables->default_table_name,table_number);
return table_name;
}
static
unsigned int myddas_util_get_table_number(MYDDAS_UTIL_CONNECTION node){
MYDDAS_TEMP_TABLES table = node->temporary_tables;
if (table->table_numbers == NULL) {
table->last_number = (struct myddas_table_integers *) malloc (sizeof(struct myddas_table_integers));
table->table_numbers = table->last_number;
table->last_number->number = 0;
table->last_number->next=NULL;
}
else {
unsigned int last_number = table->last_number->number;
table->last_number->next = (struct myddas_table_integers *) malloc (sizeof(struct myddas_table_integers));
table->last_number = table->last_number->next;
table->last_number->number = ++last_number;
table->last_number->next=NULL;
}
return table->last_number->number;
}
static
void myddas_util_error_message(char *message,int line,char *file){
#ifdef DEBUG
@@ -307,7 +484,7 @@ void myddas_util_error_message(char *message,int line,char *file){
static MYDDAS_UTIL_CONNECTION
myddas_util_search_previous_connection(void *conn){
MYDDAS_UTIL_CONNECTION top = MYDDAS_TOP;
MYDDAS_UTIL_CONNECTION top = MYDDAS_GLOBAL_STRUCT->myddas_top_connections;
for(;top->next!=NULL;top=top->next)
if (top->next->connection == conn)
return top;
@@ -325,11 +502,16 @@ myddas_util_initialize_connection(void *conn,void *enviromment,
}
new->predicates=NULL;
new->connection=conn;
new->temporary_tables=NULL;
new->odbc_enviromment=enviromment;
new->next=next;
#ifdef MYDDAS_STATS
new->totalNumberOfRows=0;
new->totalTimeofDBServer=0;
new->lastTimeofDBServer=0;
new->totalFromDBServer=0;
new->lastFromDBServer=0;
new->total_db_row=0;
#endif
return new;
}
@@ -372,9 +554,6 @@ myddas_util_add_predicate_node(MYDDAS_UTIL_PREDICATE new,
}
/* DUVIDA: nesta estrutura (list_preds) existe um char* que <20> atribuido
por uma funcao do YAP (YAP_AtomOfTerm) na funcao c_db_add_preds.
Temos que fazer free deste apontador?*/
static void
myddas_util_delete_predicate_list(MYDDAS_UTIL_PREDICATE preds_list){
MYDDAS_UTIL_PREDICATE to_delete = NULL;