Elffuss BraidHub

kikocis/java-driver public

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