/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.datastore.index.mapping;

import static com.google.common.base.Preconditions.checkNotNull;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.LongField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;

import com.b2international.collections.LongCollection;
import com.b2international.collections.list.LongList;
import com.b2international.collections.set.LongSet;
import com.b2international.commons.pcj.PrimitiveCollections;

/**
 * @since 4.3
 */
public class LongIndexField extends IndexFieldBase<Long> implements LongCollectionIndexField {

	public LongIndexField(String fieldName) {
		this(fieldName, true);
	}
	
	public LongIndexField(String fieldName, boolean stored) {
		super(fieldName, stored);
	}

	@Override
	protected IndexableField toField(Long value) {
		return new LongField(fieldName(), value, isStored());
	}

	@Override
	protected BytesRef toBytesRef(Long value) {
		return _toBytesRef(value);
	}
	
	@Override
	protected Type getSortFieldType() {
		return Type.LONG;
	}
	
	@Override
	public Long getValue(IndexableField field) {
		final Number num = field.numericValue();
		checkNotNull(num, "Cannot get numeric value from field '%s'");
		return num.longValue();
	}
	
	@Override
	public final LongSet getValueAsLongSet(Document doc) {
		final IndexableField[] fields = getFields(doc);
		final LongSet longIds = PrimitiveCollections.newLongOpenHashSet(fields.length + 1);
		addIdsToLongCollection(fields, longIds);
		return longIds;
	}

	@Override
	public final LongList getValueAsLongList(Document doc) {
		final IndexableField[] fields = getFields(doc);
		final LongList longIds = PrimitiveCollections.newLongArrayList(fields.length + 1);
		addIdsToLongCollection(fields, longIds);
		return longIds;
	}

	private void addIdsToLongCollection(final IndexableField[] fields, final LongCollection longIds) {
		for (final IndexableField field : fields) {
			longIds.add(getValue(field));
		}
	}

	/**
	 * @param value
	 * @return
	 * @deprecated - if possible don't use this API, use {@link Mappings} or {@link #LongIndexField(String) constructor} instead
	 */
	public static BytesRef _toBytesRef(Long value) {
		final BytesRef bytesRef = new BytesRef();
		NumericUtils.longToPrefixCoded(value, 0, bytesRef);
		return bytesRef;
	}

}

@@ -747,16 +747,100 @@
 NotNull;
+%0Aimport static com.google.common.collect.Lists.newArrayList;%0A%0Aimport java.util.List;
 %0A%0Aimport
@@ -965,24 +965,110 @@
 xableField;%0A
+import org.apache.lucene.queries.TermsFilter;%0Aimport org.apache.lucene.search.Filter;%0A
 import org.a
@@ -1243,16 +1243,69 @@
 ection;%0A
+import com.b2international.collections.LongIterator;%0A
 import c
@@ -1441,32 +1441,214 @@
 ons.
-pcj.PrimitiveCollections
+CompareUtils;%0Aimport com.b2international.commons.collect.PrimitiveLists;%0Aimport com.b2international.commons.collect.PrimitiveSets;%0Aimport com.b2international.snowowl.datastore.index.lucene.MatchNoDocsFilter
 ;%0A%0A/
@@ -2554,34 +2554,27 @@
  = Primitive
-Collection
+Set
 s.newLongOpe
@@ -2822,26 +2822,20 @@
 rimitive
-Collection
+List
 s.newLon
@@ -2929,17 +2929,18 @@
 Ids;%0A%09%7D%0A
+%09
 %0A
-
 %09private
@@ -3077,16 +3077,16 @@
 elds) %7B%0A
-
 %09%09%09longI
@@ -3116,16 +3116,608 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09public final Filter createTermsFilter(LongCollection values) %7B%0A%09%09if (CompareUtils.isEmpty(values)) %7B%0A%09%09%09return new MatchNoDocsFilter(); %0A%09%09%7D else %7B%0A%09%09%09// Converted BytesRef values should be unique, but TermsFilter requires a writable list for sorting%0A%09%09%09final LongSet uniqueValues = PrimitiveSets.newLongOpenHashSet(values);%0A%0A%09%09%09final List%3CBytesRef%3E uniqueBytesRefs = newArrayList();%0A%09%09%09final LongIterator iter = uniqueValues.iterator();%0A%09%09%09while (iter.hasNext()) %7B%0A%09%09%09%09uniqueBytesRefs.add(toBytesRef(iter.next()));%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return new TermsFilter(fieldName(), uniqueBytesRefs);%0A%09%09%7D%0A%09%7D
 %0A%0A%09/**%0A%09

add createTermsFilter(LongCollection) method to LongIndexField