Giter VIP home page Giter VIP logo

Comments (1)

dmlux avatar dmlux commented on July 28, 2024

I ran into the same problem and solved it by writing the sitemap contents to strings and check the length of these strings. If any of the generated lists exceeds 10Mb then I decreased the number of max URLs and started all over again. This might take up some time but if you have a chance to test locally with your production data you can find a better starting value for maxUrls which boosts the performance of the while loop for your production code. In our case it took at most a second or third try to find a better value for maxUrls.

private final Map<String, Pair<Date, String>> sitemaps = new HashMap<>();
private final AtomicInteger maxUrls = new AtomicInteger(50000);
private void generateGoogleImageSitemapContents(String fileNamePrefix, Function<Integer, GoogleImageSitemapGenerator> sitemapCreator) {
    Predicate<GoogleImageSitemapGenerator> exceeds10Mb = googleImageSitemapGenerator -> googleImageSitemapGenerator.writeAsStrings()
        .stream()
        .anyMatch(list -> list.getBytes(US_ASCII).length > 1e+7);
        
    GoogleImageSitemapGenerator generator = sitemapCreator.apply(maxUrls.get());
    if (generator == null) {
        return;
    }
    while (exceeds10Mb.test(generator) && maxUrls.get() > 0) {
        generator = sitemapCreator.apply(maxUrls.addAndGet(-maxUrls.get() / 2));
    }

    // In theory now all lists are smaller than 10Mb lets store them for later
    AtomicInteger counter = new AtomicInteger(0);
    generator.writeAsStrings()
        .forEach(sitemapContent -> sitemaps.put(fileNamePrefix + counter.getAndIncrement() + ".xml", Pair.of(new Date(), sitemapContent)));
}

And later you can use this method like this

generateGoogleImageSitemapContents("changes_daily_with_images_", maxUrls -> {
    try {
        GoogleImageSitemapGenerator imageGenerator = GoogleImageSitemapGenerator.builder(baseUrl, null)
            .dateFormat(new W3CDateFormat(MINUTE))
            .maxUrls(maxUrls)
            .build();

        addProducts(imageGenerator);
        addCategories(imageGenerator);

        return imageGenerator;
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
});

In my case I store the sitemaps in-memory and deliver them on request. The Pair of date and sitemap content is used to add a <lastMod/>-Tag to each list. The lists are created before they are queried and are ready if the crawler requests them. This way it is also possible to update the list in background and use Google-Ping-Service to inform the crawler about changes.

from sitemapgen4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.