Search code examples
javaperformancedisruptor-pattern

Disruptor how to use his ring buffer to read a file?


I am looking to use dirsuptor ring buffer to parse a file. But i do not see how to set a range of value to the ring buffer.

In example below it seem. It loop to each item to assign it to the buffer. But me, i would like to assign directly x items.

When i do FileInputStream.read( byte[] bytes ), I would like put these bytes into the ring buffer.

Usually my buffer is twice bigger than bytes I read. Like this I could read another page while i am computing one ( eg bytes array length == buffer / 2 ):

ExecutorService exec = Executors.newCachedThreadPool();
// Preallocate RingBuffer with 1024 ValueEvents
Disruptor<ValueEvent> disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, 1024, exec);

// Build dependency graph
RingBuffer<ValueEvent> ringBuffer = disruptor.start();

for (long i = 10; i < 2000; i++) {
    String uuid = UUID.randomUUID().toString();
    // Two phase commit. Grab one of the 1024 slots
    long seq = ringBuffer.next();
    ValueEvent valueEvent = ringBuffer.get(seq);
    valueEvent.setValue(uuid);
    ringBuffer.publish(seq);
}

Thanks


Solution

  • Since version 3.0.0 of the Disruptor it has supported batch publication.

    There are a variety of publishEvents methods on the Ringbuffer depending on how you wish to transform your events.

    Given your sample code I suspect you want to look at this version, and supply an array of UUIDs. There are multiple versions depending on how many arguments you wish to pass to the translator.

    The alternative version is to capture the UUID in the EventTranslator it self.