more flexible initialization of the mappingRepository

This commit is contained in:
Albert Shift 2015-04-09 20:56:22 -07:00
parent 4a309b10d0
commit 00a3e20a98
5 changed files with 46 additions and 24 deletions

View file

@ -112,21 +112,5 @@ public final class Casser {
public static <E> E map(Class<E> iface, Map<String, Object> src, ClassLoader classLoader) {
return settings.getMapperInstantiator().instantiate(iface, src, classLoader);
}
protected static MappingRepositoryBuilder createMappingRepository() {
MappingRepositoryBuilder builder = new MappingRepositoryBuilder();
for (Object dslProxy : dslCache.values()) {
if (dslProxy instanceof DslExportable) {
DslExportable e = (DslExportable) dslProxy;
builder.addEntity(e.getCasserMappingEntity());
}
}
return builder;
}
}

View file

@ -44,7 +44,7 @@ public class SessionInitializer extends AbstractSessionOperations {
private boolean showCql = false;
private Executor executor = MoreExecutors.sameThreadExecutor();
private MappingRepositoryBuilder mappingRepository;
private MappingRepositoryBuilder mappingRepository = new MappingRepositoryBuilder();
private boolean dropUnusedColumns = false;
private boolean dropUnusedIndexes = false;
@ -181,7 +181,6 @@ public class SessionInitializer extends AbstractSessionOperations {
Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator");
mappingRepository = Casser.createMappingRepository();
initList.forEach(dsl -> mappingRepository.add(dsl));
TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes);

View file

@ -16,5 +16,5 @@
package com.noorq.casser.mapping;
public enum CasserEntityType {
TABLE, USER_DEFINED_TYPE;
TABLE, TUPLE, USER_DEFINED_TYPE;
}

View file

@ -25,6 +25,9 @@ import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.noorq.casser.core.Casser;
import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.Either;
public final class MappingRepositoryBuilder {
@ -78,15 +81,22 @@ public final class MappingRepositoryBuilder {
public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) {
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity entity = entityMap.get(iface);
if (entity == null) {
entity = type.isPresent() ?
new CasserMappingEntity(iface, type.get()) :
new CasserMappingEntity(iface);
if (!(dsl instanceof DslExportable)) {
dsl = Casser.dsl(iface);
}
DslExportable e = (DslExportable) dsl;
entity = e.getCasserMappingEntity();
if (type.isPresent() && entity.getType() != type.get()) {
throw new CasserMappingException("unexpected entity type " + entity.getType() + " for " + entity);
}
CasserMappingEntity concurrentEntity = entityMap.putIfAbsent(iface, entity);
if (concurrentEntity == null) {

View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* 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.noorq.casser.mapping;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Tuple {
}