All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hmsonline.cassandra.index.Configuration Maven / Gradle / Ivy

package com.hmsonline.cassandra.index;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

public class Configuration {
  public static final String KEYSPACE = "keyspace";
  public static final String COLUMN_FAMILY = "column_family";
  public static final String COLUMNS = "columns";
  private static final String COLUMN_DELIM = ",";

  private Map>> config = new HashMap>>();

  public void addIndex(String indexName, Map indexProperties) {
    String keyspace = indexProperties.get(KEYSPACE);
    String columnFamily = indexProperties.get(COLUMN_FAMILY);
    String columns = indexProperties.get(COLUMNS);

    if (StringUtils.isEmpty(keyspace) || StringUtils.isEmpty(columnFamily)
            || StringUtils.isEmpty(columns)) {
      return;
    }

    Set columnSet = new LinkedHashSet();
    for (String column : columns.split(COLUMN_DELIM)) {
      if (StringUtils.isNotEmpty(column)) {
        columnSet.add(column.trim());
      }
    }

    String key = generateKey(keyspace, columnFamily);
    if (!config.containsKey(key)) {
      config.put(key, new HashMap>());
    }
    config.get(key).put(indexName, columnSet);
  }

  public Map> getIndexes(String keyspace,
          String columnFamily) {
    String key = generateKey(keyspace, columnFamily);
    return config.containsKey(key) ? config.get(key)
            : new HashMap>();
  }

  public Set getIndexNames(String keyspace, String columnFamily) {
    return getIndexes(keyspace, columnFamily).keySet();
  }

  public Set getIndexColumns(String keyspace, String columnFamily,
          String indexName) {
    return getIndexes(keyspace, columnFamily).get(indexName);
  }

  public boolean isEmpty() {
    return config.isEmpty();
  }

  public void clear() {
    config.clear();
  }

  private String generateKey(String keyspace, String columnFamily) {
    return keyspace.hashCode() + "_" + columnFamily.hashCode();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy