<commit_before>/*
 * Copyright 2002-2014 the original author or 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 org.springframework.data.crate.core.mapping.schema;

import static java.lang.Boolean.TRUE;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.data.crate.core.CyclicReferenceBarrier.cyclicReferenceBarrier;
import static org.springframework.data.crate.core.mapping.CrateSimpleTypes.HOLDER;
import static org.springframework.data.util.ClassTypeInformation.from;
import static org.springframework.util.Assert.notNull;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.springframework.data.crate.InvalidCrateApiUsageException;
import org.springframework.data.crate.core.CyclicReferenceBarrier;
import org.springframework.data.crate.core.mapping.CratePersistentEntity;
import org.springframework.data.crate.core.mapping.CratePersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.TypeInformation;

/**
 * 
 * @author <NAME>
 * @since 1.0.0
 * 
 * Creates table definitions for persistent entities.
 * Creates columns from entity properties and maps them from java type to crate data type.
 * Nested arrays/collections of arrays/collections are not currently supported by crate db
 */
class EntityTableMapper {
	
	private final Logger logger = getLogger(getClass());
	
	private final PrimitiveColumnMapper primitiveTypeMapper;
	private final PrimitiveCollectionTypeColumnMapper primitiveCollectionTypeMapper;
	private final MapTypeColumnMapper mapTypeMapper;
	
	private final MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext;
	
	private EntityTableMapper(MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext) {
		this.mappingContext = mappingContext;
		this.primitiveTypeMapper = new PrimitiveColumnMapper();
		this.primitiveCollectionTypeMapper = new PrimitiveCollectionTypeColumnMapper();
		this.mapTypeMapper = new MapTypeColumnMapper();
	}
	
	public static EntityTableMapper entityTableMapper(MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext) {
		return new EntityTableMapper(mappingContext);
	}
	
	/**
	 * @param entity entity object, must not be {@literal null}. 
	 * @return table definition containing table name and columns
	 */
	public TableDefinition createDefinition(CratePersistentEntity<?> entity) {
		
		List<Column> columns = new LinkedList<Column>();
		
		mapColumns(entity, columns, cyclicReferenceBarrier());
		
		return new TableDefinition(entity.getTableName(), columns);
	}
	
	/**
	 * Recursively crawls over a nested object's fields
	 * @param root entity object, must not be {@literal null}.
	 * @param columns list of columns fot root entity object, must not be {@literal null}.<commit_msg>added method to return list of columns for a given persistent entity<commit_after>/*
 * Copyright 2002-2014 the original author or 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 org.springframework.data.crate.core.mapping.schema;

import static java.lang.Boolean.TRUE;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.data.crate.core.CyclicReferenceBarrier.cyclicReferenceBarrier;
import static org.springframework.data.crate.core.mapping.CrateSimpleTypes.HOLDER;
import static org.springframework.data.util.ClassTypeInformation.from;
import static org.springframework.util.Assert.notNull;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.springframework.data.crate.InvalidCrateApiUsageException;
import org.springframework.data.crate.core.CyclicReferenceBarrier;
import org.springframework.data.crate.core.mapping.CratePersistentEntity;
import org.springframework.data.crate.core.mapping.CratePersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.TypeInformation;

/**
 * 
 * @author <NAME>
 * @since 1.0.0
 * 
 * Creates table definitions for persistent entities.
 * Creates columns from entity properties and maps them from java type to crate data type.
 * Nested arrays/collections of arrays/collections are not currently supported by crate db
 */
class EntityTableMapper {
	
	private final Logger logger = getLogger(getClass());
	
	private final PrimitiveColumnMapper primitiveTypeMapper;
	private final PrimitiveCollectionTypeColumnMapper primitiveCollectionTypeMapper;
	private final MapTypeColumnMapper mapTypeMapper;
	
	private final MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext;
	
	private EntityTableMapper(MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext) {
		this.mappingContext = mappingContext;
		this.primitiveTypeMapper = new PrimitiveColumnMapper();
		this.primitiveCollectionTypeMapper = new PrimitiveCollectionTypeColumnMapper();
		this.mapTypeMapper = new MapTypeColumnMapper();
	}
	
	public static EntityTableMapper entityTableMapper(MappingContext<? extends CratePersistentEntity<?>, CratePersistentProperty> mappingContext) {
		return new EntityTableMapper(mappingContext);
	}
	
	/**
	 * @param entity entity object, must not be {@literal null}. 
	 * @return table definition containing table name and columns
	 */
	public TableDefinition createDefinition(CratePersistentEntity<?> entity) {
		
		List<Column> columns = createColumns(entity);
		
		return new TableDefinition(entity.getTableName(), columns);
	}
	
	/**
	 * @param entity entity object, must not be {@literal null}. 
	 * @return list of columns
	 */
	public List<Column> createColumns(CratePersistentEntity<?> entity) {
		
		List<Column> columns = new LinkedList<Column>();
		
		mapColumns(entity, columns, cyclicReferenceBarrier());
		
		return columns;
	}
	
	/**
	 * Recursively crawls over a nested object's fields
	 * @param root entity object, must not be {@literal null}.
	 * @param columns list of columns fot root entity object, must not be {@literal null}.
{'fraction_non_alphanumeric': 0.08256754903305445, 'fraction_numerical': 0.0046632835002057334, 'mean_word_length': 5.430335097001763, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '6082400', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1837, 'n_words': 596}