Browse code

[mapi] BM-14975 Feat: Implement extended buffers packing to speed up email and attachments downloads.

Thomas Cataldo authored on 02/07/2019 16:48:59
Showing 2 changed files
... ...
@@ -28,6 +28,7 @@ import java.util.HashSet;
28 28
 import java.util.LinkedList;
29 29
 import java.util.List;
30 30
 import java.util.Map;
31
+import java.util.Optional;
31 32
 import java.util.Set;
32 33
 import java.util.concurrent.ConcurrentHashMap;
33 34
 
... ...
@@ -340,9 +341,24 @@ public class ContainerStore extends JdbcAbstractStore {
340 341
 		delete("delete from t_container_location where container_uid  = ? ", new Object[] { container.uid });
341 342
 	}
342 343
 
343
-	public String getContainerLocation(String containerUid) throws SQLException {
344
-		return unique("select location from t_container_location where container_uid = ?", StringCreator.FIRST,
345
-				Collections.emptyList(), new Object[] { containerUid });
344
+	/**
345
+	 * Returns null if the container location is unknown, or an optional if the
346
+	 * location is known.
347
+	 * 
348
+	 * @param containerUid
349
+	 * @return
350
+	 * @throws SQLException
351
+	 */
352
+	public Optional<String> getContainerLocation(String containerUid) throws SQLException {
353
+		String ret = unique("select coalesce(location, 'DIR') from t_container_location where container_uid = ?",
354
+				StringCreator.FIRST, Collections.emptyList(), new Object[] { containerUid });
355
+		if (ret == null) {
356
+			return null;
357
+		} else if ("DIR".equals(ret)) {
358
+			return Optional.empty();
359
+		} else {
360
+			return Optional.of(ret);
361
+		}
346 362
 	}
347 363
 
348 364
 }
... ...
@@ -20,6 +20,7 @@ package net.bluemind.core.container.persistance;
20 20
 import java.sql.SQLException;
21 21
 import java.util.Collection;
22 22
 import java.util.LinkedHashSet;
23
+import java.util.Optional;
23 24
 import java.util.Set;
24 25
 
25 26
 import javax.sql.DataSource;
... ...
@@ -36,54 +37,46 @@ import net.bluemind.core.rest.BmContext;
36 37
 public class DataSourceRouter {
37 38
 
38 39
 	private static final Logger logger = LoggerFactory.getLogger(DataSourceRouter.class);
39
-	private static final Cache<String, String> cache = CacheBuilder.newBuilder().build();
40
+	private static final Cache<String, Optional<String>> cache = CacheBuilder.newBuilder().build();
40 41
 
41 42
 	public static DataSource get(BmContext context, String containerUid) {
42 43
 
43
-		String loc = cache.getIfPresent(containerUid);
44
+		Optional<String> loc = cache.getIfPresent(containerUid);
44 45
 		if (loc == null) {
45 46
 			ContainerStore directoryContainerStore = new ContainerStore(context, context.getDataSource(),
46 47
 					context.getSecurityContext());
47 48
 			try {
48 49
 				loc = directoryContainerStore.getContainerLocation(containerUid);
50
+				if (loc != null) {
51
+					cache.put(containerUid, loc);
52
+				}
49 53
 			} catch (SQLException e) {
50 54
 				throw ServerFault.sqlFault(e);
51 55
 			}
52
-			if (loc != null) {
53
-				cache.put(containerUid, loc);
54
-				if (logger.isDebugEnabled()) {
55
-					logger.debug("Fetch DataSource for container {}: {}", containerUid, loc);
56
-				}
57
-			}
58 56
 		}
59
-
60
-		if (loc != null) {
61
-			return context.getMailboxDataSource(loc);
57
+		if (logger.isDebugEnabled()) {
58
+			logger.debug("{} for {}", loc, containerUid);
62 59
 		}
63
-
64
-		return context.getDataSource();
60
+		return loc == null ? context.getDataSource()
61
+				: loc.map(l -> context.getMailboxDataSource(l)).orElse(context.getDataSource());
65 62
 	}
66 63
 
67 64
 	public static String location(BmContext context, String containerUid) {
68 65
 
69
-		String loc = cache.getIfPresent(containerUid);
66
+		Optional<String> loc = cache.getIfPresent(containerUid);
70 67
 		if (loc == null) {
71 68
 			ContainerStore directoryContainerStore = new ContainerStore(context, context.getDataSource(),
72 69
 					context.getSecurityContext());
73 70
 			try {
74 71
 				loc = directoryContainerStore.getContainerLocation(containerUid);
72
+				if (loc != null) {
73
+					cache.put(containerUid, loc);
74
+				}
75 75
 			} catch (SQLException e) {
76 76
 				throw ServerFault.sqlFault(e);
77 77
 			}
78
-			if (loc != null) {
79
-				cache.put(containerUid, loc);
80
-				if (logger.isDebugEnabled()) {
81
-					logger.debug("Fetch DataSource for container {}: {}", containerUid, loc);
82
-				}
83
-			}
84 78
 		}
85
-
86
-		return loc;
79
+		return loc == null ? null : loc.orElse(null);
87 80
 	}
88 81
 
89 82
 	public static Collection<DataSource> getAll(BmContext context) {