<commit_before>/*
 * Copyright 1999-2011 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * 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.orientechnologies.orient.core.type.tree.provider;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.profiler.OProfiler;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.record.impl.ORecordBytesLazy;
import com.orientechnologies.orient.core.serialization.OMemoryStream;
import com.orientechnologies.orient.core.serialization.OSerializableStream;
import com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializer;
import com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory;
import com.orientechnologies.orient.core.serialization.serializer.stream.OStreamSerializer;
import com.orientechnologies.orient.core.serialization.serializer.stream.OStreamSerializerFactory;
import com.orientechnologies.orient.core.storage.OStorage;

public class OMVRBTreeMapProvider<K, V> extends OMVRBTreeProviderAbstract<K, V> {
	private static final long			serialVersionUID					= 1L;

	public final static byte			CURRENT_PROTOCOL_VERSION	= 3;

	protected final OMemoryStream	stream;
	protected OBinarySerializer<K> keySerializer;
	protected OStreamSerializer  	valueSerializer;
	protected boolean							keepKeysInMemory;
	protected boolean							keepValuesInMemory;

	public OMVRBTreeMapProvider(final OStorage iStorage, final String iClusterName, final ORID iRID) {
		this(iStorage, iClusterName, null, null);
		record.setIdentity(iRID.getClusterId(), iRID.getClusterPosition());
	}

	public OMVRBTreeMapProvider(final OStorage iStorage, final String iClusterName, final OBinarySerializer<K> iKeySerializer,
			final OStreamSerializer iValueSerializer) {
		super(new ORecordBytesLazy(), iStorage, iClusterName);
		((ORecordBytesLazy) record).recycle(this);
		stream = new OMemoryStream();
		keySerializer = iKeySerializer;
		valueSerializer = iValueSerializer;
	}

	public OMVRBTreeEntryDataProvider<K, V> getEntry(final ORID iRid) {
		return new OMVRBTreeMapEntryProvider<K, V>(this, iRid);
	}

	public OMVRBTreeEntryDataProvider<K, V> createEntry() {
		return new OMVRBTreeMapEntryProvider<K, V>(this);
	}

	@Override
	protected void load(final ODatabaseRecord iDb) {
		((ORecordBytesLazy) record).recycle(this);
		super.load(iDb);
	}

	@Override
	protected void load(final OStorage iSt) {
		((ORecordBytesLazy) record).recycle(this);
		super.load(iSt);
	}

	public boolean updateConfig() {
		final boolean changed = super.updateConfig();
		keepKeysInMemory = OGlobalConfiguration.MVRBTREE_ENTRY_KEYS_IN_MEMORY.getValueAsBoolean();
		keepValuesInMemory = OGlobalConfiguration.MVRBTREE_ENTRY_VALUES_IN_MEMORY.getValueAsBoolean();
		return changed;
	}

	public byte[] toStream() throws OSerializationException {
		final long timer = OProfiler.getInstance().startChrono();

		try {
			stream.jump(0);
			stream.set(CURRENT_PROTOCOL_VERSION);
			stream.setAsFixed(root != null ? root.toStream() : ORecordId.EMPTY_RECORD_ID_STREAM);

			stream.set(size);
			stream.set(pageSize);
			stream.set(keySize);

			stream.set(keySerializer.getId());
			stream.set(valueSerializer.getName());

			final byte[] result = stream.toByteArray();
			record.fromStream(result);
			return result;

		} finally {
			OProfiler.getInstance().stopChrono("OMVRBTreeMapProvider.toStream", timer);
		}
	}

	public OSerializableStream fromStream(final byte[] iStream) throws OSerializationException {
		final long timer = OProfiler.getInstance().startChrono();
		try {
			stream.setSource(iStream);
			byte protocolVersion = stream.peek();
			if (protocolVersion != -1) {
				// @COMPATIBILITY BEFORE 0.9.25
				stream.getAsByte();
				if (protocolVersion != CURRENT_PROTOCOL_VERSION)
					throw new OSerializationException(
									"The index has been created with a previous version of OrientDB. To use it with this version of OrientDB you need to export and import your database. "
													+ protocolVersion + "<->" + CURRENT_PROTOCOL_VERSION);
			}

			root = new ORecordId();
			root.fromStream(stream.getAsByteArrayFixed(ORecordId.PERSISTENT_SIZE));

			size = stream.getAsInteger();
			if (protocolVersion == -1)
				// @COMPATIBILITY BEFORE 0.9.25
				pageSize = stream.getAsShort();
			else
				pageSize = stream.getAsInteger();

			if (protocolVersion < 1) {
				keySize = 1;
				OLogManager.instance().warn(this,
						"Previous index version was found, " + " partial composite index queries may do not work if you do not rebuild index.");
			} else
				keySize = stream.getAsInteger();

			keySerializer = OBinarySerializerFactory.INSTANCE.getObjectSerializer(stream.getAsByte());
			valueSerializer = OStreamSerializerFactory.get(stream.getAsString());

		} catch (Exception e) {
			OLogManager.instance().error(this, "Error on unmarshalling OMVRBTreeMapProvider object from record: %s", e,
					OSerializationException.class, root);
		} finally {
			OProfiler.getInstance().stopChrono("OMVRBTreeMapProvider.fromStream", timer);
		}
		return this;
	}
}<commit_msg>Unneeded version check was removed.<commit_after>/*
 * Copyright 1999-2011 <NAME> (l.<EMAIL>--at--orient<EMAIL>)
 *
 * 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.orientechnologies.orient.core.type.tree.provider;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.profiler.OProfiler;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.record.impl.ORecordBytesLazy;
import com.orientechnologies.orient.core.serialization.OMemoryStream;
import com.orientechnologies.orient.core.serialization.OSerializableStream;
import com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializer;
import com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory;
import com.orientechnologies.orient.core.serialization.serializer.stream.OStreamSerializer;
import com.orientechnologies.orient.core.serialization.serializer.stream.OStreamSerializerFactory;
import com.orientechnologies.orient.core.storage.OStorage;

public class OMVRBTreeMapProvider<K, V> extends OMVRBTreeProviderAbstract<K, V> {
	private static final long			serialVersionUID					= 1L;

	public final static byte			CURRENT_PROTOCOL_VERSION	= 3;

	protected final OMemoryStream	stream;
	protected OBinarySerializer<K> keySerializer;
	protected OStreamSerializer  	valueSerializer;
	protected boolean							keepKeysInMemory;
	protected boolean							keepValuesInMemory;

	public OMVRBTreeMapProvider(final OStorage iStorage, final String iClusterName, final ORID iRID) {
		this(iStorage, iClusterName, null, null);
		record.setIdentity(iRID.getClusterId(), iRID.getClusterPosition());
	}

	public OMVRBTreeMapProvider(final OStorage iStorage, final String iClusterName, final OBinarySerializer<K> iKeySerializer,
			final OStreamSerializer iValueSerializer) {
		super(new ORecordBytesLazy(), iStorage, iClusterName);
		((ORecordBytesLazy) record).recycle(this);
		stream = new OMemoryStream();
		keySerializer = iKeySerializer;
		valueSerializer = iValueSerializer;
	}

	public OMVRBTreeEntryDataProvider<K, V> getEntry(final ORID iRid) {
		return new OMVRBTreeMapEntryProvider<K, V>(this, iRid);
	}

	public OMVRBTreeEntryDataProvider<K, V> createEntry() {
		return new OMVRBTreeMapEntryProvider<K, V>(this);
	}

	@Override
	protected void load(final ODatabaseRecord iDb) {
		((ORecordBytesLazy) record).recycle(this);
		super.load(iDb);
	}

	@Override
	protected void load(final OStorage iSt) {
		((ORecordBytesLazy) record).recycle(this);
		super.load(iSt);
	}

	public boolean updateConfig() {
		final boolean changed = super.updateConfig();
		keepKeysInMemory = OGlobalConfiguration.MVRBTREE_ENTRY_KEYS_IN_MEMORY.getValueAsBoolean();
		keepValuesInMemory = OGlobalConfiguration.MVRBTREE_ENTRY_VALUES_IN_MEMORY.getValueAsBoolean();
		return changed;
	}

	public byte[] toStream() throws OSerializationException {
		final long timer = OProfiler.getInstance().startChrono();

		try {
			stream.jump(0);
			stream.set(CURRENT_PROTOCOL_VERSION);
			stream.setAsFixed(root != null ? root.toStream() : ORecordId.EMPTY_RECORD_ID_STREAM);

			stream.set(size);
			stream.set(pageSize);
			stream.set(keySize);

			stream.set(keySerializer.getId());
			stream.set(valueSerializer.getName());

			final byte[] result = stream.toByteArray();
			record.fromStream(result);
			return result;

		} finally {
			OProfiler.getInstance().stopChrono("OMVRBTreeMapProvider.toStream", timer);
		}
	}

	public OSerializableStream fromStream(final byte[] iStream) throws OSerializationException {
		final long timer = OProfiler.getInstance().startChrono();
		try {
			stream.setSource(iStream);
			byte protocolVersion = stream.peek();
			if (protocolVersion != -1) {
				// @COMPATIBILITY BEFORE 0.9.25
				stream.getAsByte();
				if (protocolVersion != CURRENT_PROTOCOL_VERSION)
					throw new OSerializationException(
									"The index has been created with a previous version of OrientDB. To use it with this version of OrientDB you need to export and import your database. "
													+ protocolVersion + "<->" + CURRENT_PROTOCOL_VERSION);
			}

			root = new ORecordId();
			root.fromStream(stream.getAsByteArrayFixed(ORecordId.PERSISTENT_SIZE));

			size = stream.getAsInteger();
			if (protocolVersion == -1)
				// @COMPATIBILITY BEFORE 0.9.25
				pageSize = stream.getAsShort();
			else
				pageSize = stream.getAsInteger();

			keySize = stream.getAsInteger();

			keySerializer = OBinarySerializerFactory.INSTANCE.getObjectSerializer(stream.getAsByte());
			valueSerializer = OStreamSerializerFactory.get(stream.getAsString());

		} catch (Exception e) {
			OLogManager.instance().error(this, "Error on unmarshalling OMVRBTreeMapProvider object from record: %s", e,
					OSerializationException.class, root);
		} finally {
			OProfiler.getInstance().stopChrono("OMVRBTreeMapProvider.fromStream", timer);
		}
		return this;
	}
}
{'fraction_non_alphanumeric': 0.09818275772973904, 'fraction_numerical': 0.00447851175609336, 'mean_word_length': 5.1700318809776835, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, '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': '1469537', 'n_tokens_mistral': 3848, 'n_tokens_neox': 3482, 'n_words': 778}