int ReferentialDML_InsertTuple(int xid, ReferentialAlgebra_context_t*context, char * tablename, tuple_t t) { //XXX assumes table format is key value, starting with a string. tuple_t val = tupleTail(t, 1); size_t val_len; byte * val_byte = byteTuple(val,&val_len); recordid tableRid = ReferentialDML_lookupTableRid(xid,context,tablename); if(t.type[0] == string_typ) { ThashInsert(xid, tableRid, (byte*)t.col[0].string, 1+strlen(t.col[0].string), val_byte, val_len); } else if(t.type[0] == int64_typ) { abort(); // int keys not supported yet ThashInsert(xid, tableRid, (byte*)&t.col[0].int64, sizeof(int64_t), val_byte, val_len); } else { abort(); } free(val_byte); tupleFree(val); return 0; }
static void* threadFunc(void* arg_ptr) { int j, k, startKey, endKey; int xid, count = 0; int bufferCurrentLength, bufferTotalSize; char* insertBuffer; /* Allocate the buffer that stores all outstanding hash table insertions. */ bufferTotalSize = BUFFER_INITIAL_LENGTH; bufferCurrentLength = 0; insertBuffer = stasis_malloc(bufferTotalSize, char); xid = Tbegin(); k = (intptr_t) arg_ptr; startKey = baseKey + (k * opsPerThread); endKey = startKey + opsPerThread; for (j = startKey; j < endKey; j++) { ThashInsert(xid, hashTable, (byte*)&j, sizeof(int), (byte*)&j, sizeof(int)); writeKeyToBuffer(&insertBuffer, &bufferCurrentLength, &bufferTotalSize, j); count++; if ((count % opsPerTransaction) == 0) { /* Prior to committing the transaction, write the hash table insertion buffer to * the insert-log so that we can keep track of which insertions were possibly * committed. After the Tcommit() call, write the insertion buffer to the commit- * log so that we can keep track of which insertions were definitely committed. */ writeBufferToLog(insertLog, insertBuffer, bufferCurrentLength); Tcommit(xid); writeBufferToLog(commitLog, insertBuffer, bufferCurrentLength); bufferCurrentLength = 0; xid = Tbegin(); count = 0; } } /* Prior to committing the transaction, write the hash table insertion buffer to * the insert-log so that we can keep track of which insertions were possibly * committed. After the Tcommit() call, write the insertion buffer to the commit- * log so that we can keep track of which insertions were definitely committed. */ writeBufferToLog(insertLog, insertBuffer, bufferCurrentLength); Tcommit(xid); writeBufferToLog(commitLog, insertBuffer, bufferCurrentLength); return NULL; }
int ReferentialDDL_CreateTable(int xid, ReferentialAlgebra_context_t*context, create *q) { recordid newTable = ThashCreate(xid, VARIABLE_LENGTH, VARIABLE_LENGTH); // XXX assumes first col is a string! tuple_t ridtpl = tupleRid(newTable); tuple_t union_typ_tup = tupleUnion_typ(q->schema); assert(union_typ_tup.count); assert(union_typ_tup.col[0].int64 == string_typ); // XXX ridtpl = tupleCatTuple(ridtpl,union_typ_tup); tupleFree(union_typ_tup); size_t tplLen; byte * tplBytes = byteTuple(ridtpl,&tplLen); ThashInsert(xid,context->hash,(byte*)q->tbl,strlen(q->tbl)+1,tplBytes,tplLen); free(tplBytes); return 1; }
/* // no equivalent call in stasis api JNIEXPORT jlong JNICALL Java_stasis_jni_Stasis_hash_1cardinality (JNIEnv *, jclass, jlong, jlongArray); */ JNIEXPORT jbyteArray JNICALL Java_stasis_jni_Stasis_hash_1insert (JNIEnv *e, jclass c, jlong xid, jlongArray jbarid, jbyteArray jbakey, jbyteArray jbaval) { recordid rid = recordid_jlongArray(e,jbarid); if((*e)->ExceptionOccurred(e)) return 0; size_t keylen; byte * key = bytes_jbyteArray(e,jbakey, &keylen); if((*e)->ExceptionOccurred(e)) return 0; size_t vallen; byte * val = bytes_jbyteArray(e,jbaval, &vallen); if((*e)->ExceptionOccurred(e)) return 0; byte * ret; int retsize = ThashLookup((int)xid,rid,key,keylen,&ret); jbyteArray jbaret; if(retsize == -1) { jbaret = 0; } else { jbaret = jbyteArray_bytes(e,ret,retsize); } if((*e)->ExceptionOccurred(e)) return 0; // printf("Calling insert %lld %lld %lld %lx %lld %lx %lld (retsize %lld)", (long long)xid, (long long)rid.page, (long long)rid.slot, (unsigned long)key, (long long) keylen, (unsigned long)val, (long long) keylen, (long long)retsize); fflush(0); ThashInsert((int)xid, rid, key, keylen, val, vallen); // printf("Returned from insert"); fflush(0); return jbaret; }
static void * go (void * arg_ptr) { // pthread_mutex_lock(&hash_mutex); pthread_mutex_lock(&mutex); activeThreads++; if(activeThreads > max_active) { max_active = activeThreads; } pthread_mutex_unlock(&mutex); int k = *(int*)arg_ptr; int j; int xid = Tbegin(); // double sum_x_squared = 0; // double sum = 0; for(j = k * count; j < (k+1) *(count) ; j++) { // struct timeval start, endtime; // gettimeofday(&start, NULL); ThashInsert(xid, hash, (byte*)&j, sizeof(int), (byte*)&j, sizeof(int)); putCount++; /* gettimeofday(&endtime, NULL); double microSecondsPassed = 1000000 * (endtime.tv_sec - start.tv_sec); microSecondsPassed = (microSecondsPassed + endtime.tv_usec) - start.tv_usec; sum += microSecondsPassed; sum_x_squared += (microSecondsPassed * microSecondsPassed) ; */ // printf("(%d)", k); if(alwaysCommit) { // printf("Commit"); commitCount++; Tcommit(xid); xid = Tbegin(); } } Tcommit(xid); commitCount++; /* for(j = k * count; j < (k+1) *(count) ; j++) { int tmp = -100; TlogicalHashLookup(xid, hash, &j, sizeof(int), &tmp, sizeof(int)); assert(j == tmp); } */ // double count_d = count; // double mean = sum / count_d; // double variance = sqrt((sum_x_squared / count_d) - (mean * mean)); // pthread_mutex_unlock(&hash_mutex); pthread_mutex_lock(&mutex); activeThreads--; /* avg_mean += mean; avg_var += variance; if(mean > max_mean ) { max_mean = mean; } if(variance > max_var) { max_var = variance; } */ pthread_mutex_unlock(&mutex); return NULL; }
static void * go (void * arg_ptr) { // pthread_mutex_lock(&hash_mutex); pthread_mutex_lock(&mutex); activeThreads++; if(activeThreads > max_active) { max_active = activeThreads; } pthread_mutex_unlock(&mutex); int k = *(int*)arg_ptr; int j; int xid = 0; if(!alwaysCommit) { xid = Tbegin(); } double sum_x_squared = 0; double sum = 0; double log_multiplier = COUNTER_RESOLUTION / log(MAX_SECONDS * 1000000000.0); struct timeval timeout_tv; struct timespec timeout; gettimeofday(&timeout_tv, NULL); timeout.tv_sec = timeout_tv.tv_sec; timeout.tv_nsec = 1000 * timeout_tv.tv_usec; timeout.tv_nsec = (int)(1000000000.0 * ((double)random() / (double)RAND_MAX)); timeout.tv_sec++; // struct timeval start; pthread_mutex_lock(&mutex); pthread_cond_timedwait(&never, &mutex, &timeout); pthread_mutex_unlock(&mutex); /* gettimeofday(&start, NULL); assert(timeout.tv_sec <= start.tv_sec); assert(timeout.tv_nsec <= start.tv_nsec || timeout.tv_sec < start.tv_sec);*/ for(j = k * count; j < (k+1) *(count) ; j++) { // struct timeval start, struct timeval endtime_tv; struct timespec endtime; // gettimeofday(&start, NULL); // start = timeout; /* gettimeofday(&start, NULL); assert(timeout.tv_sec <= start.tv_sec); assert(timeout.tv_nsec <= start.tv_nsec || timeout.tv_sec < start.tv_sec); */ if(alwaysCommit) { xid = Tbegin(); } ThashInsert(xid, hash, (byte*)&j, sizeof(int), (byte*)&j, sizeof(int)); if(alwaysCommit) { Tcommit(xid); } gettimeofday(&endtime_tv, NULL); endtime.tv_sec = endtime_tv.tv_sec; endtime.tv_nsec = 1000 * endtime_tv.tv_usec; double microSecondsPassed = 1000000000.0 * (double)(endtime.tv_sec - timeout.tv_sec); microSecondsPassed = (microSecondsPassed + (double)endtime.tv_nsec) - (double)timeout.tv_nsec; assert(microSecondsPassed > 0.0); sum += microSecondsPassed; sum_x_squared += (microSecondsPassed * microSecondsPassed) ; int bucket = (log_multiplier * log(microSecondsPassed)); if(bucket >= COUNTER_RESOLUTION) { bucket = COUNTER_RESOLUTION - 1; } // timeout.tv_sec++; // 0123456789 addTimespec(&timeout, 1000000000.0 / thread_requests_per_sec); pthread_mutex_lock(&mutex); buckets[bucket]++; pthread_cond_timedwait(&never, &mutex, &timeout); pthread_mutex_unlock(&mutex); // printf("(%d)", k); } /* for(j = k * count; j < (k+1) *(count) ; j++) { int tmp = -100; TlogicalHashLookup(xid, hash, &j, sizeof(int), &tmp, sizeof(int)); assert(j == tmp); } */ // double count_d = count; // double mean = sum / count_d; // double variance = sqrt((sum_x_squared / count_d) - (mean * mean)); // pthread_mutex_unlock(&hash_mutex); pthread_mutex_lock(&mutex); activeThreads--; /* avg_mean += mean; avg_var += variance; if(mean > max_mean ) { max_mean = mean; } if(variance > max_var) { max_var = variance; } */ pthread_mutex_unlock(&mutex); if(!alwaysCommit) { Tcommit(xid); } return NULL; }