/* * returns a pointer to a table, or NULL if fails */ struct table *bucket_get_table(struct bucket *bucket, int64_t id, int flags) { struct table *table; int wrlocked; if (bucket == NULL) return NULL; wrlocked = 0; pthread_rwlock_rdlock(&bucket->rwlock); again: HASH_FIND_INT64(bucket->tables, &id, table); if (table == NULL) { if (wrlocked == 0) { pthread_rwlock_unlock(&bucket->rwlock); pthread_rwlock_wrlock(&bucket->rwlock); wrlocked = 1; goto again; } table = table_create_and_read(bucket, id, flags); if (table) HASH_ADD_INT64(bucket->tables, id, table); } pthread_rwlock_unlock(&bucket->rwlock); return table; }
struct schema *schema_get_from_hash(uint64_t hash, const char *bucket_name) { struct schema *schema; pthread_mutex_lock(&mutex); HASH_FIND_INT64(schemas, &hash, schema); if (schema == NULL) { schema = schema_create_from_hash(hash, bucket_name); if (schema) HASH_ADD_INT64(schemas, hash, schema); } pthread_mutex_unlock(&mutex); return schema; }
// it is *key* to return self here, since it gets set // when the first value is added struct point_hash* point_hash_insert(struct point_hash* self, int64 id, struct point* point) { struct point_hash* pth = point_hash_find(self, id); if (pth == NULL) { pth = point_hash_new(id); // note this modifies self when self is NULL // hpixid is expanded to pth->hpixid HASH_ADD_INT64(self, hpixid, pth); } // add a new pointer as data vector_push(pth->points, &point); return self; }
int main(int argc, char** argv) { int64 counter=0; if (argc < 2) { usage_and_exit(); } const char* config_url = argv[1]; struct sconfig* config=sconfig_read(config_url); // this is the beginning of the table struct lensum_hash* hash = NULL; struct lensum* lensum = lensum_new(config->nbin); struct lensum* lensum_tot = lensum_new(config->nbin); while (lensum_read(stdin, lensum)) { counter++; if (counter == 1) { wlog("first lensum: %ld %ld %.8g %ld\n", lensum->index, lensum->zindex, lensum->weight, lensum->totpairs); } if ((counter % 10000) == 0) { wlog("."); } if (((counter+1) % 1000000) == 0) { wlog("\n"); comma_print(stderr,counter+1); wlog("\n"); } struct lensum_hash* this_lens = find_lens(hash, lensum->zindex); if (this_lens == NULL) { // copy of lensum made inside struct lensum_hash* lh = lensum_hash_fromlensum(lensum); // this gets expanded to lh->lensum->zindex HASH_ADD_INT64(hash, lensum->zindex, lh); } else { lensum_add(this_lens->lensum, lensum); } lensum_add(lensum_tot, lensum); } wlog("\nlast lensum: %ld %ld %.8g %ld\n", lensum->index, lensum->zindex, lensum->weight, lensum->totpairs); wlog("Read a total of %ld\n", counter); // this is the summary lensum_print(lensum_tot); wlog("Writing results to stdout\n"); struct lensum_hash *tlensum=NULL; for(tlensum=hash; tlensum != NULL; tlensum=tlensum->hh.next) { lensum_write(tlensum->lensum, stdout); } wlog("Done\n"); return 0; }