com.opensearchserver.hadse.index.IndexCatalog.java Source code

Java tutorial

Introduction

Here is the source code for com.opensearchserver.hadse.index.IndexCatalog.java

Source

/** Copyright 2014 OpenSearchServer, Inc.
 * http://www.opensearchserver.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package com.opensearchserver.hadse.index;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.opensearchserver.hadse.Hadse;
import com.opensearchserver.hadse.cluster.ClusterCatalog;
import com.opensearchserver.hadse.index.shard.ShardDef;
import com.opensearchserver.hadse.index.shard.ShardItem;

public class IndexCatalog {

    /**
     * Return the File object if the directory exists
     * 
     * @param indexName
     * @return
     */
    public final static boolean exists(String indexName) {
        File file = new File(Hadse.data_dir, indexName);
        if (!file.exists())
            return false;
        if (!file.isDirectory())
            return false;
        return true;
    }

    public final static void loadAll() {
        if (Hadse.data_dir == null)
            return;
        if (!(Hadse.data_dir.isDirectory()))
            return;
        File[] indexList = Hadse.data_dir.listFiles((FileFilter) DirectoryFileFilter.INSTANCE);
        if (indexList == null)
            return;
        for (File indexDirectory : indexList) {
            IndexItem indexItem = IndexItem.get(indexDirectory.getName());
            indexItem.load();
        }
    }

    public final static ResponseEntity<?> create(String indexName, int shards, int replicas)
            throws HadseIndexException, JsonGenerationException, JsonMappingException, IOException {
        File indexDirectory = new File(Hadse.data_dir, indexName);
        if (indexDirectory.exists())
            return new ResponseEntity<Object>(HttpStatus.CONFLICT);
        if (!indexDirectory.mkdir())
            return new ResponseEntity<String>("Unable to create the index directory",
                    HttpStatus.INTERNAL_SERVER_ERROR);
        IndexItem indexItem = IndexItem.get(indexName);
        for (int shard = 1; shard <= shards; shard++) {
            String shardName = Integer.toString(shard);
            ShardDef shardDef = ShardDef.write(indexDirectory, shardName, ClusterCatalog.nextShardCandidate());
            indexItem.add(ShardItem.load(shardDef, indexDirectory));
        }
        return new ResponseEntity<String>("Index created: " + indexName, HttpStatus.CREATED);
    }

    public final static ResponseEntity<?> delete(String indexName) throws IOException {
        File indexFile = new File(Hadse.data_dir, indexName);
        if (!indexFile.exists())
            return new ResponseEntity<String>("Index not found:" + indexName, HttpStatus.NOT_FOUND);
        FileUtils.deleteDirectory(indexFile);
        if (indexFile.exists())
            return new ResponseEntity<String>("Unable to delete the directory", HttpStatus.INTERNAL_SERVER_ERROR);
        IndexItem.remove(indexName);
        return new ResponseEntity<String>("Index deleted: " + indexName, HttpStatus.OK);
    }
}