Search in sources :

Example 1 with InternalDatabusEventsListener

use of com.linkedin.databus.core.InternalDatabusEventsListener in project databus by linkedin.

the class DummySuccessfulErrorCountingConsumer method writeEventsToBuffer.

static WriteEventsResult writeEventsToBuffer(DbusEventBuffer buf, DbusEventInfo[] eventInfos, int winSize) throws UnsupportedEncodingException {
    Random rng = new Random(100);
    final List<Integer> eventOfs = new ArrayList<Integer>(eventInfos.length);
    final List<DbusEventKey> eventKeys = new ArrayList<DbusEventKey>(eventInfos.length);
    InternalDatabusEventsListener ofsTracker = new InternalDatabusEventsListener() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public void onEvent(DbusEvent event, long offset, int size) {
            DbusEventInternalWritable e = (DbusEventInternalWritable) event;
            assertTrue("endEvents() gave us an invalid event! (offset = " + offset + ", size = " + size + ")", e.isValid(true));
            // FIXME: is this correct?  position() == offset; why subtract size?
            eventOfs.add(e.getRawBytes().position() - e.size());
        }
    };
    buf.addInternalListener(ofsTracker);
    for (int i = 0; i < eventInfos.length; ++i) {
        if (i % winSize == 0) {
            if (i > 0)
                buf.endEvents(i);
            buf.startEvents();
        }
        DbusEventKey key = new DbusEventKey(RngUtils.randomString(rng, rng.nextInt(50)).getBytes("UTF-8"));
        buf.appendEvent(key, eventInfos[i], null);
        eventKeys.add(key);
    }
    buf.endEvents(eventInfos.length);
    buf.removeInternalListener(ofsTracker);
    return new WriteEventsResult(eventOfs, eventKeys);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Random(java.util.Random) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventInternalWritable(com.linkedin.databus.core.DbusEventInternalWritable) ArrayList(java.util.ArrayList) DbusEventKey(com.linkedin.databus.core.DbusEventKey) InternalDatabusEventsListener(com.linkedin.databus.core.InternalDatabusEventsListener) Checkpoint(com.linkedin.databus.core.Checkpoint)

Aggregations

Checkpoint (com.linkedin.databus.core.Checkpoint)1 DbusEvent (com.linkedin.databus.core.DbusEvent)1 DbusEventInternalWritable (com.linkedin.databus.core.DbusEventInternalWritable)1 DbusEventKey (com.linkedin.databus.core.DbusEventKey)1 InternalDatabusEventsListener (com.linkedin.databus.core.InternalDatabusEventsListener)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1