<commit_before>/**
 * Copyright 2008 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 net.sf.katta;

import java.util.List;

import net.sf.katta.client.Client;
import net.sf.katta.client.IClient;
import net.sf.katta.index.IndexMetaData;
import net.sf.katta.master.IPaths;
import net.sf.katta.slave.Hit;
import net.sf.katta.slave.Hits;
import net.sf.katta.slave.IQuery;
import net.sf.katta.slave.Query;
import net.sf.katta.slave.SlaveMetaData;
import net.sf.katta.util.KattaException;
import net.sf.katta.util.ZkConfiguration;
import net.sf.katta.zk.ZKClient;

public class Katta {

  private final ZKClient _client = new ZKClient(new ZkConfiguration());

  public static void main(final String[] args) throws KattaException {
    if (args.length < 1) {
      usage();
    }
    final String command = args[0];
    final Katta katta = new Katta();
    if (command.equals("search")) {
      final String[] indexNames = args[1].split(",");
      final String query = args[2];
      if (args.length > 3) {
        final int count = Integer.parseInt(args[3]);
        katta.search(indexNames, query, count);
      } else {
        katta.search(indexNames, query);
      }
    } else if (command.endsWith("addIndex")) {
      katta.addIndex(args[1], args[2], args[3]);
    } else if (command.endsWith("removeIndex")) {
      katta.removeIndex(args[1]);
    } else if (command.endsWith("listIndexes")) {
      katta.listIndex();
    } else if (command.endsWith("listSlaves")) {
      katta.listSlaves();
    } else if (command.endsWith("startSlave")) {
      startSlave();
    } else if (command.endsWith("startMaster")) {
      startMaster();
    } else if (command.endsWith("showStructure")) {
      katta.showStructure();
    }
  }

  public void removeIndex(final String indexName) throws KattaException {
    _client.waitForZooKeeper(5000);
    final String indexPath = IPaths.INDEXES + "/" + indexName;
    if (_client.exists(indexPath)) {
      _client.delete(indexPath);
    } else {
      System.err.println("Unknown index:" + indexName);
    }

  }

  public void showStructure() throws KattaException {
    _client.waitForZooKeeper(5000);
    _client.showFolders(System.out);
  }

  public static void startMaster() throws KattaException {
    final ZkConfiguration conf = new ZkConfiguration();
    final Server server = new Server(conf);
    final ZKClient client = new ZKClient(conf);
    client.waitForZooKeeper(5000);
    server.startMasterOrSlave(client, true);
    server.join();
  }

  public static void startSlave() throws KattaException {
    final ZkConfiguration configuration = new ZkConfiguration();
    final ZKClient client = new ZKClient(configuration);
    client.waitForZooKeeper(5000);
    final Server slave = new Server(configuration);
    slave.startMasterOrSlave(client, false);
    slave.join();
  }

  public void listSlaves() throws KattaException {
    _client.waitForZooKeeper(5000);
    final List<String> slaves = _client.getChildren(IPaths.SLAVES);
    if (null != slaves) {
      // header
      System.out.println("name \t:\t start time  \t:\t is healthy  \t:\t status");
      for (final String slave : slaves) {
        final String path = IPaths.SLAVES + "/" + slave;
        final SlaveMetaData slaveMetaData = new SlaveMetaData();
        _client.readData(path, slaveMetaData);
        System.out.println(slaveMetaData.toString());
      }
    }
  }

  public void listIndex() throws KattaException {
    _client.waitForZooKeeper(5000);
    final List<String> indexes = _client.getChildren(IPaths.INDEXES);
    for (final String index : indexes) {
      final IndexMetaData indexMetaData = new IndexMetaData();
      _client.readData(IPaths.INDEXES + "/" + index, indexMetaData);
      System.out.println("index: " + index + "\n\tisDeployed: " + indexMetaData.isDeployed() + "\n\tpath: "
          + indexMetaData.getPath());
      // maybe show shards
      // maybe show serving slaves..
      // maybe show replication level...
    }
  }

  public void addIndex(final String name, final String path, final String analyzerClass) throws KattaException {
    _client.waitForZooKeeper(5000);
    _client.showFolders(System.out);
    _client.create(IPaths.INDEXES + "/" + name, new IndexMetaData(path, analyzerClass, false));
    boolean indexDeployed = false;
    while (!indexDeployed) {
      final IndexMetaData indexMetaData = new IndexMetaData();
      _client.readData(IPaths.INDEXES + "/" + name, indexMetaData);
      indexDeployed = indexMetaData.isDeployed();
    }
    // maybe check if index is valid...
  }

  public void search(final String[] indexNames, final String queryString, final int count) throws KattaException {
    final IClient client = new Client();
    final IQuery query = new Query(queryString);
    final long start = System.currentTimeMillis();
    final Hits hits = client.search(query, indexNames, count);
    final long end = System.currentTimeMillis();
    System.out.println(hits.size() + " hits found in " + ((end - start) / 1000.0) + "sec.");
    int index = 0;
    for (final Hit hit : hits.getHits()) {
      System.out.println("Hit: " + index);
      System.out.println("\tSlave:\t" + hit.getSlave());
      System.out.println("\tShard:\t" + hit.getShard());
      System.out.println("\tDocId:\t" + hit.getDocId());
      System.out.println("\tScore:\t" + hit.getScore());
      index++;
    }
  }

  public void search(final String[] indexNames, final String queryString) throws KattaException {
    final IClient client = new Client();
    final IQuery query = new Query(queryString);
    final long start = System.currentTimeMillis();
    final int hitsSize = client.count(query, indexNames);
    final long end = System.currentTimeMillis();
    System.out.println(hitsSize + " Hits found in " + ((end - start) / 1000.0) + "sec.");<commit_msg>o ask only 1 times per second for the deployed index o make the waiting interruptable<commit_after>/**
 * Copyright 2008 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 net.sf.katta;

import java.util.List;

import net.sf.katta.client.Client;
import net.sf.katta.client.IClient;
import net.sf.katta.index.IndexMetaData;
import net.sf.katta.master.IPaths;
import net.sf.katta.slave.Hit;
import net.sf.katta.slave.Hits;
import net.sf.katta.slave.IQuery;
import net.sf.katta.slave.Query;
import net.sf.katta.slave.SlaveMetaData;
import net.sf.katta.util.KattaException;
import net.sf.katta.util.ZkConfiguration;
import net.sf.katta.zk.ZKClient;

public class Katta {

  private final ZKClient _client = new ZKClient(new ZkConfiguration());

  public static void main(final String[] args) throws KattaException, InterruptedException {
    if (args.length < 1) {
      usage();
    }
    final String command = args[0];
    final Katta katta = new Katta();
    if (command.equals("search")) {
      final String[] indexNames = args[1].split(",");
      final String query = args[2];
      if (args.length > 3) {
        final int count = Integer.parseInt(args[3]);
        katta.search(indexNames, query, count);
      } else {
        katta.search(indexNames, query);
      }
    } else if (command.endsWith("addIndex")) {
      katta.addIndex(args[1], args[2], args[3]);
    } else if (command.endsWith("removeIndex")) {
      katta.removeIndex(args[1]);
    } else if (command.endsWith("listIndexes")) {
      katta.listIndex();
    } else if (command.endsWith("listSlaves")) {
      katta.listSlaves();
    } else if (command.endsWith("startSlave")) {
      startSlave();
    } else if (command.endsWith("startMaster")) {
      startMaster();
    } else if (command.endsWith("showStructure")) {
      katta.showStructure();
    }
  }

  public void removeIndex(final String indexName) throws KattaException {
    _client.waitForZooKeeper(5000);
    final String indexPath = IPaths.INDEXES + "/" + indexName;
    if (_client.exists(indexPath)) {
      _client.delete(indexPath);
    } else {
      System.err.println("Unknown index:" + indexName);
    }

  }

  public void showStructure() throws KattaException {
    _client.waitForZooKeeper(5000);
    _client.showFolders(System.out);
  }

  public static void startMaster() throws KattaException {
    final ZkConfiguration conf = new ZkConfiguration();
    final Server server = new Server(conf);
    final ZKClient client = new ZKClient(conf);
    client.waitForZooKeeper(5000);
    server.startMasterOrSlave(client, true);
    server.join();
  }

  public static void startSlave() throws KattaException {
    final ZkConfiguration configuration = new ZkConfiguration();
    final ZKClient client = new ZKClient(configuration);
    client.waitForZooKeeper(5000);
    final Server slave = new Server(configuration);
    slave.startMasterOrSlave(client, false);
    slave.join();
  }

  public void listSlaves() throws KattaException {
    _client.waitForZooKeeper(5000);
    final List<String> slaves = _client.getChildren(IPaths.SLAVES);
    if (null != slaves) {
      // header
      System.out.println("name \t:\t start time  \t:\t is healthy  \t:\t status");
      for (final String slave : slaves) {
        final String path = IPaths.SLAVES + "/" + slave;
        final SlaveMetaData slaveMetaData = new SlaveMetaData();
        _client.readData(path, slaveMetaData);
        System.out.println(slaveMetaData.toString());
      }
    }
  }

  public void listIndex() throws KattaException {
    _client.waitForZooKeeper(5000);
    final List<String> indexes = _client.getChildren(IPaths.INDEXES);
    for (final String index : indexes) {
      final IndexMetaData indexMetaData = new IndexMetaData();
      _client.readData(IPaths.INDEXES + "/" + index, indexMetaData);
      System.out.println("index: " + index + "\n\tisDeployed: " + indexMetaData.isDeployed() + "\n\tpath: "
          + indexMetaData.getPath());
      // maybe show shards
      // maybe show serving slaves..
      // maybe show replication level...
    }
  }

  public void addIndex(final String name, final String path, final String analyzerClass) throws KattaException,
      InterruptedException {
    _client.waitForZooKeeper(5000);
    _client.showFolders(System.out);
    _client.create(IPaths.INDEXES + "/" + name, new IndexMetaData(path, analyzerClass, false));
    boolean indexDeployed = false;
    while (!indexDeployed) {
      final IndexMetaData indexMetaData = new IndexMetaData();
      _client.readData(IPaths.INDEXES + "/" + name, indexMetaData);
      indexDeployed = indexMetaData.isDeployed();
      Thread.sleep(1000);
    }
    // maybe check if index is valid...
  }

  public void search(final String[] indexNames, final String queryString, final int count) throws KattaException {
    final IClient client = new Client();
    final IQuery query = new Query(queryString);
    final long start = System.currentTimeMillis();
    final Hits hits = client.search(query, indexNames, count);
    final long end = System.currentTimeMillis();
    System.out.println(hits.size() + " hits found in " + ((end - start) / 1000.0) + "sec.");
    int index = 0;
    for (final Hit hit : hits.getHits()) {
      System.out.println("Hit: " + index);
      System.out.println("\tSlave:\t" + hit.getSlave());
      System.out.println("\tShard:\t" + hit.getShard());
      System.out.println("\tDocId:\t" + hit.getDocId());
      System.out.println("\tScore:\t" + hit.getScore());
      index++;
    }
  }

  public void search(final String[] indexNames, final String queryString) throws KattaException {
    final IClient client = new Client();
    final IQuery query = new Query(queryString);
    final long start = System.currentTimeMillis();
    final int hitsSize = client.count(query, indexNames);
    final long end = System.currentTimeMillis();
    System.out.println(hitsSize + " Hits found in " + ((end - start) / 1000.0) + "sec.");
{'fraction_non_alphanumeric': 0.1198328607670497, 'fraction_numerical': 0.008879271750485002, 'mean_word_length': 3.420514511873351, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '959377', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3981, 'n_words': 1089}