001/*
002 * (C) Copyright 2014-2015 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 *     Thierry Delprat
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.redis.contribs;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.core.redis.RedisAdmin;
029import org.nuxeo.ecm.core.redis.RedisExecutor;
030import org.nuxeo.ecm.core.uidgen.AbstractUIDSequencer;
031import org.nuxeo.runtime.api.Framework;
032
033import redis.clients.jedis.exceptions.JedisException;
034
035/**
036 * Redis-based UID generator.
037 *
038 * @since 7.4
039 */
040public class RedisUIDSequencer extends AbstractUIDSequencer {
041
042    protected static final Log log = LogFactory.getLog(RedisUIDSequencer.class);
043
044    protected String namespace;
045
046    @Override
047    public void init() {
048        RedisAdmin redisAdmin = Framework.getService(RedisAdmin.class);
049        namespace = redisAdmin.namespace("counters");
050    }
051
052    @Override
053    public void dispose() {
054    }
055
056    @Override
057    public void initSequence(String key, long id) {
058        RedisExecutor executor = Framework.getService(RedisExecutor.class);
059        try {
060            executor.execute(jedis -> jedis.set(namespace + key, String.valueOf(id)));
061        } catch (JedisException e) {
062            throw new NuxeoException(e);
063        }
064    }
065
066    @Override
067    public long getNextLong(String key) {
068        RedisExecutor executor = Framework.getService(RedisExecutor.class);
069        try {
070            return executor.execute(jedis -> jedis.incr(namespace + key));
071        } catch (JedisException e) {
072            throw new NuxeoException(e);
073        }
074    }
075
076    @Override
077    public List<Long> getNextBlock(String key, int blockSize) {
078        List<Long> ret = new ArrayList<>(blockSize);
079        if (blockSize == 1) {
080            ret.add(getNextLong(key));
081            return ret;
082        }
083        RedisExecutor executor = Framework.getService(RedisExecutor.class);
084        long last;
085        try {
086            last = executor.execute(jedis -> jedis.incrBy(namespace + key, blockSize));
087        } catch (JedisException e) {
088            throw new NuxeoException(e);
089        }
090        for (int i = blockSize - 1; i >= 0; i--) {
091            ret.add(last - i);
092        }
093        return ret;
094    }
095}