001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat <tdelprat@nuxeo.com>
016 */
017package org.nuxeo.elasticsearch.seqgen;
018
019import java.util.NoSuchElementException;
020
021import org.elasticsearch.action.index.IndexResponse;
022import org.elasticsearch.client.Client;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.ecm.core.uidgen.AbstractUIDSequencer;
025import org.nuxeo.ecm.core.uidgen.UIDSequencer;
026import org.nuxeo.elasticsearch.ElasticSearchConstants;
027import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Elasticsearch implementation of {@link UIDSequencer}.
032 * <p>
033 * Since elasticsearch does not seem to support a notion of native sequence, the implementation uses the auto-increment
034 * of the version attribute as described in the <a href=
035 * "http://blogs.perl.org/users/clinton_gormley/2011/10/elasticsearchsequence---a-blazing-fast-ticket-server.html"
036 * >ElasticSearch::Sequence - a blazing fast ticket server</a> blog post.
037 *
038 * @since 7.3
039 */
040public class ESUIDSequencer extends AbstractUIDSequencer {
041
042    protected Client esClient = null;
043
044    protected Client getClient() {
045        if (esClient == null) {
046            ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
047            esClient = esa.getClient();
048            try {
049                ensureESIndex(esClient);
050            } catch (NoSuchElementException | NuxeoException e) {
051                esClient = null;
052                throw e;
053            }
054        }
055        return esClient;
056    }
057
058    @Override
059    public void dispose() {
060        if (esClient != null) {
061            esClient.close();
062        }
063    }
064
065    @Override
066    public int getNext(String sequenceName) {
067        String source = "{ \"ts\" : " + System.currentTimeMillis() + "}";
068        IndexResponse res = getClient().prepareIndex(getESIndexName(), ElasticSearchConstants.SEQ_ID_TYPE, sequenceName).setSource(
069                source).execute().actionGet();
070        return (int) res.getVersion();
071    }
072
073    @Override
074    public void init() {
075        getClient();
076    }
077
078    protected void ensureESIndex(Client esClient) {
079        boolean indexExists = esClient.admin().indices().prepareExists(getESIndexName()).execute().actionGet().isExists();
080        if (!indexExists) {
081            throw new NuxeoException(String.format(
082                    "Sequencer %s needs an elasticSearchIndex contribution with type %s", getName(),
083                    ElasticSearchConstants.SEQ_ID_TYPE));
084        }
085    }
086
087    protected String getESIndexName() {
088        ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
089        return esa.getIndexNameForType(ElasticSearchConstants.SEQ_ID_TYPE);
090    }
091
092}