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

ai.platon.pulsar.common.collect.collector.QueueCollector.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package ai.platon.pulsar.common.collect.collector

import ai.platon.pulsar.common.Priority13
import ai.platon.pulsar.common.urls.UrlAware
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue

open class QueueCollector(
    val queue: Queue = ConcurrentLinkedQueue(),
    priority: Int = Priority13.NORMAL.value
) : AbstractPriorityDataCollector(priority) {

    override var name = "QueueC"

    override val size: Int
        get() = queue.size

    var loadArgs: String? = null

    constructor(priority: Priority13): this(ConcurrentLinkedQueue(), priority.value)

    override fun hasMore() = queue.isNotEmpty()

    override fun collectTo(sink: MutableList): Int {
        beforeCollect()

        var count = 0
        val url = queue.poll()
        if (url != null) {
            if (loadArgs != null) {
                url.args += " $loadArgs"
            }
            if (url.label.isNotBlank()) {
                labels.add(url.label)
            }

            sink.add(url)
            ++count
        }

        return afterCollect(count)
    }

    override fun dump(): List {
        return queue.map { it.url }
    }

    override fun clear() = queue.clear()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy