/* * @return:'0' for not found, '1' for success, '-1' for update-conflict-rollback. */ int Data_Update(int table_id, TupleId tuple_id, TupleId value, int nid) { int index=0; int h; DataRecord datard; int status; THREAD* threadinfo; /* get the pointer to current thread information. */ threadinfo=(THREAD*)pthread_getspecific(ThreadInfoKey); index=threadinfo->index; int lindex; lindex = GetLocalIndex(index); if (Send3(lindex, nid, cmd_updatefind, table_id, tuple_id) == -1) printf("update find send error\n"); if (Recv(lindex, nid, 2) == -1) printf("update find recv error\n"); status = *(recv_buffer[lindex]); h = *(recv_buffer[lindex]+1); if (status == 0) return 0; /* record the updated data. */ datard.type=DataUpdate; datard.table_id=table_id; datard.tuple_id=tuple_id; datard.value=value; datard.node_id = nid; datard.index=h; DataRecordInsert(&datard); return 1; }
//.......Function running on thread....... The Dispatcher. bool Sender::WorkerThread() { while (true) { Message msg = SendQ_.deQ(); Send1(msg); Send2(msg); Send3(msg); Send4(msg); } Verbose::show("Send thread stopping"); return true; }
/* * @return: '0' to rollback, '1' to go head. */ int Data_Insert(int table_id, TupleId tuple_id, TupleId value, int nid) { int index; int status; uint64_t h; DataRecord datard; THREAD* threadinfo; /* get the pointer to current thread information. */ threadinfo=(THREAD*)pthread_getspecific(ThreadInfoKey); index=threadinfo->index; /* * the node transaction process must to get the data from the storage process in the * node itself or in the other nodes, both use the socket to communicate. */ int lindex; lindex = GetLocalIndex(index); if ((Send3(lindex, nid, cmd_insert, table_id, tuple_id)) == -1) printf("insert send error!\n"); if ((Recv(lindex, nid, 2)) == -1) printf("insert recv error!\n"); status = *(recv_buffer[lindex]); h = *(recv_buffer[lindex]+1); if (status == 0) return 0; datard.type=DataInsert; datard.table_id=table_id; datard.tuple_id=tuple_id; datard.value=value; datard.index=h; datard.node_id = nid; DataRecordInsert(&datard); return 1; }
/* * @input:'isupdate':true for reading before updating, false for commonly reading. * @return:NULL for read nothing, to rollback or just let it go. */ TupleId Data_Read(int table_id, TupleId tuple_id, int nid, int* flag) { int i; int h; int index, visible; int status; uint64_t value; Snapshot* snap; char* DataMemStart; int size; *flag=1; /* send size */ size = 3 + 3 + MAXPROCS; THREAD* threadinfo; /* get the pointer to current thread information. */ threadinfo=(THREAD*)pthread_getspecific(ThreadInfoKey); index=threadinfo->index; int lindex; lindex = GetLocalIndex(index); /* get the pointer to data-memory. */ DataMemStart=(char*)pthread_getspecific(DataMemKey); /* get pointer to transaction-snapshot-data. */ snap=(Snapshot*)pthread_getspecific(SnapshotDataKey); if (Send3(lindex, nid, cmd_readfind, table_id, tuple_id) == -1) printf("read find send error\n"); if (Recv(lindex, nid, 2) == -1) printf("read find recv error\n"); status = *(recv_buffer[lindex]); h = *(recv_buffer[lindex]+1); if (status == 0) { *flag = 0; return 0; } visible=IsDataRecordVisible(DataMemStart, table_id, tuple_id, nid); if(visible == -1) { /* current transaction has deleted the tuple to read, so return to rollback. */ *flag=-1; return 0; } else if(visible > 0) { /* see own transaction's update. */ return visible; } *(send_buffer[lindex]) = cmd_readversion; *(send_buffer[lindex]+1) = snap->tcount; *(send_buffer[lindex]+2) = snap->tid_min; *(send_buffer[lindex]+3) = snap->tid_max; for (i = 0; i < MAXPROCS; i++) *(send_buffer[lindex]+4+i) = snap->tid_array[i]; *(send_buffer[lindex]+4+i) = table_id; *(send_buffer[lindex]+5+i) = h; if (send(connect_socket[nid][lindex], send_buffer[lindex], size*sizeof(uint64_t), 0) == -1) printf("read version send error\n"); if (Recv(lindex, nid, 2) == -1) printf("read version recv error\n"); status = *(recv_buffer[lindex]); value = *(recv_buffer[lindex]+1); if (status == 4) { *flag = -2; return 0; } if (status == 0) { *flag = -3; return 0; } if (status == 1) return value; return 0; }