All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taosdata.jdbc.tmq.ConsumerRecords Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
package com.taosdata.jdbc.tmq;

import java.util.*;

public class ConsumerRecords implements Iterable> {

    public static final ConsumerRecords EMPTY = new ConsumerRecords<>(Collections.emptyMap());

    private final Map>> records;

    public ConsumerRecords(Map>> records) {
        this.records = records;
    }

    public ConsumerRecords() {
        this.records = new HashMap<>();
    }

    /**
     * Get the partitions which have records contained in this record set.
     * @return the set of partitions with data in this record set (may be empty if no data was returned)
     */
    public Set partitions() {
        return Collections.unmodifiableSet(records.keySet());
    }

    /**
     * The number of records for all topics
     */
    public int count() {
        int count = 0;
        for (List> recs: this.records.values())
            count += recs.size();
        return count;
    }

    public void put(TopicPartition tp, ConsumerRecord r) {
        if (records.containsKey(tp)) {
            records.get(tp).add(r);
        } else {
            ArrayList> list = new ArrayList<>();
            list.add(r);
            records.put(tp, list);
        }
    }

    public List> get(TopicPartition partition) {
        List> recs = this.records.get(partition);
        if (recs == null) return Collections.emptyList();
        else return Collections.unmodifiableList(recs);
    }

    @Override
    public Iterator> iterator() {
        return new Iterator>() {
            final Iterator>> iters = records.values().iterator();
            Iterator> current;

            @Override
            public boolean hasNext() {
                while (current == null || !current.hasNext()) {
                    if (iters.hasNext()) current = iters.next().iterator();
                    else return false;
                }
                return true;
            }

            @Override
            public ConsumerRecord next() {
                return current.next();
            }
        };
    }

    public boolean isEmpty() {
        return records.isEmpty();
    }

    @SuppressWarnings("unchecked")
    public static  ConsumerRecords emptyRecord() {
        return (ConsumerRecords) EMPTY;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy