Elffuss BraidHub

kikocis/java-driver public

08758edd9a11 · 7.7 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import java.util.Collections;
19import java.util.HashMap;
20import java.util.Map;
21import java.util.Set;
22
23import com.google.common.collect.Sets;
24
25import com.datastax.driver.core.ProtocolVersion;
26import com.datastax.driver.core.Session;
27import com.datastax.driver.core.TypeCodec;
28import com.datastax.driver.mapping.annotations.Accessor;
29import com.datastax.driver.mapping.annotations.Table;
30import com.datastax.driver.mapping.annotations.UDT;
31
32/**
33 * Mapping manager from which to obtain entity mappers.
34 */
35public class MappingManager {
36
37 private final Session session;
38 private final ProtocolVersion protocolVersion;
39 final boolean isCassandraV1;
40
41 private volatile Map<Class<?>, Mapper<?>> mappers = Collections.emptyMap();
42 private volatile Map<Class<?>, MappedUDTCodec<?>> udtCodecs = Collections.emptyMap();
43 private volatile Map<Class<?>, Object> accessors = Collections.emptyMap();
44
45 private final Set<Class<?>> registeredUDTs = Sets.newConcurrentHashSet();
46
47 /**
48 * Creates a new {@code MappingManager} using the provided {@code Session}.
49 * <p>
50 * Note that this constructor forces the initialization of the session.
51 *
52 * @param session the {@code Session} to use.
53 */
54 public MappingManager(Session session) {
55 this.session = session;
56 this.protocolVersion = getProtocolVersion(session);
57 // This is not strictly correct because we could connect to C* 2.0 with the v1 protocol.
58 // But mappers need to make a decision early so that generated queries are compatible, and we don't know in advance
59 // which nodes might join the cluster later.
60 // At least if protocol >=2 we know there won't be any 1.2 nodes ever.
61 this.isCassandraV1 = protocolVersion == ProtocolVersion.V1;
62 }
63
64 private static ProtocolVersion getProtocolVersion(Session session) {
65 session.init();
66 return session.getCluster().getConfiguration().getProtocolOptions().getProtocolVersion();
67 }
68
69 /**
70 * The underlying {@code Session} used by this manager.
71 * <p>
72 * Note that you can get obtain the {@code Cluster} object corresponding
73 * to that session using {@code getSession().getCluster()}.
74 * <p>
75 * It is inadvisable to close the returned Session while this manager and
76 * its mappers are in use.
77 *
78 * @return the underlying session used by this manager.
79 */
80 public Session getSession() {
81 return session;
82 }
83
84 /**
85 * Creates a {@code Mapper} for the provided class (that must be annotated by a
86 * {@link Table} annotation).
87 * <p>
88 * The {@code MappingManager} only ever keeps one Mapper for each class, and so calling this
89 * method multiple times on the same class will always return the same object.
90 * <p>
91 * If the type of any field in the class is an {@link UDT}-annotated classes, a codec for that
92 * class will automatically be created and registered with the underlying {@code Cluster}.
93 * This works recursively with UDTs nested in other UDTs or in collections.
94 *
95 * @param <T> the type of the class to map.
96 * @param klass the (annotated) class for which to return the mapper.
97 * @return the {@code Mapper} object for class {@code klass}.
98 */
99 public <T> Mapper<T> mapper(Class<T> klass) {
100 return getMapper(klass);
101 }
102
103 /**
104 * Creates a {@code TypeCodec} for the provided class (that must be annotated by
105 * a {@link UDT} annotation).
106 * <p>
107 * This method also registers the codec against the underlying {@code Cluster}.
108 * In addition, the codecs for any nested UDTs will also be created and registered.
109 * <p>
110 * You don't need to call this method explicitly if you already call {@link #mapper(Class)}
111 * for a class that references this UDT class (creating a mapper will automatically
112 * process all UDTs that it uses).
113 *
114 * @param <T> the type of the class to map.
115 * @param klass the (annotated) class for which to return the codec.
116 * @return the codec that maps the provided class to the corresponding user-defined type.
117 */
118 public <T> TypeCodec<T> udtCodec(Class<T> klass) {
119 return getUDTCodec(klass);
120 }
121
122 /**
123 * Creates an accessor object based on the provided interface (that must be annotated by
124 * a {@link Accessor} annotation).
125 * <p>
126 * The {@code MappingManager} only ever keep one Accessor for each class, and so calling this
127 * method multiple time on the same class will always return the same object.
128 *
129 * @param <T> the type of the accessor class.
130 * @param klass the (annotated) class for which to create an accessor object.
131 * @return the accessor object for class {@code klass}.
132 */
133 public <T> T createAccessor(Class<T> klass) {
134 return getAccessor(klass);
135 }
136
137 @SuppressWarnings("unchecked")
138 private <T> Mapper<T> getMapper(Class<T> klass) {
139 Mapper<T> mapper = (Mapper<T>)mappers.get(klass);
140 if (mapper == null) {
141 synchronized (mappers) {
142 mapper = (Mapper<T>)mappers.get(klass);
143 if (mapper == null) {
144 EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
145 mapper = new Mapper<T>(this, klass, entityMapper);
146 Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>(mappers);
147 newMappers.put(klass, mapper);
148 mappers = newMappers;
149 }
150 }
151 }
152 return mapper;
153 }
154
155 @SuppressWarnings("unchecked")
156 <T> TypeCodec<T> getUDTCodec(Class<T> mappedClass) {
157 MappedUDTCodec<T> codec = (MappedUDTCodec<T>)udtCodecs.get(mappedClass);
158 if (codec == null) {
159 synchronized (udtCodecs) {
160 codec = (MappedUDTCodec<T>)udtCodecs.get(mappedClass);
161 if (codec == null) {
162 codec = AnnotationParser.parseUDT(mappedClass, ReflectionMapper.factory(), this);
163 session.getCluster().getConfiguration().getCodecRegistry().register(codec);
164
165 HashMap<Class<?>, MappedUDTCodec<?>> newCodecs = new HashMap<Class<?>, MappedUDTCodec<?>>(udtCodecs);
166 newCodecs.put(mappedClass, codec);
167 udtCodecs = newCodecs;
168 }
169 }
170 }
171 return codec;
172 }
173
174 @SuppressWarnings("unchecked")
175 private <T> T getAccessor(Class<T> klass) {
176 T accessor = (T)accessors.get(klass);
177 if (accessor == null) {
178 synchronized (accessors) {
179 accessor = (T)accessors.get(klass);
180 if (accessor == null) {
181 AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
182 mapper.prepare(this);
183 accessor = mapper.createProxy();
184 Map<Class<?>, Object> newAccessors = new HashMap<Class<?>, Object>(accessors);
185 newAccessors.put(klass, accessor);
186 accessors = newAccessors;
187 }
188 }
189 }
190 return accessor;
191 }
192}