Beispiel #1
0
void destroy_shp_hash(void) {
    struct hashtable_itr *iterator=NULL;
    shpinfo *si;
    int ret;

    if (shp_hash) {
        // walk through the hashtable, free any pointers in the values
        // that aren't null, or we'll leak like a sieve.

        // the hashtable functions always attempt to dereference iterator,
        // and don't check if you give it a null, but will return null if
        // there's nothing in the table.  Grrrr.
        iterator=hashtable_iterator(shp_hash);
        do {
            ret=0;
            if (iterator) {
                si = hashtable_iterator_value(iterator);
                if (si) 
                    empty_shpinfo(si);
                ret=hashtable_iterator_advance(iterator);
            }
        } while (ret);
        hashtable_destroy(shp_hash, 1);  // destroy the hashtable, freeing
                                         // what's left of the entries
        shp_hash=NULL;
        if (iterator) free(iterator);
    }
}
Beispiel #2
0
void print_queue_stats(char *temp, int len_limit) {
    char *pos = temp;
    int remains = len_limit - 3;
    int res;
    int64_t set_hits,get_hits;
    pthread_rwlock_rdlock(&qlist_ht_lock);
    char *k;
    queue_t *q;
    struct hashtable_itr *itr = NULL;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            k = hashtable_iterator_key(itr);
            q = hashtable_iterator_value(itr);
            pthread_mutex_lock(&(q->lock));
            set_hits = q->set_hits;
            get_hits = q->get_hits;
            pthread_mutex_unlock(&(q->lock));
            if (remains > strlen(k) + 50) {
                res = sprintf(pos, "STAT %s %lld/%lld\r\n", k, set_hits, get_hits);
                remains -= res;
                pos += res;                
            } else {
                break;
            }
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    pthread_rwlock_unlock(&qlist_ht_lock);
    sprintf(pos, "END");
    return;
}
/**
 * Use this function iterator-style to go through the entries
 * in the table of managed interfaces. You should call 
 * policy_table_iterator_reset before iterating over entries with 
 * this function.
 * 
 *
 * @return
 *      Pointer to policy table entry if successful, otherwise NULL.
 */
policy_table_entry_t *
policy_table_next(void)
{
    policy_table_entry_t *v;

    INSIST(if_table != NULL);
    if(hashtable_count(if_table) < 1)
        return NULL;

    if(itr_broken) {
        if(itr)
            free(itr);
        itr = hashtable_iterator(if_table); // mallocs
        INSIST(itr != NULL);
        itr_broken = FALSE;
        // now it's at the first element
    } else if (!hashtable_iterator_advance(itr)){
        // no more
        free(itr);
        itr = NULL;
        itr_broken = TRUE; // will reset next time around

        return NULL;
    }

    v = hashtable_iterator_value(itr);

    return v;
}
Beispiel #4
0
static void
add_file_info_indexes(uint32_t *indexes, uint32_t size,
                      struct manifest *mf, struct hashtable *included_files)
{
	if (size == 0) {
		return;
	}

	// path --> index
	struct hashtable *mf_files =
		create_string_index_map(mf->files, mf->n_files);
	// struct file_info --> index
	struct hashtable *mf_file_infos =
		create_file_info_index_map(mf->file_infos, mf->n_file_infos);
	struct hashtable_itr *iter = hashtable_iterator(included_files);
	uint32_t i = 0;
	do {
		char *path = hashtable_iterator_key(iter);
		struct file_hash *file_hash = hashtable_iterator_value(iter);
		indexes[i] = get_file_hash_index(mf, path, file_hash, mf_files,
		                                 mf_file_infos);
		i++;
	} while (hashtable_iterator_advance(iter));
	assert(i == size);

	hashtable_destroy(mf_file_infos, 1);
	hashtable_destroy(mf_files, 1);
}
Beispiel #5
0
void purge_shp_hash(time_t secs_now) {
    struct hashtable_itr *iterator=NULL;
    shpinfo *si;
    int ret;


    if (secs_now > purge_time) {  // Time to purge
        //time_now = localtime(&secs_now);
        //(void)strftime(timestring,100,"%a %b %d %H:%M:%S %Z %Y",time_now);
        //fprintf(stderr,"Purging...%s\n",timestring);

        purge_time += PURGE_PERIOD;

        if (shp_hash) {
            // walk through the hash table and kill entries that are old

            iterator=hashtable_iterator(shp_hash);
            do {
                ret=0;
                if (iterator) {  // must check this, because could be null
                                 // if the iterator malloc failed
                    si=hashtable_iterator_value(iterator);
                    
                    if (si) {
                        if (secs_now > si->last_access+PURGE_PERIOD) {
                            // this is stale, hasn't been accessed in a while
                            //fprintf(stderr,
                            // "found stale entry for %s, deleting it.\n",
                            // si->filename);
                            //fprintf(stderr,"    Destroying si=%lx\n",
                            //            (unsigned long int) si);
                            ret=hashtable_iterator_remove(iterator);
                            
                            // Important that we NOT do the
                            // destroy first, because we've used
                            // the filename pointer field of the
                            // structure as the key, and the
                            // remove function will free that.  If
                            // we clobber the struct first, we
                            // invite segfaults
                            destroy_shpinfo(si);
                            
                            //fprintf(stderr,"    removing from hashtable\n");
                        } else {
                            ret=hashtable_iterator_advance(iterator);
                        }
                    }
                }
            } while (ret);
            // we're now done with the iterator.  Free it to stop us from
            // leaking!
            if (iterator) free(iterator);
        }
        //fprintf(stderr,"   done Purging...\n");
    }
}
Beispiel #6
0
/*
 * matWrite - write signals from measurement structure to MAT file
 */
int matWrite(measurement_t *measurement, const char *outFileName)
{
  size_t dims[2];
  int err = 0;
  mat_t *mat;
  matvar_t *matvar;

  mat = Mat_Create(outFileName, NULL);
  if (mat != NULL) {

    /* loop over all time series */
    struct hashtable *timeSeriesHash = measurement->timeSeriesHash;

    /* Iterator constructor only returns a valid iterator if
     * the hashtable is not empty */
    if (hashtable_count(timeSeriesHash) > 0) {

      struct hashtable_itr *itr = hashtable_iterator(timeSeriesHash);
      do {
        char         *signalName = hashtable_iterator_key(itr);
        timeSeries_t *timeSeries = hashtable_iterator_value(itr);
        double *timeValue = (double *)malloc(sizeof(double)*2*timeSeries->n);
        unsigned int i;

        /*
         * build up a 1x2n array with time stamps in [0..n-1] and
         * values in [n..2n-1].
         */
        for(i=0;i<timeSeries->n;i++) {
          timeValue[i] = timeSeries->time[i];
        }
        for(i=0;i<timeSeries->n;i++) {
          timeValue[timeSeries->n + i] = timeSeries->value[i];
        }
        dims[0] = timeSeries->n;
        dims[1] = 2;

        /* output signal to mat structure and free up temp array. */
        matvar = Mat_VarCreate(signalName, MAT_C_DOUBLE, MAT_T_DOUBLE,
                               2, dims, timeValue, 0);
        Mat_VarWrite(mat, matvar, 0);
        Mat_VarFree(matvar);

        free(timeValue);
      } while (hashtable_iterator_advance(itr));
      free(itr);
    }

    Mat_Close(mat);
  } else {
    fprintf(stderr, "error: could not create MAT file %s\n", outFileName);
    err = 1;
  }

  return err;
}
Beispiel #7
0
void provide_symbols(TCCState *tccs) {
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            tcc_add_symbol(tccs, s->name, s->addr);
        } while(hashtable_iterator_advance(itr));
        free(itr);
    }
}
/**
 * first_name_htbl_element - return first element of the name hash table.
 * @ph_elt: the path hash table the name hash table belongs to
 * @itr: double pointer to a 'struct hashtable_itr' object where the
 *       information about further iterations is stored
 *
 * This function implements name hash table iteration together with
 * 'next_name_htbl_element()'. Returns the first name hash table element or
 * %NULL if the hash table is empty.
 */
struct name_htbl_element *
first_name_htbl_element(struct path_htbl_element *ph_elt,
			struct hashtable_itr **itr)
{
	if (!path_htbl || !ph_elt || hashtable_count(ph_elt->name_htbl) == 0)
		return NULL;

	*itr = hashtable_iterator(ph_elt->name_htbl);
	return hashtable_iterator_value(*itr);
}
Beispiel #9
0
char *symbol_declarations(void) {
    char *decls = strdup("");
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            asprintfa(&decls, "extern %s;\n", s->decl);
        } while(hashtable_iterator_advance(itr));
        free(itr);
    }
    return decls;
}
Beispiel #10
0
void symbols_close(void) {
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            free((void *) s->decl);
            free(s);
        } while(hashtable_iterator_remove(itr));
        free(itr);
    }
    hashtable_destroy(symbol_table, 0);
}
Beispiel #11
0
static void copy_table(zk_hashtable *from, watcher_object_list_t *to) {
    struct hashtable_itr *it;
    int hasMore;
    if(hashtable_count(from->ht)==0)
        return;
    it=hashtable_iterator(from->ht);
    do {
        watcher_object_list_t *w = hashtable_iterator_value(it);
        copy_watchers(w, to, 1);
        hasMore=hashtable_iterator_advance(it);
    } while(hasMore);
    free(it);
}
Beispiel #12
0
static void do_clean_hashtable(zk_hashtable* ht)
{
    struct hashtable_itr *it;
    int hasMore;
    if(hashtable_count(ht->ht)==0)
        return;
    it=hashtable_iterator(ht->ht);
    do {
        watcher_object_list_t* w=hashtable_iterator_value(it);
        destroy_watcher_object_list(w);
        hasMore=hashtable_iterator_remove(it);
    } while(hasMore);
    free(it);
}
Beispiel #13
0
/**
 * Save a hashtable to disk
 *
 * @param table         Hashtable to save
 * @param filename      Filename to write hashtable into
 * @param keywrite      Pointer to function that writes a single key
 * @param valuewrite    Pointer to function that writes a single value
 * @return              Number of entries written or -1 on error
 */
int
hashtable_save(HASHTABLE *table, char *filename,
               int (*keywrite)(int, void*),
               int (*valuewrite)(int, void*))
{
    int fd, rval = 0;
    HASHITERATOR *iter;
    void *key, *value;

    if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
    {
        return -1;
    }
    if (write(fd, "HASHTABLE", 7) != 7)     // Magic number
    {
        close(fd);
        return -1;
    }
    write(fd, &rval, sizeof(rval)); // Write zero counter, will be overrwriten at end
    if ((iter = hashtable_iterator(table)) != NULL)
    {
        while ((key = hashtable_next(iter)) != NULL)
        {
            if (!(*keywrite)(fd, key))
            {
                close(fd);
                hashtable_iterator_free(iter);
                return -1;
            }
            if ((value = hashtable_fetch(table, key)) == NULL ||
                (*valuewrite)(fd, value) == 0)
            {
                close(fd);
                hashtable_iterator_free(iter);
                return -1;
            }
            rval++;
        }
    }

    /* Now go back and write the count of entries */
    if (lseek(fd, 7L, SEEK_SET) != -1)
    {
        write(fd, &rval, sizeof(rval));
    }

    close(fd);
    hashtable_iterator_free(iter);
    return rval;
}
/**
 * Mark all policies in the table UNVERIFIED. Since we can't delete routes or
 * filters, all that are left unverified (i.e. they don't get 
 * added again) when calling policy_table_clean() get removed.
 * 
 * @return
 *      TRUE upon success;
 *      FALSE if aborted because there's still changes pending 
 *            in which case all route statuses stay the same
 */
boolean
policy_table_unverify_all(void)
{
    policy_table_entry_t * pol = NULL;
    ped_policy_route_t * route = NULL;
    struct hashtable_itr * iterator = NULL;
    
    if(hashtable_count(if_table) < 1)
        return TRUE; // nothing to do
        
    if(changes_pending > 0)
        return FALSE; // don't do anything if there's still changes pending

    junos_trace(PED_TRACEFLAG_HT, "%s", __func__);

    iterator = hashtable_iterator(if_table); // mallocs
    INSIST(iterator != NULL);

    // go through all policies
    do {
        pol = hashtable_iterator_value(iterator);
        
        // for all filters and routes in route lists set status to UNVERIFIED
        
        if(!pol->broken) {
            if(pol->filter) {
                pol->filter->status = FILTER_UNVERIFIED;
            }
            if((route = pol->route)) {
                while(route) {
                    // changes_pending is wrong if this fails
                    INSIST(route->status != ROUTE_PENDING);
                    route->status = ROUTE_UNVERIFIED;
                    route = route->next;
                }
            }
        }
        
    } while (hashtable_iterator_advance(iterator));

    free(iterator);
    
    clean_table = TRUE;
    
    return TRUE;
}
Beispiel #15
0
void
reclose_cache(void)
{
	struct reformat *re;
        struct hashtable_itr *itr;
	char *filesname;

	#ifdef DEBUG_TIME
	        struct timeval start_time, end_time;
	        gettimeofday(&start_time, NULL);
	#endif

      	//itererer over hash, frigjør alle elementer
        if (lots_cache!= NULL && hashtable_count(lots_cache) > 0)
        {

                itr = hashtable_iterator(lots_cache);

                do {
                	filesname = hashtable_iterator_key(itr);
                        re = hashtable_iterator_value(itr);

			#ifdef DEBUG
			printf("reclose_cache: closing \"%s\"\n",filesname);
			#endif

			if (re != NULL) {
				reclose(re);
			}

             	} while (hashtable_iterator_remove(itr));

                free(itr);
	}
	#ifdef DEBUG_TIME
	        gettimeofday(&end_time, NULL);
	        printf("Time debug: reclose_cache %f\n",getTimeDifference(&start_time,&end_time));
	#endif


	return; 
}
Beispiel #16
0
amibis_iterator_t
amibis_remoteservice_connectoriterator(
		amibis_remoteservice_t srv)
{
	amibis_iterator_t itr = NULL;

	itr = malloc(sizeof(struct amibis_iterator));
	if (itr == NULL) {
		return NULL;
	}

	itr->type = HASHTABLE_ITERATOR;
	itr->ptr = hashtable_iterator(srv->connectors);
	if (itr->ptr == NULL) {
		free(itr);
		itr = NULL;
	}

	return itr;
}
Beispiel #17
0
cinv_status_t cinv_structure_delete(CInvContext *context,
	CInvStructure *structure) {

	if (hashtable_count(structure->members) > 0) {
		struct hashtable_itr it;
		hashtable_iterator(structure->members, &it);

		do {
			free(hashtable_iterator_key(&it));
			free(hashtable_iterator_value(&it));
		} while (hashtable_iterator_remove(&it));
	}

	hashtable_destroy(structure->members, 0);

	free(structure);

	context_clear_error(context);
	return CINV_SUCCESS;
}
Beispiel #18
0
/**
 * Free a value_t pointer, as well as any data stored within it. Container types
 * will be recursively freed.
 * @param value	value_t pointer to free
 */
void free_value(value_t *value) {
	list_t *listval, *cur, *temp;
	hashtable_t *hashval;
	hashtable_itr_t *itr;
	value_t *tempv;

	switch(value->type) {
	case INTTYPE:
	case BOOLTYPE:
	case LONGTYPE:
	case DOUBLETYPE:
		break;
	case STRINGTYPE:
	case REFTYPE:
		free(value->value.s);
		break;
	case INVOTYPE:
		free(value->value.v->name);
		tempv = encap_hash(value->value.v->params);
		free_value(tempv);
		break;
	case LISTTYPE:
		listval = value->value.l;
		list_for_each_safe(cur, temp, listval) {
			free_value((value_t*)cur->item);
			free(cur);
		}
		free(listval);
		break;
	case DICTTYPE:
		hashval = value->value.h;
		if (hashtable_count(hashval) > 0) {
			itr = hashtable_iterator(hashval);
			do {
				free_value((value_t*)hashtable_iterator_value(itr));
			} while (hashtable_iterator_advance(itr));
			free(itr);
		}
		hashtable_destroy(hashval, 0);
		break;
	}
Beispiel #19
0
/* this function obtains all sender stats. hlper to getAllStatsLines()
 * We need to keep this looked to avoid resizing of the hash table
 * (what could otherwise cause a segfault).
 */
static void
getSenderStats(rsRetVal(*cb)(void*, const char*),
	void *usrptr,
	statsFmtType_t fmt,
	const int8_t bResetCtrs)
{
	struct hashtable_itr *itr = NULL;
	struct sender_stats *stat;
	char fmtbuf[2048];

	pthread_mutex_lock(&mutSenders);

	/* Iterator constructor only returns a valid iterator if
	 * the hashtable is not empty
	 */
	if(hashtable_count(stats_senders) > 0) {
		itr = hashtable_iterator(stats_senders);
		do {
			stat = (struct sender_stats*)hashtable_iterator_value(itr);
			if(fmt == statsFmt_Legacy) {
				snprintf(fmtbuf, sizeof(fmtbuf),
					"_sender_stat: sender=%s messages=%"
					PRIu64,
					stat->sender, stat->nMsgs);
			} else {
				snprintf(fmtbuf, sizeof(fmtbuf),
					"{ \"name\":\"_sender_stat\", "
					"\"sender\":\"%s\", \"messages\":\"%"
					PRIu64 "\"}",
					stat->sender, stat->nMsgs);
			}
			fmtbuf[sizeof(fmtbuf)-1] = '\0';
			cb(usrptr, fmtbuf);
			if(bResetCtrs)
				stat->nMsgs = 0;
		} while (hashtable_iterator_advance(itr));
	}

	free(itr);
	pthread_mutex_unlock(&mutSenders);
}
Beispiel #20
0
void
cache_indexes_empty(void)
{
	size_t *cached;
	struct hashtable_itr *itr;

	cached = indexcachescached;
	itr = hashtable_iterator(indexcachehash);
	if (hashtable_count(indexcachehash) > 0) {
		do {
			indexcache_t *ic;

			ic = hashtable_iterator_value(itr);
			if (ic == NULL)
				continue;
			munmap(ic->ptr, ic->size);
		} while (hashtable_iterator_advance(itr));
	}

	hashtable_destroy(indexcachehash, 1);
}
Beispiel #21
0
void bdb_qlist_db_close(void){
    dump_qstats_to_db();
    
    struct hashtable_itr *itr = NULL;
    queue_t *q;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            q = hashtable_iterator_value(itr);
            q->dbp->close(q->dbp, 0);
            pthread_mutex_destroy(&(q->lock));
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    if (qlist_dbp != NULL) {
        qlist_dbp->close(qlist_dbp, 0);        
    }
    fprintf(stderr, "qlist_dbp->close: OK\n");
}
Beispiel #22
0
void reseedEncoderFLY(LTencoderFLY *encoder, unsigned long newSeed) {

    // Reinit the totGenSymbols and the refRandomSeed
    encoder->totGenSymbols = 0;
    unsigned long oldSeed = encoder->refRandomSeed;
    encoder->refRandomSeed = newSeed;
    unsigned long oldConfSeed;
    struct hashtable *h = encoder->DistributionsTable;

#ifdef DEBUGencoderPrintBucketRandomSeed
    printf("Encoder[reseed()] - Seed: %lu\n", encoder->refRandomSeed);
#endif

    struct hashtable_itr *itr;
    struct key *k;
    struct value *v;

    // Update all the configurations, if any
    itr = hashtable_iterator(h);
    if (hashtable_count(h) > 0) {
        do {
            k = hashtable_iterator_key(itr);
            v = hashtable_iterator_value(itr);

            // Update the confSeed
            oldConfSeed = v->confSeed;
            v->confSeed = newSeed + (oldConfSeed-oldSeed);

            // Reinit the random generator
            initRandom(&v->randomGenerator, v->confSeed);

            // Erase the number of generated symbols
            v->genSymbols = 0;
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    return;
}
Beispiel #23
0
void ht_to_perl_ht(HV *perl_ht, struct hashtable *params) {
    if (!hashtable_count(params)) return;

    struct hashtable_itr *itr;
    itr = hashtable_iterator(params);


    do {
        char *param = hashtable_iterator_key(itr);
        char *value = hashtable_iterator_value(itr);
        
        // check if key already exists
        if (hv_exists(perl_ht, param, strlen(param))) {
            fprintf(stderr, "Parameter '%s' is already defined. Ignoring.\n", param);
            continue;
        }

        hv_store(perl_ht, param, strlen(param),
            sv_2mortal(newSVpv(value, 0)), 0);
        
    } while (hashtable_iterator_advance(itr));
    free(itr);
}
/**
 * free_devtable_info - free device table information.
 *
 * This function frees the path hash table and the name hash tables.
 */
void free_devtable_info(void)
{
	struct hashtable_itr *ph_itr;
	struct path_htbl_element *ph_elt;

	if (!path_htbl)
		return;

	if (hashtable_count(path_htbl) > 0) {
		ph_itr = hashtable_iterator(path_htbl);
		do {
			ph_elt = hashtable_iterator_value(ph_itr);
			/*
			 * Note, since we use the same string for the key and
			 * @name in the name hash table elements, we do not
			 * have to iterate name hash table because @name memory
			 * will be freed when freeing the key.
			 */
			hashtable_destroy(ph_elt->name_htbl, 1);
		} while (hashtable_iterator_advance(ph_itr));
	}
	hashtable_destroy(path_htbl, 1);
}
Beispiel #25
0
/* check if a sender has not sent info to us for an extended period
 * of time.
 */
void
checkGoneAwaySenders(const time_t tCurr)
{
	struct hashtable_itr *itr = NULL;
	struct sender_stats *stat;
	const time_t rqdLast = tCurr - glblSenderStatsTimeout;
	struct tm tm;

	pthread_mutex_lock(&mutSenders);

	/* Iterator constructor only returns a valid iterator if
	 * the hashtable is not empty
	 */
	if(hashtable_count(stats_senders) > 0) {
		itr = hashtable_iterator(stats_senders);
		do {
			stat = (struct sender_stats*)hashtable_iterator_value(itr);
			if(stat->lastSeen < rqdLast) {
				if(glblReportGoneAwaySenders) {
					localtime_r(&stat->lastSeen, &tm);
					LogMsg(0, RS_RET_SENDER_GONE_AWAY,
						LOG_WARNING,
						"removing sender '%s' from connection "
						"table, last seen at "
						"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
						stat->sender,
						tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
						tm.tm_hour, tm.tm_min, tm.tm_sec);
				}
				hashtable_remove(stats_senders, (void*)stat->sender);
			}
		} while (hashtable_iterator_advance(itr));
	}

	pthread_mutex_unlock(&mutSenders);
	free(itr);
}
Beispiel #26
0
void *
cache_free(cache_t *c)
{

    	cache_value_t *v;

    struct hashtable_itr *itr;
    if (hashtable_count(c->c_data) > 0)
    {
        itr = hashtable_iterator(c->c_data);
        do {
            v = hashtable_iterator_value(itr);

	    c->c_freevalue(v->p);
	    free(v);
        } while (hashtable_iterator_advance(itr));

     	free(itr);

    }

    hashtable_destroy(c->c_data,0); /* second arg indicates "free(value)" */

}
Beispiel #27
0
int transaction_serialize(transaction* t, tr_submit_msg* msg, int max_size) {
    int size = 0;
    int hsize = 0;
	key* k;
    flat_key_val* kv;
    flat_key_hash* kh;
	struct hashtable_itr* itr;
	
	msg->type = TRANSACTION_SUBMIT;
    msg->id.client_id = t->id.client_id;
    msg->id.seqnumber = t->id.seqnumber;
    msg->id.node_id = NodeID;
    msg->start = t->st;
    msg->readset_count = 0;
    msg->writeset_count = 0;
    
    // copy readset hashes
	if (hashtable_count(t->rs) > 0) {
		itr = hashtable_iterator(t->rs);
		do {
			if ((size + (int)sizeof(flat_key_hash)) > max_size) {
				return -1;
			}
				
			k = hashtable_iterator_key(itr);
			kh = (flat_key_hash*)&msg->data[size];
			
			kh->hash[0] = hashtable_iterator_hash(itr);
			kh->hash[1] = djb2_hash(k->data, k->size);
			
			size += sizeof(flat_key_hash);
			msg->readset_count++;
		} while (hashtable_iterator_advance(itr));
		free(itr);
	}

    
    // copy writeset hashes
	if (hashtable_count(t->ws) > 0) {
		itr = hashtable_iterator(t->ws);
		do {
			if ((size + (int)sizeof(flat_key_hash)) > max_size) {
				return -1;
			}
			
			k = hashtable_iterator_key(itr);
			kh = (flat_key_hash*)&msg->data[size];
			
			kh->hash[0] = hashtable_iterator_hash(itr);
			kh->hash[1] = djb2_hash(k->data, k->size);
			
			size += sizeof(flat_key_hash);
			msg->writeset_count++;
		} while (hashtable_iterator_advance(itr));
		free(itr);
		hsize = size;
		
		itr = hashtable_iterator(t->ws);
		do {
			kv = hashtable_iterator_value(itr);
			if ((size + (int)FLAT_KEY_VAL_SIZE(kv)) > max_size) {
				return -1;
			}
			
			memcpy(&msg->data[size], kv, FLAT_KEY_VAL_SIZE(kv));
			size += (int)FLAT_KEY_VAL_SIZE(kv);
		} while (hashtable_iterator_advance(itr));
		free(itr);
	}
    msg->writeset_size = (size - hsize);
    return TR_SUBMIT_MSG_SIZE(msg);
}
int bbdocument_add(char subname[],char documenturi[],char documenttype[],char document[],const int dokument_size,unsigned int lastmodified,char *acl_allow, char *acl_denied,const char title[], char doctype[], char *attributes, container *attrkeys) {

	struct ReposetoryHeaderFormat ReposetoryHeader;

	int htmlbuffersize = 0;//((dokument_size *2) +512);	//+512 da vi skal ha med div meta data, som html kode
	char *htmlbuffer = NULL;// = malloc(htmlbuffersize);
	char *imagebuffer;
	char *documenttype_real;
	size_t imageSize;
	unsigned int DocIDForExistTest;
	unsigned int lastmodifiedForExistTest;
	struct hashtable *metahash = NULL;
	buffer *documentbuffer;

	memset(&ReposetoryHeader,0,sizeof(ReposetoryHeader));

	printf("bbdocument_add: \"%s\"\n",documenturi);

	printf("bbdocument_add: Attributes = \"%s\" (", attributes);
	if (attrkeys!=NULL) printf("+)\n");
	else printf(" )\n");


	//tester at det ikke finnes først. Hvis det finnes hånterer vi det. Eventuelt også lagre  den nye versjonen hvis det er forandret.
	if (uriindex_get(documenturi,&DocIDForExistTest,&lastmodifiedForExistTest,subname)) {

		if (lastmodifiedForExistTest == lastmodified) {
			printf("bbdocument_add: Uri \"%s\" all redy exist with DocID \"%u\" and time \"%u\"\n",documenturi,DocIDForExistTest,lastmodifiedForExistTest);
			return 1;
		}
		//hvis url er kjent, men oppdater rebruker vi den.
		ReposetoryHeader.DocID = DocIDForExistTest;
	}
	else {
		//hvis vi ikke har DocID har vi et system som trenger å få opprettet det. For eks bb eller bdisk.
		ReposetoryHeader.DocID = rGeneraeADocID(subname);

		#ifdef DEBUG
			printf("Dident have a known DocID for document. Did generet on. DOcID is now %u\n",ReposetoryHeader.DocID);
		#endif

	}

	// hvis vi har en "text applicatino" enkoding, som ikke er en vanlig tekst fil, men tekst som kommer fra 
	// en apllikasjon behandler vi det som text.
	//if (strcmp(documenttype,"tapp") == 0) {
	//	documenttype_real = strdup("text");
	//}
	//else 
	if (documenttype[0] == '\0') {
		if ((documenttype_real = sfindductype(documenturi)) == NULL) {
			printf("Will use .none as documenttype because I can't decide format. File name isent dos type (8.3): %s\n", documenturi);
			documenttype_real = strdup("none");
			
		}
	}
	else {
		documenttype_real = malloc(strlen(documenttype)+1);
		strcpy(documenttype_real,documenttype);
	}



	//hvis vi ikke her med noen egen doctype så bruker vi den vi har fått via documenttype
	if (doctype[0] == '\0') {
		//vi trenger ikke å ha \0 på slutten her
		strncpy(ReposetoryHeader.doctype,documenttype_real,sizeof(ReposetoryHeader.doctype));
	}
	else {
		//vi trenger ikke å ha \0 på slutten her
		strncpy(ReposetoryHeader.doctype,doctype,sizeof(ReposetoryHeader.doctype));
	}

	documentbuffer = buffer_init(0);
	if (!bbdocument_convert(documenttype_real,document,dokument_size,documentbuffer,title,subname,documenturi, lastmodified,acl_allow, acl_denied, &metahash)) {

		printf("can't run bbdocument_convert\n");
		//lager en tom html buffer
		//Setter titelen som subjekt. Hva hvis vi ikke har title?
		htmlbuffersize = strlen(html_text_tempelate) + strlen(title) + 1;
		htmlbuffer = malloc(htmlbuffersize);
		snprintf(htmlbuffer, htmlbuffersize, html_text_tempelate,title,"");
		htmlbuffersize = strlen(htmlbuffer);
		printf("useing title \"%s\" as title\n",title);
		printf("htmlbuffersize %i\n",htmlbuffersize);
		free(buffer_abort(documentbuffer));
	} else {
		htmlbuffersize = buffer_length(documentbuffer);
		htmlbuffer = buffer_exit(documentbuffer);
	}

	buffer *attrbuffer = buffer_init(-1);
	bprintf(attrbuffer, "%s,", attributes);

	if (metahash) {
		struct hashtable_itr *itr;

		if (hashtable_count(metahash) > 0) {
			itr = hashtable_iterator(metahash);
			do {
				char *key, *value;

				key = hashtable_iterator_key(itr);
				value = hashtable_iterator_value(itr);

				printf("%s: Key: %s Value: %s\n", documenttype_real, key, value);

				if (strcmp(key, "lastmodified") == 0) {
					lastmodified = atol(value);
					continue;
				}

				bprintf(attrbuffer, "%s=%s,", key, value);
			} while (hashtable_iterator_advance(itr));

			free(itr);
		}
		hashtable_destroy(metahash, 1);
	}
	char *all_attributes = buffer_exit(attrbuffer);



	//prøver å lag et bilde
	//if ( (imagebuffer = generate_thumbnail( document, dokument_size, &imageSize )) == NULL ) {
	if (!bbdocument_makethumb(documenttype_real,document,dokument_size,&imagebuffer,&imageSize)) {
		printf("can't generate image\n");
		ReposetoryHeader.imageSize = 0;
		imagebuffer = NULL;
	}
	else {
		debug("generated image\n");
		ReposetoryHeader.imageSize = imageSize;
	}


	ReposetoryHeader.clientVersion = 2.14;

	//runarb:  8 juli 2008: tar bort bruken av ReposetoryHeader's url
	//runarb: 11 juli 2008: kan ikke gjøre dette, da vi kopierer den inn i DocumentIndex fra ReposetoryHeader 
	strncpy(ReposetoryHeader.url,documenturi,sizeof(ReposetoryHeader.url));
		

	ReposetoryHeader.response = 200;
	strcpy(ReposetoryHeader.content_type,"htm");

	ReposetoryHeader.acl_allowSize = strlen(acl_allow);
#ifdef IIACL
	ReposetoryHeader.acl_deniedSize = strlen(acl_denied);
#endif
	ReposetoryHeader.time = lastmodified;

	ReposetoryHeader.storageTime = 0; //setes automatisk av rApendPostcompress

#ifdef DEBUG
	printf("ACL was allow \"%s\", %i bytes, denied \"%s\", %i bytes\nsubname %s\n",acl_allow,ReposetoryHeader.acl_allowSize,acl_allow,ReposetoryHeader.acl_allowSize,subname);
#endif

	ReposetoryHeader.urllen = strlen(documenturi);
	ReposetoryHeader.attributeslen = strlen(all_attributes);
	rApendPostcompress(&ReposetoryHeader, htmlbuffer, imagebuffer, subname, acl_allow, acl_denied, NULL, documenturi, all_attributes, attrkeys, htmlbuffersize);

#ifdef DEBUG	
	printf("legger til DocID \"%u\", time \"%u\"\n",ReposetoryHeader.DocID,lastmodified);
	printf("htmlSize %u, imageSize %ho\n",ReposetoryHeader.htmlSize2,ReposetoryHeader.imageSize);
	printf("html: -%s-\n",htmlbuffer);
#endif

	uriindex_add(documenturi,ReposetoryHeader.DocID,lastmodified,subname);

	free(all_attributes);
	free(htmlbuffer);
	free(documenttype_real);

	if (imagebuffer != NULL) {
		free(imagebuffer);
	}

	return 1;
}	
Beispiel #29
0
static void dump_qstats_to_db(void) {
    /* qstats hashtable */
    char *kk;
    qstats_t *s;
    struct hashtable *qstats_htp = NULL;    
    qstats_htp = create_hashtable(64, hashfromkey, equalkeys);
    assert(qstats_htp != NULL);
    
    /* cp hashtable to stats table, this is very fast in-memory */
    pthread_rwlock_rdlock(&qlist_ht_lock);
    char *k;
    queue_t *q;
    int result;
    struct hashtable_itr *itr = NULL;
    itr = hashtable_iterator(qlist_htp);
    assert(itr != NULL);
    if (hashtable_count(qlist_htp) > 0)
    {
        do {
            k = hashtable_iterator_key(itr);
            q = hashtable_iterator_value(itr);
            pthread_mutex_lock(&(q->lock));
            if (q->old_set_hits == q->set_hits &&
                q->old_get_hits == q->get_hits) {
                pthread_mutex_unlock(&(q->lock));
                continue;
            }
            q->old_set_hits = q->set_hits;
            q->old_get_hits = q->get_hits;
            pthread_mutex_unlock(&(q->lock));
            kk = strdup(k);
            assert(kk);
            s = calloc(1, sizeof(qstats_t));
            assert(s);
            s->set_hits = q->old_set_hits;
            s->get_hits = q->old_get_hits;
            result = hashtable_insert(qstats_htp, (void *)kk, (void *)s);
            assert(result != 0);
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    pthread_rwlock_unlock(&qlist_ht_lock);
    
    /* dump stats hashtable to db */
    DBT dbkey, dbdata;
    int ret;
    DB_TXN *txnp = NULL;
    ret = envp->txn_begin(envp, NULL, &txnp, 0);
    CHECK_DB_RET(ret);
    itr = hashtable_iterator(qstats_htp);
    assert(itr != NULL);
    if (hashtable_count(qstats_htp) > 0)
    {
        do {
            kk = hashtable_iterator_key(itr);
            s = hashtable_iterator_value(itr);
            BDB_CLEANUP_DBT();
            dbkey.data = (void *)kk;
            dbkey.size = strlen(kk) + 1;
            dbdata.data = (void *)s;
            dbdata.size = sizeof(qstats_t);
            ret = qlist_dbp->put(qlist_dbp, txnp, &dbkey, &dbdata, 0);
            CHECK_DB_RET(ret);
            fprintf(stderr, "dump stats[%s], set_hits: %lld, get_hits: %lld \n",
                    kk, s->set_hits, s->get_hits);
        } while (hashtable_iterator_advance(itr));
    }
    free(itr);
    itr = NULL;
    ret = txnp->commit(txnp, 0);
    CHECK_DB_RET(ret);

    hashtable_destroy(qstats_htp, 1);
    qstats_htp = NULL;
    return;
dberr:
    if(itr != NULL) {
        free(itr);
    }
    if (qstats_htp != NULL) {
        hashtable_destroy(qstats_htp, 1);
    }
    if (txnp != NULL){
        txnp->abort(txnp);
    }
    if (settings.verbose > 1) {
        fprintf(stderr, "dump_qstats_to_db: %s\n", db_strerror(ret));
    }
}
Beispiel #30
0
/**
 * test1	spinlock_acquire_nowait tests
 *
 * Test that spinlock_acquire_nowait returns false if the spinlock
 * is already taken.
 *
 * Test that spinlock_acquire_nowait returns true if the spinlock
 * is not taken.
 *
 * Test that spinlock_acquire_nowait does hold the spinlock.
 */
static bool do_hashtest(
        int argelems,
        int argsize)
{
        bool       succp = true;
        HASHTABLE* h;
        int        nelems;
        int        i;
        int*       val_arr;
        int        hsize;
        int        longest;
        int*       iter;
        
        ss_dfprintf(stderr,
                    "testhash : creating hash table of size %d, including %d "
                    "elements in total, at time %g.",
                    argsize,
                    argelems,
                    (double)clock()-start); 
        
        val_arr = (int *)malloc(sizeof(void *)*argelems);
        
        h = hashtable_alloc(argsize, hfun, cmpfun);

        ss_dfprintf(stderr, "\t..done\nAdd %d elements to hash table.", argelems);
        
        for (i=0; i<argelems; i++) {
            val_arr[i] = i;
            hashtable_add(h, (void *)&val_arr[i], (void *)&val_arr[i]);
        }
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);
        
        ss_dfprintf(stderr, "\t..done\nRead hash table statistics.");
        
        hashtable_get_stats((void *)h, &hsize, &nelems, &longest);

        ss_dfprintf(stderr, "\t..done\nValidate read values.");
        
        ss_info_dassert(hsize == (argsize > 0 ? argsize: 1), "Invalid hash size");
        ss_info_dassert((nelems == argelems) || (nelems == 0 && argsize == 0),
                        "Invalid element count");
        ss_info_dassert(longest <= nelems, "Too large longest list value");
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);

        ss_dfprintf(stderr, "\t..done\nValidate iterator.");
        
        HASHITERATOR *iterator = hashtable_iterator(h);
        read_lock(h);
        for (i=0; i < (argelems+1); i++) {
            iter = (int *)hashtable_next(iterator);
            if (iter == NULL) break;
            if (argelems < 100) ss_dfprintf(stderr, "\nNext item, iter = %d, i = %d", *iter, i);
        }
        read_unlock(h);
        ss_info_dassert((i == argelems) || (i == 0 && argsize == 0), "\nIncorrect number of elements from iterator");
        hashtable_iterator_free(iterator);
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);

        ss_dfprintf(stderr, "\t\t..done\n\nTest completed successfully.\n\n");
        
        CHK_HASHTABLE(h);
        hashtable_free(h);
        

		free(val_arr);
        return succp;
}