/*
 * Copyright (C) 2016 Datty.io Authors
 *
 * 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 io.datty.aerospike.executor;

import java.util.Map;
import java.util.Set;

import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.policy.QueryPolicy;

import io.datty.aerospike.AerospikeCache;
import io.datty.aerospike.AerospikeCacheManager;
import io.datty.aerospike.support.AerospikeValueUtil;
import io.datty.api.DattyRow;
import io.datty.api.operation.GetOperation;
import io.datty.api.result.GetResult;
import io.datty.support.LongVersion;
import rx.Single;
import rx.functions.Func1;

/**
 * AerospikeGet
 * 
 * @author Alex Shvid
 *
 */

public enum AerospikeGet implements AerospikeOperation<GetOperation, GetResult> {

	INSTANCE;
	
	@Override
	public Single<GetResult> execute(AerospikeCache cache, final GetOperation operation) {
		
		AerospikeCacheManager cacheManager = cache.getParent();
		QueryPolicy queryPolicy = cache.getConfig().getQueryPolicy(operation, false);
		Key recordKey = new Key(cacheManager.getConfig().getNamespace(), cache.getCacheName(), operation.getMajorKey());
		Set<String> minorKeys = operation.getMinorKeys();
		
		Single<Record> result;
		if (operation.isAllMinorKeys()) {
			 result = cacheManager.getClient().get(queryPolicy, recordKey, cache.singleExceptionTransformer(operation, false));
		}
		else if (minorKeys.isEmpty()) {
			 result = cacheManager.getClient().getHeader(queryPolicy, recordKey, cache.singleExceptionTransformer(operation, false));
		}		
		else {
			String[] binNames = minorKeys.toArray(new String[minorKeys.size()]);
			 result = cacheManager.getClient().get(queryPolicy, recordKey, binNames, cache.singleExceptionTransformer(operation, false));
		}
		
		return result.map(new Func1<Record, GetResult>() {

			@Override
			public GetResult call(Record rec) {
				return toGetResult(rec, operation);
			}
			
		});
		
	}
	
	private GetResult toGetResult(Record record, GetOperation operation) {
		
		GetResult result = new GetResult();
		
		if (record != null) {
			
			DattyRow row = new DattyRow();
			result.setRow(row);
			
			result.setVersion(new LongVersion(record.generation));

			if (operation.isAllMinorKeys()) {
				
				for (Map.Entry<String, Object> e : record.bins.entrySet()) {
					Object value = e.getValue();
					if (value != null) {
						row.putValue(e.getKey(), AerospikeValueUtil.toByteBuf(value));
					}
				}
				
			}
			else {
				
				for (String minorKey : operation.getMinorKeys()) {
					Object value = record.bins.get(minorKey);
					if (value != null) {
						row.putValue(minorKey, AerospikeValueUtil.toByteBuf(value));
					}
				}
				
			}
		
		}
		
		return result;
	}
	

}
@@ -2912,32 +2912,38 @@
 toByteBuf(value)
+, true
 );%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
@@ -3159,16 +3159,22 @@
 f(value)
+, true
 );%0A%09%09%09%09%09

update legacy code