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 org.elasticsearch.action.index.IndexResponse;
020import org.elasticsearch.client.Client;
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.core.uidgen.AbstractUIDSequencer;
023import org.nuxeo.ecm.core.uidgen.UIDSequencer;
024import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * Elasticsearch implementation of {@link UIDSequencer}.
029 * <p>
030 * Since elasticsearch does not seem to support a notion of native sequence, the implementation uses the auto-increment
031 * of the version attribute as described in the <a href=
032 * "http://blogs.perl.org/users/clinton_gormley/2011/10/elasticsearchsequence---a-blazing-fast-ticket-server.html"
033 * >ElasticSearch::Sequence - a blazing fast ticket server</a> blog post.
034 *
035 * @since 7.3
036 */
037public class ESUIDSequencer extends AbstractUIDSequencer {
038
039    public static final String IDX_TYPE = "seqId";
040
041    protected Client esClient = null;
042
043    protected Client getClient() {
044        if (esClient == null) {
045            ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
046            esClient = esa.getClient();
047            try {
048                ensureESIndex(esClient);
049            } catch (NuxeoException e) {
050                esClient = null;
051                throw e;
052            }
053        }
054        return esClient;
055    }
056
057    @Override
058    public void dispose() {
059        if (esClient != null) {
060            esClient.close();
061        }
062    }
063
064    @Override
065    public int getNext(String sequenceName) {
066        String source = "{ \"ts\" : " + System.currentTimeMillis() + "}";
067        String indexName = getName();
068        IndexResponse res = getClient().prepareIndex(indexName, IDX_TYPE, sequenceName).setSource(source).execute().actionGet();
069        return (int) res.getVersion();
070    }
071
072    @Override
073    public void init() {
074        getClient();
075    }
076
077    protected void ensureESIndex(Client esClient) {
078        boolean indexExists = esClient.admin().indices().prepareExists(getName()).execute().actionGet().isExists();
079        if (!indexExists) {
080            throw new NuxeoException(String.format("Sequencer %s needs an elasticSearchIndex contribution named %s",
081                    getName(), getName()));
082        }
083    }
084
085}