/* returns != 0 if present in nometa array, == 0 if absent */ int st_check_if_matches_nometa (const t_st_strtable *st_strtable, const char *strdata) { int strdata_hash; int pos; int total_entries; strdata_hash = string_fingerprint (strdata); total_entries = st_strtable->nometa.total_entries; pos = 0; while (pos < total_entries) { if (st_strtable->nometa.hashes_tab[pos] == strdata_hash) { if (strcmp (strdata, st_strtable->nometa.exp_tab[pos]) == 0) return (1); } pos++; } return (0); }
void st_insert_base (t_st_strtable *st_strtable, const char *strexp, int force_nometa) { int has_meta; /* !=0 if meta */ int strexp_len; int prev_exp_data_len; int i, pos; int *total_entries; int **hashes_tab; char ***exp_tab; char **exp_data; int *exp_data_len; int *largest_len; if (force_nometa == 0) { if (strchr (strexp, '*') == NULL) has_meta = 0; else has_meta = 1; } else { has_meta = 0; } strexp_len = strlen (strexp); if (has_meta == 0) { /* nometa */ total_entries = &(st_strtable->nometa.total_entries); hashes_tab = &(st_strtable->nometa.hashes_tab); exp_tab = &(st_strtable->nometa.exp_tab); exp_data = &(st_strtable->nometa.exp_data); exp_data_len = &(st_strtable->nometa.exp_data_len); } else { /* meta */ total_entries = &(st_strtable->meta.total_entries); exp_tab = &(st_strtable->meta.exp_tab); exp_data = &(st_strtable->meta.exp_data); exp_data_len = &(st_strtable->meta.exp_data_len); largest_len = &(st_strtable->meta.largest_len); } prev_exp_data_len = *exp_data_len; *total_entries += 1; *exp_data_len += strexp_len + 1; *exp_tab = realloc (*exp_tab, sizeof (char *) * *total_entries); *exp_data = realloc (*exp_data, sizeof (char) * *exp_data_len); if (has_meta == 0) *hashes_tab = realloc (*hashes_tab, sizeof (int *) * *total_entries); /* generate hash */ if (has_meta == 0) (*hashes_tab)[*total_entries - 1] = string_fingerprint (strexp); /* update largest_len */ if (has_meta != 0) { if (strexp_len > *largest_len) *largest_len = strexp_len; } strcpy (*exp_data + prev_exp_data_len, strexp); /* realloc'ed buffer, regenerate exp_tab */ pos = 0; for (i = 0; i < *total_entries; i++) { (*exp_tab)[i] = *exp_data + pos; pos += strlen (*exp_data + pos) + 1; } }
/* returns: >= 0 (position in matrix), ok -- -1, error */ int stats_matrix_add_into (t_stats_matrix *stats_matrix, long long int bytes_in, long long int bytes_out, const char *host_name) { int pos_in_matrix = -1; int counter; int host_name_fingerprint; int *fingerprints_table; int used_entries; t_stats_line *table; int found_in_cache = 0; fingerprints_table = stats_matrix->host_name_fingerprint; used_entries = stats_matrix->used_entries; table = stats_matrix->table; host_name_fingerprint = string_fingerprint (host_name); /* check whether host entry exists */ /* checks in cache, first */ counter = 0; while (LIKELY(counter < HOSTNAME_CACHE_ENTRIES)) { if (UNLIKELY(stats_matrix->hostname_cache.host_name_fingerprint [counter] == host_name_fingerprint)) { if (LIKELY(stats_matrix->hostname_cache.entry_position_in_table [counter] >= 0)) { if (LIKELY(! fast_strcmp (table[stats_matrix->hostname_cache.entry_position_in_table [counter]].host_name, host_name))) { pos_in_matrix = stats_matrix->hostname_cache.entry_position_in_table [counter]; found_in_cache = 1; counter = HOSTNAME_CACHE_ENTRIES; /* ends loop */ } } } counter++; } /* if not found in cache, search the whole table */ if (UNLIKELY(pos_in_matrix < 0)) { counter = 0; while (LIKELY(counter < used_entries)) { if (UNLIKELY(fingerprints_table[counter] == host_name_fingerprint)) { if (LIKELY(! fast_strcmp (table[counter].host_name, host_name))) { pos_in_matrix = counter; counter = used_entries; /* ends loop */ } } counter++; } } /* create host entry, if it does not exist */ if (UNLIKELY(pos_in_matrix < 0)) { pos_in_matrix = stats_matrix_alloc_entry (stats_matrix); if (pos_in_matrix < 0) // realloc error return (-1); stats_matrix->table[pos_in_matrix].total_bytes_orig = 0; stats_matrix->table[pos_in_matrix].total_bytes_comp = 0; stats_matrix->table[pos_in_matrix].total_accesses = 0; strncpy (stats_matrix->table[pos_in_matrix].host_name, host_name, HOSTNAME_BUFFER - 1); stats_matrix->table[pos_in_matrix].host_name[strlen (host_name)] = '\0'; stats_matrix->host_name_fingerprint[pos_in_matrix] = string_fingerprint (stats_matrix->table[pos_in_matrix].host_name); } /* if not found in cache, roll the cache and insert that entry */ if (found_in_cache == 0) { stats_matrix->hostname_cache.entry_position_in_table[stats_matrix->hostname_cache.next_avail] = pos_in_matrix; stats_matrix->hostname_cache.host_name_fingerprint [stats_matrix->hostname_cache.next_avail] = host_name_fingerprint; stats_matrix->hostname_cache.next_avail++; if (stats_matrix->hostname_cache.next_avail == HOSTNAME_CACHE_ENTRIES) stats_matrix->hostname_cache.next_avail = 0; } /* update host data */ stats_matrix->table[pos_in_matrix].total_bytes_orig += bytes_in; stats_matrix->table[pos_in_matrix].total_bytes_comp += bytes_out; stats_matrix->table[pos_in_matrix].total_accesses++; return (pos_in_matrix); }