remove more duplicated code
git-svn-id: svn+ssh://svn.corp.yahoo.com/yahoo/yrl/labs/pnuts/code/logstore@528 8dad8b1f-cf64-0410-95b6-bcf113ffbcfe
This commit is contained in:
parent
cead29f0e9
commit
2f562a175a
3 changed files with 31 additions and 48 deletions
|
@ -212,7 +212,6 @@ void logserver::eventLoop()
|
||||||
|
|
||||||
int maxfd;
|
int maxfd;
|
||||||
|
|
||||||
struct timeval Timeout;
|
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
|
@ -406,25 +405,6 @@ void *serverLoop(void *args)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void readfromsocket(int sockd, byte *buf, int count)
|
|
||||||
{
|
|
||||||
|
|
||||||
int n = 0;
|
|
||||||
while( n < count )
|
|
||||||
{
|
|
||||||
n += read( sockd, buf + n, count - n);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void writetosocket(int sockd, byte *buf, int count)
|
|
||||||
{
|
|
||||||
int n = 0;
|
|
||||||
while( n < count )
|
|
||||||
{
|
|
||||||
n += write( sockd, buf + n, count - n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -504,12 +484,12 @@ void * thread_work_fn( void * args)
|
||||||
|
|
||||||
//read the key
|
//read the key
|
||||||
tuple.key = (byte*) malloc(*tuple.keylen);
|
tuple.key = (byte*) malloc(*tuple.keylen);
|
||||||
readfromsocket(*(item->data->workitem), (byte*) tuple.key, *tuple.keylen);
|
logserver::readfromsocket(*(item->data->workitem), (byte*) tuple.key, *tuple.keylen);
|
||||||
//read the data
|
//read the data
|
||||||
if(!tuple.isDelete() && opcode != logserver::OP_FIND)
|
if(!tuple.isDelete() && opcode != logserver::OP_FIND)
|
||||||
{
|
{
|
||||||
tuple.data = (byte*) malloc(*tuple.datalen);
|
tuple.data = (byte*) malloc(*tuple.datalen);
|
||||||
readfromsocket(*(item->data->workitem), (byte*) tuple.data, *tuple.datalen);
|
logserver::readfromsocket(*(item->data->workitem), (byte*) tuple.data, *tuple.datalen);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
tuple.data = 0;
|
tuple.data = 0;
|
||||||
|
@ -570,7 +550,7 @@ void * thread_work_fn( void * args)
|
||||||
assert(n == sizeof(uint8_t));
|
assert(n == sizeof(uint8_t));
|
||||||
|
|
||||||
//send the tuple
|
//send the tuple
|
||||||
writetosocket(*(item->data->workitem), (byte*) dt->keylen, dt->byte_length());
|
logserver::writetosocket(*(item->data->workitem), (byte*) dt->keylen, dt->byte_length());
|
||||||
|
|
||||||
//free datatuple
|
//free datatuple
|
||||||
free(dt->keylen);
|
free(dt->keylen);
|
||||||
|
@ -643,7 +623,7 @@ void * thread_work_fn( void * args)
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(item->data->th_mut);
|
pthread_mutex_unlock(item->data->th_mut);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
23
logserver.h
23
logserver.h
|
@ -142,6 +142,29 @@ public:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
// XXX utility methods, pull out into some other class.
|
||||||
|
|
||||||
|
static inline void readfromsocket(int sockd, byte *buf, int count)
|
||||||
|
{
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
while( n < count )
|
||||||
|
{
|
||||||
|
n += read( sockd, buf + n, count - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void writetosocket(int sockd, byte *buf, int count)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
while( n < count )
|
||||||
|
{
|
||||||
|
n += write( sockd, buf + n, count - n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//main loop of server
|
//main loop of server
|
||||||
|
|
|
@ -19,26 +19,6 @@
|
||||||
#undef begin
|
#undef begin
|
||||||
#undef end
|
#undef end
|
||||||
|
|
||||||
inline void readfromsocket(int sockd, byte *buf, int count)
|
|
||||||
{
|
|
||||||
|
|
||||||
int n = 0;
|
|
||||||
while( n < count )
|
|
||||||
{
|
|
||||||
n += read( sockd, buf + n, count - n);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void writetosocket(int sockd, byte *buf, int count)
|
|
||||||
{
|
|
||||||
int n = 0;
|
|
||||||
while( n < count )
|
|
||||||
{
|
|
||||||
n += write( sockd, buf + n, count - n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode, datatuple &tuple)
|
datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode, datatuple &tuple)
|
||||||
{
|
{
|
||||||
struct sockaddr_in serveraddr;
|
struct sockaddr_in serveraddr;
|
||||||
|
@ -84,9 +64,9 @@ datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode,
|
||||||
n = write(sockfd, (byte*) tuple.datalen, sizeof(uint32_t));
|
n = write(sockfd, (byte*) tuple.datalen, sizeof(uint32_t));
|
||||||
assert( n == sizeof(uint32_t));
|
assert( n == sizeof(uint32_t));
|
||||||
|
|
||||||
writetosocket(sockfd, (byte*) tuple.key, *tuple.keylen);
|
logserver::writetosocket(sockfd, (byte*) tuple.key, *tuple.keylen);
|
||||||
if(!tuple.isDelete() && *tuple.datalen != 0)
|
if(!tuple.isDelete() && *tuple.datalen != 0)
|
||||||
writetosocket(sockfd, (byte*) tuple.data, *tuple.datalen);
|
logserver::writetosocket(sockfd, (byte*) tuple.data, *tuple.datalen);
|
||||||
|
|
||||||
//read the reply code
|
//read the reply code
|
||||||
uint8_t rcode;
|
uint8_t rcode;
|
||||||
|
@ -105,12 +85,12 @@ datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode,
|
||||||
assert(n == sizeof(uint32_t));
|
assert(n == sizeof(uint32_t));
|
||||||
//read key
|
//read key
|
||||||
rcvdtuple->key = (byte*) malloc(*rcvdtuple->keylen);
|
rcvdtuple->key = (byte*) malloc(*rcvdtuple->keylen);
|
||||||
readfromsocket(sockfd, (byte*) rcvdtuple->key, *rcvdtuple->keylen);
|
logserver::readfromsocket(sockfd, (byte*) rcvdtuple->key, *rcvdtuple->keylen);
|
||||||
if(!rcvdtuple->isDelete())
|
if(!rcvdtuple->isDelete())
|
||||||
{
|
{
|
||||||
//read key
|
//read key
|
||||||
rcvdtuple->data = (byte*) malloc(*rcvdtuple->datalen);
|
rcvdtuple->data = (byte*) malloc(*rcvdtuple->datalen);
|
||||||
readfromsocket(sockfd, (byte*) rcvdtuple->data, *rcvdtuple->datalen);
|
logserver::readfromsocket(sockfd, (byte*) rcvdtuple->data, *rcvdtuple->datalen);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
Loading…
Reference in a new issue