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

de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.EntitySetJooqCurser Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
 * Karlsruhe, Germany.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntitySet;
import de.fraunhofer.iosb.ilt.frostserver.path.UrlHelper;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.DataSize;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.query.Query;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.jooq.Cursor;
import org.jooq.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An entity set backed by a database Curser. This can iterated over exactly
 * once.
 */
public class EntitySetJooqCurser implements EntitySet {

    private static final Logger LOGGER = LoggerFactory.getLogger(EntitySetJooqCurser.class.getName());

    private final Query staQuery;
    private final QueryState queryState;
    private final Cursor results;
    private final DataSize size;
    private final ResultBuilder resultBuilder;

    private String nextLink;
    private long count = -1;
    private int maxFetch;

    private final EntityType type;
    private NavigationPropertyMain.NavigationPropertyEntitySet navigationProperty;

    private CursorIterator iterator;
    private Entity first;
    private int fetchedCount;

    public EntitySetJooqCurser(EntityType type, Cursor results, QueryState queryState, ResultBuilder resultBuilder) {
        this.type = type;
        this.staQuery = resultBuilder.getStaQuery();
        this.results = results;
        this.size = resultBuilder.getDataSize();
        this.queryState = queryState;
        this.resultBuilder = resultBuilder;
        this.maxFetch = staQuery.getTopOrDefault();
    }

    @Override
    public Entity first() {
        return first;
    }

    @Override
    public void add(Entity entity) {
        throw new UnsupportedOperationException("Not supported on this implementation.");
    }

    @Override
    public int size() {
        throw new UnsupportedOperationException("Not supported on this implementation.");
    }

    @Override
    public long getCount() {
        return count;
    }

    @Override
    public void setCount(long count) {
        this.count = count;
    }

    @Override
    public String getNextLink() {
        if (nextLink == null && results.hasNext() && maxFetch > 0) {
            generateNextLink();
        }
        return nextLink;
    }

    private void generateNextLink() {
        nextLink = UrlHelper.generateNextLink(resultBuilder.getPath(), staQuery, maxFetch);
    }

    @Override
    public void setNextLink(String nextLink) {
        this.nextLink = nextLink;
    }

    @Override
    public EntityType getEntityType() {
        return type;
    }

    @Override
    public NavigationPropertyMain.NavigationPropertyEntitySet getNavigationProperty() {
        return navigationProperty;
    }

    @Override
    public boolean isEmpty() {
        return !results.hasNext();
    }

    @Override
    public Iterator iterator() {
        if (iterator == null) {
            iterator = new CursorIterator(this);
            return iterator;
        }
        throw new IllegalStateException("EntitySetJooqCurser can only be iterated once.");
    }

    private static class CursorIterator implements Iterator {

        private final EntitySetJooqCurser parent;

        public CursorIterator(EntitySetJooqCurser parent) {
            this.parent = parent;
        }

        @Override
        public boolean hasNext() {
            return parent.results.hasNext() && parent.maxFetch > parent.fetchedCount;
        }

        @Override
        public Entity next() {
            if (!hasNext()) {
                throw new NoSuchElementException("Cursor is closed or empty.");
            }
            parent.fetchedCount++;
            final Entity entity = fetchNext();
            if (parent.size.isExceeded()) {
                LOGGER.debug("Size limit reached: {} > {}.", parent.size.getDataSize(), parent.size.getMaxSize());
                parent.maxFetch = parent.fetchedCount;
                generateNextLinkAndClose(entity);
            } else if (parent.fetchedCount >= parent.maxFetch) {
                generateNextLinkAndClose(entity);
            }
            parent.resultBuilder.expandEntity(entity, parent.staQuery);
            if (parent.first == null) {
                parent.first = entity;
            }
            return entity;
        }

        private Entity fetchNext() {
            final Record tuple = parent.results.fetchNext();
            return parent.queryState.entityFromRecord(tuple, parent.size, parent.staQuery);
        }

        private void generateNextLink(Entity last, Entity next) {
            parent.nextLink = UrlHelper.generateNextLink(parent.resultBuilder.getPath(), parent.staQuery, parent.maxFetch, last, next);
        }

        private void generateNextLinkAndClose(Entity entity) {
            if (parent.results.hasNext()) {
                final Entity next = fetchNext();
                generateNextLink(entity, next);
            }
            parent.results.close();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy