001/* (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
002 *
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 *     http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License.
014 *
015 * Contributors:
016 *     bdelbosc
017 */
018package org.nuxeo.elasticsearch.core;
019
020import org.apache.commons.lang.StringUtils;
021import org.nuxeo.elasticsearch.api.IndexNameGenerator;
022
023/**
024 * @since 9.3
025 */
026public class IncrementalIndexNameGenerator implements IndexNameGenerator {
027    protected static final String SEP = "-";
028
029    @Override
030    public String getNextIndexName(String aliasName, String oldIndexName) {
031        if (StringUtils.isEmpty(oldIndexName)) {
032            return aliasName + SEP + "0000";
033        }
034        int i = oldIndexName.lastIndexOf(SEP);
035        if (i < 0) {
036            throw new IllegalArgumentException("Invalid index name: " + oldIndexName);
037        }
038        int index = Integer.parseInt(oldIndexName.substring(i + 1)) + 1;
039        return String.format("%s%s%04d", aliasName, SEP, index);
040    }
041}