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

com.questdb.JournalIterators Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
/*******************************************************************************
 *    ___                  _   ____  ____
 *   / _ \ _   _  ___  ___| |_|  _ \| __ )
 *  | | | | | | |/ _ \/ __| __| | | |  _ \
 *  | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *   \__\_\\__,_|\___||___/\__|____/|____/
 *
 * Copyright (C) 2014-2016 Appsicle
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *
 ******************************************************************************/

package com.questdb;

import com.questdb.ex.JournalException;
import com.questdb.ex.JournalRuntimeException;
import com.questdb.iter.*;
import com.questdb.misc.Interval;
import com.questdb.misc.Rows;
import com.questdb.query.OrderedResultSetBuilder;
import com.questdb.std.ObjList;

import java.util.Iterator;

public final class JournalIterators {

    private JournalIterators() {
    }

    public static  JournalPeekingIterator bufferedIterator(Journal journal) {
        return new JournalBufferedIterator<>(journal, createRanges(journal));
    }

    public static  JournalPeekingIterator bufferedIterator(Journal journal, Interval interval) {
        return new JournalBufferedIterator<>(journal, createRanges(journal, interval));
    }

    public static  JournalPeekingIterator bufferedIterator(Journal journal, long rowid) {
        return new JournalBufferedIterator<>(journal, createRanges(journal, rowid));
    }

    public static  JournalConcurrentIterator concurrentIterator(Journal journal) {
        return new JournalConcurrentIterator<>(journal, createRanges(journal), 1024);
    }

    public static  JournalConcurrentIterator concurrentIterator(Journal journal, Interval interval) {
        return new JournalConcurrentIterator<>(journal, createRanges(journal, interval), 1024);
    }

    public static  JournalConcurrentIterator concurrentIterator(Journal journal, long rowid) {
        return new JournalConcurrentIterator<>(journal, createRanges(journal, rowid), 1024);
    }

    public static  JournalPeekingIterator incrementBufferedIterator(Journal journal) {
        try {
            long lo = journal.getMaxRowID();
            journal.refresh();
            return new JournalBufferedIterator<>(journal, createRanges(journal, journal.incrementRowID(lo)));
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
    }

    public static  JournalPeekingIterator incrementIterator(Journal journal) {
        try {
            long lo = journal.getMaxRowID();
            journal.refresh();
            return new JournalIteratorImpl<>(journal, createRanges(journal, journal.incrementRowID(lo)));
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
    }

    public static  JournalIterator iterator(Journal journal, Interval interval) {
        return new JournalIteratorImpl<>(journal, createRanges(journal, interval));
    }

    public static  JournalPeekingIterator iterator(Journal journal, long rowid) {
        return new JournalIteratorImpl<>(journal, createRanges(journal, rowid));
    }

    public static  Iterator iterator(Journal journal) {
        return new JournalIteratorImpl<>(journal, createRanges(journal));
    }

    private static  ObjList createRanges(Journal journal) {
        final int partitionCount = journal.getPartitionCount();
        ObjList ranges = new ObjList<>(partitionCount);
        try {
            for (int i = 0; i < partitionCount; i++) {
                Partition p = journal.getPartition(i, true);
                long size = p.size();
                if (size > 0) {
                    ranges.add(new JournalIteratorRange(p.getPartitionIndex(), 0, size - 1));
                }
            }
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
        return ranges;
    }

    private static  ObjList createRanges(Journal journal, Interval interval) {
        final ObjList ranges = new ObjList<>();
        try {
            journal.iteratePartitions(new OrderedResultSetBuilder(interval) {
                @Override
                public void read(long lo, long hi) throws JournalException {
                    ranges.add(new JournalIteratorRange(partition.getPartitionIndex(), lo, hi));
                }
            });
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
        return ranges;
    }

    private static  ObjList createRanges(Journal journal, long lo) {
        ObjList ranges = new ObjList<>();
        int loPartitionID = Rows.toPartitionIndex(lo);
        long loLocalRowID = Rows.toLocalRowID(lo);

        try {
            int count = journal.getPartitionCount();
            for (int i = loPartitionID; i < count; i++) {
                long localRowID = 0;
                if (i == loPartitionID) {
                    localRowID = loLocalRowID;
                }

                Partition p = journal.getPartition(i, true);
                long size = p.size();
                if (size > 0) {
                    ranges.add(new JournalIteratorRange(p.getPartitionIndex(), localRowID, size - 1));
                }
            }
            return ranges;
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy