<commit_before>/*
   Copyright 2007 Fourspaces Consulting, LLC
   Copyright 2015 The ARSnova Team

   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.fourspaces.couchdb;

import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * The View is the mechanism for performing Querys on a CouchDB instance. The view can be named or ad-hoc (see
 * AdHocView). (Currently [14 Sept 2007] named view aren't working in the mainline CouchDB code... but this _should_
 * work.)
 * <p>
 * The View object exists mainly to apply filtering to the view. Otherwise, views can be called directly from the
 * database object by using their names (or given an ad-hoc query).
 *
 * @author mbreese
 *
 */
public class View {

	public enum StaleMode {
		NONE, OK, UPDATE_AFTER
	}

	protected String key;
	protected String keys;
	protected String startKey;
	protected String endKey;
	protected Integer limit;
	protected Boolean update;
	protected Boolean reverse;
	protected int skip;
	protected Boolean group;
	protected boolean includeDocs = false;
	protected StaleMode stale = StaleMode.NONE;

	protected String name;
	protected Document document;
	protected String function;

	/**
	 * Build a view given a document and a name
	 *
	 * @param doc
	 * @param name
	 */
	public View(Document doc, String name) {
		this.document = doc;
		this.name = name;
	}

	/**
	 * Build a view given only a fullname ex: ("_add_docs", "_temp_view")
	 *
	 * @param fullname
	 */
	public View(String fullname) {
		this.name = fullname;
		this.document = null;
	}

	/**
	 * Builds a new view for a document, a given name, and the function definition. This <i>does not actually add it to
	 * the document</i>. That is handled by Document.addView()
	 * <p>
	 * This constructor should only be called by Document.addView();
	 *
	 * @param doc
	 * @param name
	 * @param function
	 */
	View(Document doc, String name, String function) {
		this.name = name;
		this.document = doc;
		this.function = function;
	}

	/**
	 * Based upon settings, builds the queryString to add to the URL for this view.
	 *
	 *
	 * @return
	 */
	public String getQueryString() {
		String queryString = "";
		if (key != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "key=" + key;
		}
		if (startKey != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "startkey=" + startKey;
		}
		if (endKey != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "endkey=" + endKey;
		}
		if (skip != 0) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "skip=" + skip;
		}
		if (limit != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "limit=" + limit;
		}
		if (update != null && update.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "update=true";
		}
		if (includeDocs != false) {
			if (queryString.length() > 0) {
				queryString += "&";
			}
			queryString += "include_docs=true";
		}
		if (reverse != null && reverse.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "descending=true";
		}
		if (group != null && group.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "group=true";
		}
		if (keys != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "keys=" + keys;
		}
		if (stale != null && stale != StaleMode.NONE) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			if (stale == StaleMode.OK) {
				queryString += "stale=ok";
			} else if (stale == StaleMode.UPDATE_AFTER) {
				queryString += "stale=update_after";
			}
		}
		return queryString.equals("") ? null : queryString;

	}

	/**
	 * The number of entries to return
	 *
	 * @param count
	 * @deprecated CouchDB 0.9 uses limit instead
	 */
	@Deprecated
	public void setCount(Integer count) {
		// this.count = count;
		setLimit(count);
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}
<commit_msg>Add support for enabling/disabling reduce for views<commit_after>/*
   Copyright 2007 Fourspaces Consulting, LLC
   Copyright 2015 The ARSnova Team

   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.fourspaces.couchdb;

import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * The View is the mechanism for performing Querys on a CouchDB instance. The view can be named or ad-hoc (see
 * AdHocView). (Currently [14 Sept 2007] named view aren't working in the mainline CouchDB code... but this _should_
 * work.)
 * <p>
 * The View object exists mainly to apply filtering to the view. Otherwise, views can be called directly from the
 * database object by using their names (or given an ad-hoc query).
 *
 * @author mbreese
 *
 */
public class View {

	public enum StaleMode {
		NONE, OK, UPDATE_AFTER
	}

	protected String key;
	protected String keys;
	protected String startKey;
	protected String endKey;
	protected Integer limit;
	protected Boolean update;
	protected Boolean reverse;
	protected int skip;
	protected boolean reduce = false;
	protected Boolean group;
	protected boolean includeDocs = false;
	protected StaleMode stale = StaleMode.NONE;

	protected String name;
	protected Document document;
	protected String function;

	/**
	 * Build a view given a document and a name
	 *
	 * @param doc
	 * @param name
	 */
	public View(Document doc, String name) {
		this.document = doc;
		this.name = name;
	}

	/**
	 * Build a view given only a fullname ex: ("_add_docs", "_temp_view")
	 *
	 * @param fullname
	 */
	public View(String fullname) {
		this.name = fullname;
		this.document = null;
	}

	/**
	 * Builds a new view for a document, a given name, and the function definition. This <i>does not actually add it to
	 * the document</i>. That is handled by Document.addView()
	 * <p>
	 * This constructor should only be called by Document.addView();
	 *
	 * @param doc
	 * @param name
	 * @param function
	 */
	View(Document doc, String name, String function) {
		this.name = name;
		this.document = doc;
		this.function = function;
	}

	/**
	 * Based upon settings, builds the queryString to add to the URL for this view.
	 *
	 *
	 * @return
	 */
	public String getQueryString() {
		String queryString = "";
		if (key != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "key=" + key;
		}
		if (startKey != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "startkey=" + startKey;
		}
		if (endKey != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "endkey=" + endKey;
		}
		if (skip != 0) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "skip=" + skip;
		}
		if (limit != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "limit=" + limit;
		}
		if (update != null && update.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "update=true";
		}
		if (includeDocs != false) {
			if (queryString.length() > 0) {
				queryString += "&";
			}
			queryString += "include_docs=true";
		}
		if (reverse != null && reverse.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "descending=true";
		}
		if (group != null && group.booleanValue()) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "group=true";
		} else if (!reduce) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "reduce=false";
		}
		if (keys != null) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			queryString += "keys=" + keys;
		}
		if (stale != null && stale != StaleMode.NONE) {
			if (!queryString.equals("")) {
				queryString += "&";
			}
			if (stale == StaleMode.OK) {
				queryString += "stale=ok";
			} else if (stale == StaleMode.UPDATE_AFTER) {
				queryString += "stale=update_after";
			}
		}
		return queryString.equals("") ? null : queryString;

	}

	/**
	 * The number of entries to return
	 *
	 * @param count
	 * @deprecated CouchDB 0.9 uses limit instead
	 */
	@Deprecated
	public void setCount(Integer count) {
		// this.count = count;
		setLimit(count);
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setReduce(boolean reduce) {
		this.reduce = reduce;
	}

{'fraction_non_alphanumeric': 0.13140992965643797, 'fraction_numerical': 0.00448567641961464, 'mean_word_length': 3.1923076923076925, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '1356792', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3033, 'n_words': 992}