/**
 * Copyright 2010 Karthik Kumar
 * 
 * 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 org.apache.nosql.lucene.index;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hbase.util.Bytes;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttributeImpl;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.store.LockObtainFailedException;

/**
 * Index Writer specific to HBase
 * 
 */
public class NoSqlIndexWriter { // TODO: extends IndexWriter {

  /**
   * Commit Log from the given index.
   */
  private final NoSqlIndexTransactionLog indexTransactionLog;

  /**
   * Field representing the primary key in the given search index.
   */
  private final String primaryKeyField;

  /**
   * The default capacity of the number of terms in the given document.
   */
  private static final int DEFAULT_TERM_CAPACITY = 50;

  /**
   * List of empty term vectors
   */
  private static final List<Integer> EMPTY_TERM_VECTOR = Arrays
      .asList(new Integer[] { 0 });

  public NoSqlIndexWriter(final NoSqlIndexTransactionLog indexTransactionLog,
      final String primaryKeyField) throws CorruptIndexException,
      LockObtainFailedException, IOException {
    // super(d, a, create, deletionPolicy, mfl);
    // TODO: bring super ctor in when we inherit from IndexWriter.

    this.indexTransactionLog = indexTransactionLog;
    this.primaryKeyField = primaryKeyField;

    // Reset the transaction Log.
    this.indexTransactionLog.init();
  }

  public void addDocument(Document doc, Analyzer analyzer)
      throws CorruptIndexException, IOException {
    String docId = doc.get(this.primaryKeyField);
    if (docId == null) {
      throw new IllegalArgumentException("Primary Key " + this.primaryKeyField
          + " not present in the document to be added ");
      // TODO: Special type of exception needed ?

    }
    List<String> allIndexedTerms = new ArrayList<String>(DEFAULT_TERM_CAPACITY);

    int position = 0;

    for (Fieldable field : doc.getFields()) {

      // Indexed field
      if (field.isIndexed() && field.isTokenized()) {

        TokenStream tokens = field.tokenStreamValue();

        if (tokens == null) {
          tokens = analyzer.tokenStream(field.name(), new StringReader(field
              .stringValue()));
        }
        tokens.addAttribute(TermAttribute.class);
        tokens.addAttribute(PositionIncrementAttribute.class);

        // collect term frequencies per doc
        Map<String, List<Integer>> termPositions = new HashMap<String, List<Integer>>();
        if (position > 0) {
          position += analyzer.getPositionIncrementGap(field.name());
        }

        // Build the termPositions vector for all terms
        while (tokens.incrementToken()) {
          String term = createColumnName(field.name(), tokens.getAttribute(
              TermAttribute.class).term());
          allIndexedTerms.add(term);

          List<Integer> pvec = termPositions.get(term);

          if (pvec == null) {
            pvec = new ArrayList<Integer>();
            termPositions.put(term, pvec);
          }

          position += (tokens.getAttribute(PositionIncrementAttribute.class)
              .getPositionIncrement() - 1);
          pvec.add(++position);

        }

        for (Map.Entry<String, List<Integer>> term : termPositions.entrySet()) {
          String key = term.getKey();
          indexTransactionLog.addTermVectors(key, Bytes.toBytes(docId), term
              .getValue());
        }
      }

      // Untokenized fields go in without a termPosition
      if (field.isIndexed() && !field.isTokenized()) {
        String term = this.createColumnName(field.name(), field.stringValue());
        allIndexedTerms.add(term);

        String key = term;

        indexTransactionLog.addTermVectors(key, Bytes.toBytes(docId),
            EMPTY_TERM_VECTOR);
      }

      // Stores each field as a column under this doc key
      if (field.isStored()) {

        byte[] _value = field.isBinary() ? field.getBinaryValue() : field
            .stringValue().getBytes();

        // last byte flags if binary or not
        byte[] value = new byte[_value.length + 1];
        System.arraycopy(_value, 0, value, 0, _value.length);

        value[value.length - 1] = (byte) (field.isBinary() ? 'B' : 'T');

        String key = docId;

        this.indexTransactionLog.storeField(Bytes.toBytes(key), field.name(),
            value);
      }
    }

    this.indexTransactionLog.commit();

  }

  // TODO: This method needs to be refactored to the NoSqlIndexWriter
  private String createColumnName(final String fieldName, final String term) {
    return fieldName + "/" + term;
  }
}

@@ -1149,49 +1149,8 @@
 er;%0A
-import org.apache.lucene.analysis.Token;%0A
 impo
@@ -1192,16 +1192,16 @@
 Stream;%0A
+
 import o
@@ -1274,90 +1274,8 @@
 te;%0A
-import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttributeImpl;%0A
 impo
@@ -1561,25 +1561,27 @@
 ter 
-specific to HBase
+in the noSQL world.
 %0A * 
@@ -2167,16 +2167,271 @@
  0 %7D);%0A%0A
+  /**%0A   * %0A   * @param indexTransactionLog%0A   * @param primaryKeyField%0A   *          The primary key field of the lucene Document schema to be used.%0A   * @throws CorruptIndexException%0A   * @throws LockObtainFailedException%0A   * @throws IOException%0A   */%0A
   public
@@ -5908,17 +5908,16 @@
 mmit();%0A
-%0A
   %7D%0A%0A  /

Clean up imports.