Search in sources :

Example 1 with CdcEvent

use of org.apache.ignite.cdc.CdcEvent in project ignite by apache.

the class WalRecordsConsumer method onRecords.

/**
 * Handles record from the WAL.
 * If this method return {@code true} then current offset in WAL will be stored and WAL iteration will be
 * started from it on CDC application fail/restart.
 *
 * @param recs WAL records iterator.
 * @return {@code True} if current offset in WAL should be commited.
 */
public boolean onRecords(Iterator<DataRecord> recs) {
    Iterator<CdcEvent> evts = new Iterator<CdcEvent>() {

        /**
         */
        private Iterator<CdcEvent> entries;

        @Override
        public boolean hasNext() {
            advance();
            return hasCurrent();
        }

        @Override
        public CdcEvent next() {
            advance();
            if (!hasCurrent())
                throw new NoSuchElementException();
            evtsCnt.increment();
            lastEvtTs.value(System.currentTimeMillis());
            return entries.next();
        }

        private void advance() {
            if (hasCurrent())
                return;
            while (recs.hasNext()) {
                entries = F.iterator(recs.next().writeEntries().iterator(), this::transform, true, OPERATIONS_FILTER);
                if (entries.hasNext())
                    break;
                entries = null;
            }
        }

        private boolean hasCurrent() {
            return entries != null && entries.hasNext();
        }

        /**
         */
        private CdcEvent transform(DataEntry e) {
            UnwrappedDataEntry ue = (UnwrappedDataEntry) e;
            return new CdcEventImpl(ue.unwrappedKey(), ue.unwrappedValue(), (e.flags() & DataEntry.PRIMARY_FLAG) != 0, e.partitionId(), e.writeVersion(), e.cacheId());
        }
    };
    return consumer.onEvents(evts);
}
Also used : DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) Iterator(java.util.Iterator) CdcEvent(org.apache.ignite.cdc.CdcEvent) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 CdcEvent (org.apache.ignite.cdc.CdcEvent)1 DataEntry (org.apache.ignite.internal.pagemem.wal.record.DataEntry)1 UnwrappedDataEntry (org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry)1