amortize latches during scan

git-svn-id: svn+ssh://svn.corp.yahoo.com/yahoo/yrl/labs/pnuts/code/logstore@1097 8dad8b1f-cf64-0410-95b6-bcf113ffbcfe
This commit is contained in:
sears 2010-09-02 23:02:24 +00:00
parent b6e7bf214b
commit 46eddd1269

View file

@ -278,13 +278,14 @@ public:
merge_it_(NULL), merge_it_(NULL),
last_returned(NULL), last_returned(NULL),
key(NULL), key(NULL),
valid(false) { valid(false),
reval_count(0) {
rwlc_readlock(ltable->header_mut); rwlc_readlock(ltable->header_mut);
pthread_mutex_lock(&ltable->rb_mut); pthread_mutex_lock(&ltable->rb_mut);
ltable->registerIterator(this); ltable->registerIterator(this);
pthread_mutex_unlock(&ltable->rb_mut); pthread_mutex_unlock(&ltable->rb_mut);
validate(); validate();
rwlc_unlock(ltable->header_mut); // rwlc_unlock(ltable->header_mut);
} }
explicit iterator(logtable* ltable,TUPLE *key) explicit iterator(logtable* ltable,TUPLE *key)
@ -293,18 +294,19 @@ public:
merge_it_(NULL), merge_it_(NULL),
last_returned(NULL), last_returned(NULL),
key(key), key(key),
valid(false) valid(false),
reval_count(0)
{ {
rwlc_readlock(ltable->header_mut); rwlc_readlock(ltable->header_mut);
pthread_mutex_lock(&ltable->rb_mut); pthread_mutex_lock(&ltable->rb_mut);
ltable->registerIterator(this); ltable->registerIterator(this);
pthread_mutex_unlock(&ltable->rb_mut); pthread_mutex_unlock(&ltable->rb_mut);
validate(); validate();
rwlc_unlock(ltable->header_mut); // rwlc_unlock(ltable->header_mut);
} }
~iterator() { ~iterator() {
rwlc_readlock(ltable->header_mut); // rwlc_readlock(ltable->header_mut);
pthread_mutex_lock(&ltable->rb_mut); pthread_mutex_lock(&ltable->rb_mut);
ltable->forgetIterator(this); ltable->forgetIterator(this);
invalidate(); invalidate();
@ -314,31 +316,28 @@ public:
} }
private: private:
TUPLE * getnextHelper() { TUPLE * getnextHelper() {
// rwlc_readlock(ltable->header_mut);
revalidate();
TUPLE * tmp = merge_it_->next_callerFrees(); TUPLE * tmp = merge_it_->next_callerFrees();
if(last_returned && tmp) { if(last_returned && tmp) {
assert(TUPLE::compare(last_returned->key(), last_returned->keylen(), tmp->key(), tmp->keylen()) < 0); assert(TUPLE::compare(last_returned->key(), last_returned->keylen(), tmp->key(), tmp->keylen()) < 0);
TUPLE::freetuple(last_returned); TUPLE::freetuple(last_returned);
} }
last_returned = tmp; last_returned = tmp;
// rwlc_unlock(ltable->header_mut);
return last_returned; return last_returned;
} }
public: public:
TUPLE * getnextIncludingTombstones() { TUPLE * getnextIncludingTombstones() {
rwlc_readlock(ltable->header_mut);
revalidate();
TUPLE * ret = getnextHelper(); TUPLE * ret = getnextHelper();
ret = ret ? ret->create_copy() : NULL; ret = ret ? ret->create_copy() : NULL;
rwlc_unlock(ltable->header_mut);
return ret; return ret;
} }
TUPLE * getnext() { TUPLE * getnext() {
rwlc_readlock(ltable->header_mut);
revalidate();
TUPLE * ret; TUPLE * ret;
while((ret = getnextHelper()) && ret->isDelete()) { } // getNextHelper handles its own memory. while((ret = getnextHelper()) && ret->isDelete()) { } // getNextHelper handles its own memory.
ret = ret ? ret->create_copy() : NULL; // XXX hate making copy! Caller should not manage our memory. ret = ret ? ret->create_copy() : NULL; // XXX hate making copy! Caller should not manage our memory.
rwlc_unlock(ltable->header_mut);
return ret; return ret;
} }
@ -376,7 +375,16 @@ public:
TUPLE * last_returned; TUPLE * last_returned;
TUPLE * key; TUPLE * key;
bool valid; bool valid;
int reval_count;
static const int reval_period = 100;
void revalidate() { void revalidate() {
if(reval_count == reval_period) {
rwlc_unlock(ltable->header_mut);
reval_count = 0;
rwlc_readlock(ltable->header_mut);
} else {
reval_count++;
}
if(!valid) { if(!valid) {
validate(); validate();
} else { } else {