package com.github.rnewson.couchdb.lucene.v2;

/**
 * Copyright 2009 Robert Newson
 * 
 * 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.
 */

import net.sf.json.JSONArray;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;

class Utils {

    /*
     * public static final Logger LOG = Logger.getLogger("couchdb-lucene");
     * 
     * private static PrintWriter OUT;
     * 
     * static { try { OUT = new PrintWriter(new OutputStreamWriter(System.out,
     * "UTF-8")); } catch (final UnsupportedEncodingException e) { throw new
     * Error("UTF-8 support is missing from your JVM."); } }
     * 
     * public static void out(final Object obj) { if (OUT == null) { try { OUT =
     * new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")); } catch
     * (final UnsupportedEncodingException e) { throw new
     * Error("UTF-8 support is missing from your JVM."); } }
     * OUT.println(obj.toString()); OUT.flush(); }
     * 
     * public static String throwableToJSON(final Throwable t) { return
     * error(t.getMessage() == null ? "Unknown error" : String.format("%s: %s",
     * t.getClass(), t.getMessage())); }
     * 
     * public static String error(final String txt) { return error(500, txt); }
     * 
     * public static String digest(final String data) { return
     * DigestUtils.md5Hex(data); }
     * 
     * public static String error(final int code, final Throwable t) { final
     * StringWriter writer = new StringWriter(); final PrintWriter printWriter =
     * new PrintWriter(writer); if (t.getMessage() != null) {
     * printWriter.append(t.getMessage()); printWriter.append("\n"); }
     * t.printStackTrace(printWriter); return new JSONObject().element("code",
     * code).element("body", "<pre>" + writer + "</pre>").toString(); }
     * 
     * public static String error(final int code, final String txt) { return new
     * JSONObject().element("code", code).element("body",
     * StringEscapeUtils.escapeHtml(txt)).toString(); }
     */

    public static Field text(final String name, final String value, final boolean store) {
        return new Field(name, value, store ? Store.YES : Store.NO, Field.Index.ANALYZED);
    }

    /*
     * public static Field token(final String name, final String value, final
     * boolean store) { return new Field(name, value, store ? Store.YES :
     * Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS); }
     * 
     * public static Query docQuery(final String viewname, final String id) {
     * BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new
     * Term(Constants.VIEW, viewname)), Occur.MUST); q.add(new TermQuery(new
     * Term(Constants.ID, id)), Occur.MUST); return q; }
     */

    public static String viewname(final String databaseName, final String designDocumentName, final String viewName) {
        return String.format("%s_%s_%s", databaseName, designDocumentName, viewName);        
    }

    public static String viewname(final JSONArray path) {
        return viewname(path.getString(0), path.getString(2), path.getString(3));
    }

}

@@ -650,29 +650,177 @@
 ort 
-net.sf.json.JSONArray
+java.io.File;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.math.BigInteger;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException
 ;%0A%0Ai
@@ -3392,49 +3392,39 @@
 tic 
-String
+File
  view
-name
+dir
 (final 
-String databaseName
+File baseDir
 , fi
@@ -3435,29 +3435,23 @@
 String d
-esignDocument
+atabase
 Name, fi
@@ -3465,20 +3465,24 @@
 ing view
-Name
+Function
 ) %7B%0A    
@@ -3489,93 +3489,129 @@
     
-return String.format(%22%25s_%25s_%25s%22, databaseName, designDocumentName, viewName);        
+File result = new File(baseDir, databaseName);%0A        return new File(result, md5(viewFunction.replaceAll(%22%5C%5Cs+%22, %22%22)));
 %0A   
@@ -3615,29 +3615,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  static Stri
@@ -3644,122 +3644,429 @@
 ing 
-viewname(final JSONArray path) %7B%0A        return viewname(path.getString(0), path.getString(2), path.getString(3));
+md5(final String str) %7B%0A        try %7B%0A            final MessageDigest md = MessageDigest.getInstance(%22MD5%22);%0A            return new BigInteger(1, md.digest(str.getBytes(%22UTF-8%22))).toString(16);%0A        %7D catch (final NoSuchAlgorithmException e) %7B%0A            throw new Error(%22MD5 support missing.%22);%0A        %7D catch (final UnsupportedEncodingException e) %7B%0A            throw new Error(%22UTF-8 support missing.%22);%0A        %7D
 %0A   

use dbname/md5(viewfunction) as index dir.