001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.importer.stream.producer;
020
021import java.util.concurrent.ThreadLocalRandom;
022
023import org.nuxeo.ecm.platform.importer.random.HunspellDictionaryHolder;
024import org.nuxeo.ecm.platform.importer.random.RandomTextGenerator;
025import org.nuxeo.importer.stream.message.BlobMessage;
026import org.nuxeo.lib.stream.pattern.producer.AbstractProducer;
027
028/**
029 * Build random StringBlob message.
030 *
031 * @since 9.1
032 */
033public class RandomStringBlobMessageProducer extends AbstractProducer<BlobMessage> {
034    protected static final String DEFAULT_MIME_TYPE = "plain/text";
035
036    protected final long nbBlobs;
037
038    protected final int averageSizeKB;
039
040    protected final ThreadLocalRandom rand;
041
042    protected final String marker;
043
044    protected long count = 0;
045
046    protected static RandomTextGenerator gen;
047
048    protected final String mimetype;
049
050    public RandomStringBlobMessageProducer(int producerId, long nbBlobs, String lang, int averageSizeKB,
051            String marker) {
052        super(producerId);
053        this.nbBlobs = nbBlobs;
054        this.averageSizeKB = averageSizeKB;
055        this.mimetype = DEFAULT_MIME_TYPE;
056        if (marker != null) {
057            this.marker = marker.trim() + " ";
058        } else {
059            this.marker = "";
060        }
061        synchronized (RandomDocumentMessageProducer.class) {
062            if (gen == null) {
063                gen = new RandomTextGenerator(new HunspellDictionaryHolder(lang));
064                gen.prefilCache();
065            }
066        }
067        rand = ThreadLocalRandom.current();
068    }
069
070    @Override
071    public int getPartition(BlobMessage message, int partitions) {
072        return ((int) count) % partitions;
073    }
074
075    @Override
076    public boolean hasNext() {
077        return count < nbBlobs;
078    }
079
080    @Override
081    public BlobMessage next() {
082        String filename = generateFilename();
083        String content = generateContent();
084        BlobMessage ret = new BlobMessage.StringMessageBuilder(content).setFilename(filename)
085                                                                       .setMimetype(mimetype)
086                                                                       .build();
087        count++;
088        return ret;
089    }
090
091    protected String generateFilename() {
092        return gen.getRandomTitle(rand.nextInt(4) + 1).trim().replaceAll("\\W+", "-").toLowerCase() + ".txt";
093    }
094
095    protected String generateContent() {
096        return marker + gen.getRandomText(averageSizeKB);
097    }
098
099}