Elffuss BraidHub

kikocis/java-driver public

9e8f5a07be4b · 8.4 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;
21
22import com.datastax.driver.core.ProtocolVersion;
23import com.datastax.driver.core.Session;
24import com.datastax.driver.core.TypeCodec;
25import com.datastax.driver.mapping.annotations.Accessor;
26import com.datastax.driver.mapping.annotations.Table;
27import com.datastax.driver.mapping.annotations.UDT;
28
29/**
30 * Mapping manager from which to obtain entity mappers.
31 */
32public class MappingManager {
33
34 private final Session session;
35 final boolean isCassandraV1;
36
37 private volatile Map<Class<?>, Mapper<?>> mappers = Collections.emptyMap();
38 private volatile Map<Class<?>, MappedUDTCodec<?>> udtCodecs = Collections.emptyMap();
39 private volatile Map<Class<?>, Object> accessors = Collections.emptyMap();
40
41 /**
42 * Creates a new {@code MappingManager} using the provided {@code Session}.
43 * <p>
44 * Note that this constructor forces the initialization of the session (see
45 * {@link #MappingManager(Session, ProtocolVersion)} if that is a problem for you).
46 *
47 * @param session the {@code Session} to use.
48 */
49 public MappingManager(Session session) {
50 this(session, getProtocolVersion(session));
51 }
52
53 private static ProtocolVersion getProtocolVersion(Session session) {
54 session.init();
55 return session.getCluster().getConfiguration().getProtocolOptions().getProtocolVersion();
56 }
57
58 /**
59 * Creates a new {@code MappingManager} using the provided {@code Session}.
60 * <p>
61 * This constructor is only provided for backward compatibility: before 2.1.7, {@code MappingManager} could be
62 * built from an uninitialized session; since 2.1.7, the mapper needs to know the active protocol version to
63 * adapt its internal requests, so {@link #MappingManager(Session)} will now initialize the session if needed.
64 * If you rely on the session not being initialized, use this constructor and provide the version manually.
65 *
66 * @param session the {@code Session} to use.
67 * @param protocolVersion the protocol version that will be used with this session.
68 *
69 * @since 2.1.7
70 */
71 public MappingManager(Session session, ProtocolVersion protocolVersion) {
72 this.session = session;
73 // This is not strictly correct because we could connect to C* 2.0 with the v1 protocol.
74 // But mappers need to make a decision early so that generated queries are compatible, and we don't know in advance
75 // which nodes might join the cluster later.
76 // At least if protocol >=2 we know there won't be any 1.2 nodes ever.
77 this.isCassandraV1 = (protocolVersion == ProtocolVersion.V1);
78 }
79
80 /**
81 * The underlying {@code Session} used by this manager.
82 * <p>
83 * Note that you can get obtain the {@code Cluster} object corresponding
84 * to that session using {@code getSession().getCluster()}.
85 * <p>
86 * It is inadvisable to close the returned Session while this manager and
87 * its mappers are in use.
88 *
89 * @return the underlying session used by this manager.
90 */
91 public Session getSession() {
92 return session;
93 }
94
95 /**
96 * Creates a {@code Mapper} for the provided class (that must be annotated by a
97 * {@link Table} annotation).
98 * <p>
99 * The {@code MappingManager} only ever keeps one Mapper for each class, and so calling this
100 * method multiple times on the same class will always return the same object.
101 * <p>
102 * If the type of any field in the class is an {@link UDT}-annotated classes, a codec for that
103 * class will automatically be created and registered with the underlying {@code Cluster}.
104 * This works recursively with UDTs nested in other UDTs or in collections.
105 *
106 * @param <T> the type of the class to map.
107 * @param klass the (annotated) class for which to return the mapper.
108 * @return the {@code Mapper} object for class {@code klass}.
109 */
110 public <T> Mapper<T> mapper(Class<T> klass) {
111 return getMapper(klass);
112 }
113
114 /**
115 * Creates a {@code TypeCodec} for the provided class (that must be annotated by
116 * a {@link UDT} annotation).
117 * <p>
118 * This method also registers the codec against the underlying {@code Cluster}.
119 * In addition, the codecs for any nested UDTs will also be created and registered.
120 * <p>
121 * You don't need to call this method explicitly if you already call {@link #mapper(Class)}
122 * for a class that references this UDT class (creating a mapper will automatically
123 * process all UDTs that it uses).
124 *
125 * @param <T> the type of the class to map.
126 * @param klass the (annotated) class for which to return the codec.
127 * @return the codec that maps the provided class to the corresponding user-defined type.
128 */
129 public <T> TypeCodec<T> udtCodec(Class<T> klass) {
130 return getUDTCodec(klass);
131 }
132
133 /**
134 * Creates an accessor object based on the provided interface (that must be annotated by
135 * a {@link Accessor} annotation).
136 * <p>
137 * The {@code MappingManager} only ever keep one Accessor for each class, and so calling this
138 * method multiple time on the same class will always return the same object.
139 *
140 * @param <T> the type of the accessor class.
141 * @param klass the (annotated) class for which to create an accessor object.
142 * @return the accessor object for class {@code klass}.
143 */
144 public <T> T createAccessor(Class<T> klass) {
145 return getAccessor(klass);
146 }
147
148 @SuppressWarnings("unchecked")
149 private <T> Mapper<T> getMapper(Class<T> klass) {
150 Mapper<T> mapper = (Mapper<T>)mappers.get(klass);
151 if (mapper == null) {
152 synchronized (mappers) {
153 mapper = (Mapper<T>)mappers.get(klass);
154 if (mapper == null) {
155 EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
156 mapper = new Mapper<T>(this, klass, entityMapper);
157 Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>(mappers);
158 newMappers.put(klass, mapper);
159 mappers = newMappers;
160 }
161 }
162 }
163 return mapper;
164 }
165
166 @SuppressWarnings("unchecked")
167 <T> TypeCodec<T> getUDTCodec(Class<T> mappedClass) {
168 MappedUDTCodec<T> codec = (MappedUDTCodec<T>)udtCodecs.get(mappedClass);
169 if (codec == null) {
170 synchronized (udtCodecs) {
171 codec = (MappedUDTCodec<T>)udtCodecs.get(mappedClass);
172 if (codec == null) {
173 codec = AnnotationParser.parseUDT(mappedClass, ReflectionMapper.factory(), this);
174 session.getCluster().getConfiguration().getCodecRegistry().register(codec);
175
176 HashMap<Class<?>, MappedUDTCodec<?>> newCodecs = new HashMap<Class<?>, MappedUDTCodec<?>>(udtCodecs);
177 newCodecs.put(mappedClass, codec);
178 udtCodecs = newCodecs;
179 }
180 }
181 }
182 return codec;
183 }
184
185 @SuppressWarnings("unchecked")
186 private <T> T getAccessor(Class<T> klass) {
187 T accessor = (T)accessors.get(klass);
188 if (accessor == null) {
189 synchronized (accessors) {
190 accessor = (T)accessors.get(klass);
191 if (accessor == null) {
192 AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
193 mapper.prepare(this);
194 accessor = mapper.createProxy();
195 Map<Class<?>, Object> newAccessors = new HashMap<Class<?>, Object>(accessors);
196 newAccessors.put(klass, accessor);
197 accessors = newAccessors;
198 }
199 }
200 }
201 return accessor;
202 }
203}