2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/multiplexer.h>
|
|
|
|
#include <stasis/crc32.h>
|
2005-03-14 00:58:47 +00:00
|
|
|
#include <stdlib.h>
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/operations/linearHashNTA.h>
|
2005-03-15 05:30:57 +00:00
|
|
|
|
|
|
|
#include "logger/logMemory.h"
|
|
|
|
|
2005-03-14 00:58:47 +00:00
|
|
|
lladdMultiplexer_t * lladdMultiplexer_alloc(int xid, lladdIterator_t * it,
|
|
|
|
void (*multiplexer)(byte * key,
|
|
|
|
size_t keySize,
|
|
|
|
byte * value,
|
|
|
|
size_t valueSize,
|
|
|
|
byte ** multiplexKey,
|
|
|
|
size_t * multiplexKeySize),
|
2005-03-16 00:52:21 +00:00
|
|
|
/* lladdConsumer_t * getConsumer(struct lladdFifoPool_t* fifoPool,
|
2005-03-14 00:58:47 +00:00
|
|
|
byte* multiplexKey,
|
2005-03-16 00:52:21 +00:00
|
|
|
size_t multiplexKeySize), */
|
2005-03-14 08:47:13 +00:00
|
|
|
lladdFifoPool_t * fifoPool) {
|
2005-03-14 00:58:47 +00:00
|
|
|
lladdMultiplexer_t * ret = malloc(sizeof(lladdMultiplexer_t));
|
|
|
|
ret->it = it;
|
|
|
|
ret->multiplexer = multiplexer;
|
|
|
|
ret->consumerHash = pblHtCreate();
|
2005-03-16 00:52:21 +00:00
|
|
|
// ret->getConsumer = getConsumer;
|
|
|
|
ret->fifoPool = fifoPool;
|
2005-03-14 00:58:47 +00:00
|
|
|
ret->xid = xid;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * multiplexer_worker(void * arg);
|
|
|
|
|
|
|
|
int lladdMultiplexer_start(lladdMultiplexer_t * multiplexer, pthread_attr_t * thread_attributes) {
|
|
|
|
return pthread_create(&multiplexer->worker, thread_attributes, multiplexer_worker, multiplexer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lladdMultiplexer_join(lladdMultiplexer_t * multiplexer) {
|
|
|
|
return pthread_join(multiplexer->worker,NULL);
|
|
|
|
}
|
|
|
|
|
2005-03-24 04:43:22 +00:00
|
|
|
void * lladdMultiplexer_flush(lladdMultiplexer_t * m) {
|
|
|
|
// lladdMultiplexer_t * m = arg;
|
|
|
|
lladdConsumer_t * consumer;
|
|
|
|
|
|
|
|
while(Titerator_tryNext(m->xid, m->it)) {
|
|
|
|
byte * mkey, * key, * value;
|
|
|
|
size_t mkeySize, keySize, valueSize;
|
|
|
|
|
|
|
|
keySize = Titerator_key (m->xid, m->it, &key);
|
|
|
|
valueSize = Titerator_value(m->xid, m->it, &value);
|
|
|
|
|
|
|
|
m->multiplexer(key, keySize, value, valueSize, &mkey, &mkeySize);
|
|
|
|
|
|
|
|
lladdFifo_t * fifo = m->fifoPool->getFifo(m->fifoPool, mkey, mkeySize);
|
|
|
|
consumer = fifo->consumer;
|
|
|
|
Tconsumer_push(m->xid, consumer, key, keySize, value, valueSize);
|
|
|
|
Titerator_tupleDone(m->xid, m->it);
|
|
|
|
lladdFifoPool_markDirty(m->xid, m->fifoPool, fifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over pblhash, closing consumers.
|
|
|
|
|
|
|
|
/* Titerator_close(m->xid, m->it);
|
|
|
|
|
|
|
|
// @todo Does this belong in its own function in fifo.c?
|
|
|
|
|
|
|
|
lladdFifoPool_t * pool = m->fifoPool;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < pool->fifoCount; i++) {
|
|
|
|
Tconsumer_close(m->xid, pool->pool[i]->consumer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m->fifoPool->dirtyPoolFifo) {
|
|
|
|
Tconsumer_close(m->xid, m->fifoPool->dirtyPoolFifo->consumer);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return (void*)compensation_error();
|
|
|
|
}
|
|
|
|
|
2005-03-14 00:58:47 +00:00
|
|
|
|
|
|
|
void * multiplexer_worker(void * arg) {
|
|
|
|
lladdMultiplexer_t * m = arg;
|
2005-03-15 05:30:57 +00:00
|
|
|
lladdConsumer_t * consumer;
|
|
|
|
|
2005-03-14 00:58:47 +00:00
|
|
|
while(Titerator_next(m->xid, m->it)) {
|
|
|
|
byte * mkey, * key, * value;
|
|
|
|
size_t mkeySize, keySize, valueSize;
|
|
|
|
|
|
|
|
keySize = Titerator_key (m->xid, m->it, &key);
|
|
|
|
valueSize = Titerator_value(m->xid, m->it, &value);
|
|
|
|
|
|
|
|
m->multiplexer(key, keySize, value, valueSize, &mkey, &mkeySize);
|
|
|
|
|
2005-03-16 00:52:21 +00:00
|
|
|
lladdFifo_t * fifo = m->fifoPool->getFifo(m->fifoPool, mkey, mkeySize);
|
|
|
|
consumer = fifo->consumer;
|
2005-03-14 00:58:47 +00:00
|
|
|
Tconsumer_push(m->xid, consumer, key, keySize, value, valueSize);
|
2005-03-23 08:07:53 +00:00
|
|
|
Titerator_tupleDone(m->xid, m->it);
|
2005-03-16 00:52:21 +00:00
|
|
|
lladdFifoPool_markDirty(m->xid, m->fifoPool, fifo);
|
2005-03-14 00:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over pblhash, closing consumers.
|
|
|
|
|
|
|
|
Titerator_close(m->xid, m->it);
|
|
|
|
|
2005-03-16 00:52:21 +00:00
|
|
|
/** @todo Does this belong in its own function in fifo.c? */
|
|
|
|
|
|
|
|
lladdFifoPool_t * pool = m->fifoPool;
|
2005-03-15 05:30:57 +00:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < pool->fifoCount; i++) {
|
|
|
|
Tconsumer_close(m->xid, pool->pool[i]->consumer);
|
|
|
|
}
|
|
|
|
|
2005-03-16 00:52:21 +00:00
|
|
|
if(m->fifoPool->dirtyPoolFifo) {
|
|
|
|
Tconsumer_close(m->xid, m->fifoPool->dirtyPoolFifo->consumer);
|
|
|
|
}
|
|
|
|
|
2005-03-14 00:58:47 +00:00
|
|
|
return (void*)compensation_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************** END OF MULTIXPLEXER IMPLEMENTATION **************
|
|
|
|
|
|
|
|
Sample callbacks follow.
|
|
|
|
|
2005-03-15 05:30:57 +00:00
|
|
|
*/
|
2005-03-14 00:58:47 +00:00
|
|
|
void multiplexHashLogByKey(byte * key,
|
|
|
|
size_t keySize,
|
|
|
|
byte * value,
|
|
|
|
size_t valueSize,
|
|
|
|
byte ** multiplexKey,
|
|
|
|
size_t * multiplexKeySize) {
|
|
|
|
// We don't care what the key is. It's probably an LSN.
|
|
|
|
const LogEntry * log = (const LogEntry*) value;
|
|
|
|
const byte * updateArgs = getUpdateArgs(log); // assume the log is a logical update entry.
|
2007-04-03 09:18:45 +00:00
|
|
|
switch(log->update.funcID) {
|
2005-03-14 00:58:47 +00:00
|
|
|
|
|
|
|
// If you really want to know why insert takes
|
|
|
|
// linearHash_remove_arg entries and vice versa, look at
|
|
|
|
// linearHashNTA. Note that during normal (physiological forward)
|
|
|
|
// operation, ThashInsert() *generates* insert args for its undo
|
|
|
|
// implementation, ThashRemove() and vice versa. Therefore,
|
|
|
|
// ThashRemove's operation implementation takes an insert
|
|
|
|
// argument.
|
|
|
|
|
|
|
|
case OPERATION_LINEAR_HASH_INSERT:
|
|
|
|
{
|
|
|
|
linearHash_remove_arg * arg = (linearHash_remove_arg*) updateArgs; // this *is* correct. Don't ask...
|
|
|
|
*multiplexKey = (byte*) (arg+1);
|
|
|
|
*multiplexKeySize = arg->keySize;
|
|
|
|
}
|
2005-03-15 05:30:57 +00:00
|
|
|
break;
|
2005-03-14 00:58:47 +00:00
|
|
|
case OPERATION_LINEAR_HASH_REMOVE:
|
|
|
|
{
|
|
|
|
linearHash_insert_arg * arg = (linearHash_insert_arg*)updateArgs; // this *is* correct. Don't ask....
|
|
|
|
*multiplexKey = (byte*) (arg + 1);
|
|
|
|
*multiplexKeySize = arg->keySize;
|
|
|
|
}
|
2005-03-15 05:30:57 +00:00
|
|
|
break;
|
2005-03-14 00:58:47 +00:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-24 04:43:22 +00:00
|
|
|
|
|
|
|
void multiplexByValue(byte * key, size_t keySize, byte * value, size_t valueSize, byte **multiplexKey, size_t * multiplexSize) {
|
|
|
|
*multiplexKey = value;
|
|
|
|
*multiplexSize = valueSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void multiplexByRidPage(byte * key, size_t keySize, byte * value, size_t valueSize, byte **multiplexKey, size_t * multiplexSize) {
|
|
|
|
*multiplexKey = (byte*)&(((recordid*)value)->page);
|
|
|
|
*multiplexSize = sizeof(((recordid*)value)->page);
|
|
|
|
}
|