/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Jeff Nelson, Cinchapi Software Collective
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.cinchapi.concourse.server.engine;

import java.nio.ByteBuffer;

import org.cinchapi.common.annotate.PackagePrivate;
import org.cinchapi.common.cache.ReferenceCache;
import org.cinchapi.common.io.ByteBufferOutputStream;
import org.cinchapi.common.io.ByteBuffers;
import org.cinchapi.concourse.thrift.TObject;
import org.cinchapi.concourse.thrift.Type;

/**
 * A collection of utility methods that are used in the {@link Value} class.
 * 
 * @author jnelson
 */
@PackagePrivate
abstract class Values {

	/**
	 * Encode {@code quantity} and {@code timestamp} into a ByteBuffer that
	 * conforms to the format specified for {@link Value#getBytes()}.
	 * 
	 * @param quantity
	 * @param timestamp
	 * @return the ByteBuffer encoding
	 */
	public static ByteBuffer encodeAsByteBuffer(TObject quantity, long timestamp) {
		ByteBufferOutputStream out = new ByteBufferOutputStream();
		out.write(timestamp);
		out.write(quantity.getType().ordinal());
		out.write(quantity.bufferForData());
		ByteBuffer bytes = out.toByteBuffer();
		out.close();
		return bytes;
	}

	/**
	 * Get an object of {@code type} from {@code buffer}. This method will read
	 * starting from the current position up until enough bytes for {@code type}
	 * have been read. If {@code type} equals {@link Type#STRING}, all of the
	 * remaining bytes in the buffer will be read.
	 * 
	 * @param buffer
	 * @param type
	 * @return the object.
	 */
	static TObject getQuantityFromByteBuffer(ByteBuffer buffer, Type type) {
		Object[] cacheKey = { ByteBuffers.encodeAsHexString(buffer), type };
		TObject object = quantityCache.get(cacheKey);
		if(object == null) {
			// Must allocate a heap buffer because TObject assumes it has a
			// backing array.
			object = new TObject(ByteBuffer.allocate(buffer.remaining()).put(
					buffer), type);
			quantityCache.put(object, cacheKey);
		}
		return object;
	}

	/**
	 * Maintains a cache of all the quantities that are extracted from
	 * ByteBuffers in the {@link #getQuantityFromByteBuffer(ByteBuffer, Type)}
	 * method.
	 */
	private static final ReferenceCache<TObject> quantityCache = new ReferenceCache<TObject>();

}

@@ -1330,125 +1330,33 @@
 mon.
-cache.ReferenceCache;%0Aimport org.cinchapi.common.io.ByteBufferOutputStream;%0Aimport org.cinchapi.common.io.ByteBuffers
+io.ByteBufferOutputStream
 ;%0Aim
@@ -2577,151 +2577,8 @@
 ) %7B%0A
-%09%09Object%5B%5D cacheKey = %7B ByteBuffers.encodeAsHexString(buffer), type %7D;%0A%09%09TObject object = quantityCache.get(cacheKey);%0A%09%09if(object == null) %7B%0A%09
 %09%09//
@@ -2641,17 +2641,16 @@
 has a%0A%09%09
-%09
 // backi
@@ -2665,17 +2665,14 @@
 .%0A%09%09
-%09object =
+return
  new
@@ -2728,22 +2728,16 @@
 put(
-%0A%09%09%09%09%09
 buffer),
  typ
@@ -2736,339 +2736,22 @@
 er),
- type);%0A%09%09%09quantityCache.put(object, cacheKey);%0A%09%09%7D%0A%09%09return object;%0A%09%7D%0A%0A%09/**%0A%09 * Maintains a cache of all the quantities that are extracted from%0A%09 * ByteBuffers in the %7B@link #getQuantityFromByteBuffer(ByteBuffer, Type)%7D%0A%09 * method.%0A%09 */%0A%09private static final ReferenceCache%3CTObject%3E quantityCache = new ReferenceCache%3CTObject%3E();
+%0A%09%09%09%09type);%0A%09%7D
 %0A%0A%7D%0A

Remove reference caching from Value quantities.