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

com.atlan.pkg.cache.GlossaryCache.kt Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/* SPDX-License-Identifier: Apache-2.0
   Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.pkg.cache

import com.atlan.exception.AtlanException
import com.atlan.model.assets.Glossary
import com.atlan.model.fields.AtlanField
import com.atlan.net.HttpClient
import com.atlan.pkg.PackageContext
import com.atlan.pkg.Utils

class GlossaryCache(val ctx: PackageContext<*>) : AssetCache(ctx, "glossary") {
    private val logger = Utils.getLogger(this.javaClass.name)

    private val includesOnResults: List = listOf(Glossary.NAME, Glossary.STATUS)

    /** {@inheritDoc} */
    override fun lookupByName(name: String?) {
        val result = lookupByIdentity(name)
        if (result != null) cache(result.guid, name, result)
    }

    /** {@inheritDoc}  */
    private fun lookupByIdentity(identity: String?): Glossary? {
        try {
            return Glossary.findByName(client, identity)
        } catch (e: AtlanException) {
            logger.warn { "Unable to find glossary: $identity" }
            logger.debug(e) { "Full details: " }
        }
        identity?.let { addToIgnore(identity) }
        return null
    }

    /** {@inheritDoc} */
    override fun lookupById(id: String?) {
        val result = lookupById(id, 0, ctx.client.maxNetworkRetries)
        if (result != null) cache(result.guid, getIdentityForAsset(result), result)
    }

    /** {@inheritDoc}  */
    private fun lookupById(
        guid: String?,
        currentAttempt: Int,
        maxRetries: Int,
    ): Glossary? {
        try {
            val glossary =
                Glossary.select(client)
                    .where(Glossary.GUID.eq(guid))
                    .includesOnResults(includesOnResults)
                    .pageSize(1)
                    .stream()
                    .findFirst()
            if (glossary.isPresent) {
                return glossary.get() as Glossary
            } else {
                if (currentAttempt >= maxRetries) {
                    logger.warn { "No glossary found with GUID: $guid" }
                } else {
                    Thread.sleep(HttpClient.waitTime(currentAttempt).toMillis())
                    return lookupById(guid, currentAttempt + 1, maxRetries)
                }
            }
        } catch (e: AtlanException) {
            logger.warn { "Unable to lookup or find glossary: $guid" }
            logger.debug(e) { "Full stack trace:" }
        }
        guid?.let { addToIgnore(guid) }
        return null
    }

    /** {@inheritDoc}  */
    override fun getIdentityForAsset(asset: Glossary): String {
        return asset.name
    }

    /** {@inheritDoc} */
    override fun refreshCache() {
        val count = Glossary.select(client).count()
        logger.info { "Caching all $count glossaries, up-front..." }
        Glossary.select(client)
            .includesOnResults(includesOnResults)
            .stream(true)
            .forEach { glossary ->
                glossary as Glossary
                cache(glossary.guid, getIdentityForAsset(glossary), glossary)
            }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy