DownloadQueue .java

net.minecraft.server.packs.DownloadQueue (class): members: DownloadRequest, MAX_KEPT_PACKS, FileInfoEntry, downloadBatch, BatchConfig, BatchResult, tasks.

net.minecraft.server.packsclass139 linesserver5.1 KB

Implements

API Surface (5)

CompletableFuture<DownloadQueue.BatchResult> downloadBatch(final DownloadQueue.BatchConfig config, final Map<UUID, DownloadQueue.DownloadRequest> requests)
void close()
record BatchConfig(HashFunction hashFunction, int maxSize, Map<String, String> headers, Proxy proxy, HttpUtil.DownloadProgressListener listener)
record BatchResult(Map<UUID, Path> downloaded, Set<UUID> failed)
record DownloadRequest(URL url, @Nullable HashCode hash)

Fields & Constants (6)

LOGGER
MAX_KEPT_PACKS
cacheDir
eventLog
tasks
config

String literals (7)

"log.json"
"download_failed"
"no_access"
"size"
"time"
"hash"
"file"

Full Source

DownloadQueue.java139 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.packs;
2
3
import com.google.common.hash.HashCode;
4
import com.google.common.hash.HashFunction;
5
import com.mojang.datafixers.util.Either;
6
import com.mojang.logging.LogUtils;
7
import com.mojang.serialization.Codec;
8
import com.mojang.serialization.codecs.RecordCodecBuilder;
9
import java.io.IOException;
10
import java.net.Proxy;
11
import java.net.URL;
12
import java.nio.file.Files;
13
import java.nio.file.Path;
14
import java.time.Instant;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Map;
18
import java.util.Optional;
19
import java.util.Set;
20
import java.util.UUID;
21
import java.util.concurrent.CompletableFuture;
22
import net.minecraft.core.UUIDUtil;
23
import net.minecraft.util.ExtraCodecs;
24
import net.minecraft.util.FileUtil;
25
import net.minecraft.util.HttpUtil;
26
import net.minecraft.util.Util;
27
import net.minecraft.util.eventlog.JsonEventLog;
28
import net.minecraft.util.thread.ConsecutiveExecutor;
29
import org.jspecify.annotations.Nullable;
30
import org.slf4j.Logger;
31
32
public class DownloadQueue implements AutoCloseable {
33
	private static final Logger LOGGER = LogUtils.getLogger();
34
	private static final int MAX_KEPT_PACKS = 20;
35
	private final Path cacheDir;
36
	private final JsonEventLog<DownloadQueue.LogEntry> eventLog;
37
	private final ConsecutiveExecutor tasks = new ConsecutiveExecutor(Util.nonCriticalIoPool(), "download-queue");
38
39
	public DownloadQueue(final Path cacheDir) throws IOException {
40
		this.cacheDir = cacheDir;
41
		FileUtil.createDirectoriesSafe(cacheDir);
42
		this.eventLog = JsonEventLog.open(DownloadQueue.LogEntry.CODEC, cacheDir.resolve("log.json"));
43
		DownloadCacheCleaner.vacuumCacheDir(cacheDir, 20);
44
	}
45
46
	private DownloadQueue.BatchResult runDownload(final DownloadQueue.BatchConfig config, final Map<UUID, DownloadQueue.DownloadRequest> requests) {
47
		DownloadQueue.BatchResult result = new DownloadQueue.BatchResult();
48
		requests.forEach(
49
			(id, request) -> {
50
				Path targetDir = this.cacheDir.resolve(id.toString());
51
				Path downloadedFile = null;
52
53
				try {
54
					downloadedFile = HttpUtil.downloadFile(
55
						targetDir, request.url, config.headers, config.hashFunction, request.hash, config.maxSize, config.proxy, config.listener
56
					);
57
					result.downloaded.put(id, downloadedFile);
58
				} catch (Exception e) {
59
					LOGGER.error("Failed to download {}", request.url, e);
60
					result.failed.add(id);
61
				}
62
63
				try {
64
					this.eventLog
65
						.write(
66
							new DownloadQueue.LogEntry(
67
								id,
68
								request.url.toString(),
69
								Instant.now(),
70
								Optional.ofNullable(request.hash).map(HashCode::toString),
71
								downloadedFile != null ? this.getFileInfo(downloadedFile) : Either.left("download_failed")
72
							)
73
						);
74
				} catch (Exception e) {
75
					LOGGER.error("Failed to log download of {}", request.url, e);
76
				}
77
			}
78
		);
79
		return result;
80
	}
81
82
	private Either<String, DownloadQueue.FileInfoEntry> getFileInfo(final Path downloadedFile) {
83
		try {
84
			long size = Files.size(downloadedFile);
85
			Path relativePath = this.cacheDir.relativize(downloadedFile);
86
			return Either.right(new DownloadQueue.FileInfoEntry(relativePath.toString(), size));
87
		} catch (IOException e) {
88
			LOGGER.error("Failed to get file size of {}", downloadedFile, e);
89
			return Either.left("no_access");
90
		}
91
	}
92
93
	public CompletableFuture<DownloadQueue.BatchResult> downloadBatch(
94
		final DownloadQueue.BatchConfig config, final Map<UUID, DownloadQueue.DownloadRequest> requests
95
	) {
96
		return CompletableFuture.supplyAsync(() -> this.runDownload(config, requests), this.tasks::schedule);
97
	}
98
99
	@Override
100
	public void close() throws IOException {
101
		this.tasks.close();
102
		this.eventLog.close();
103
	}
104
105
	public record BatchConfig(HashFunction hashFunction, int maxSize, Map<String, String> headers, Proxy proxy, HttpUtil.DownloadProgressListener listener) {
106
	}
107
108
	public record BatchResult(Map<UUID, Path> downloaded, Set<UUID> failed) {
109
		public BatchResult() {
110
			this(new HashMap<>(), new HashSet<>());
111
		}
112
	}
113
114
	public record DownloadRequest(URL url, @Nullable HashCode hash) {
115
	}
116
117
	private record FileInfoEntry(String name, long size) {
118
		public static final Codec<DownloadQueue.FileInfoEntry> CODEC = RecordCodecBuilder.create(
119
			i -> i.group(
120
					Codec.STRING.fieldOf("name").forGetter(DownloadQueue.FileInfoEntry::name), Codec.LONG.fieldOf("size").forGetter(DownloadQueue.FileInfoEntry::size)
121
				)
122
				.apply(i, DownloadQueue.FileInfoEntry::new)
123
		);
124
	}
125
126
	private record LogEntry(UUID id, String url, Instant time, Optional<String> hash, Either<String, DownloadQueue.FileInfoEntry> errorOrFileInfo) {
127
		public static final Codec<DownloadQueue.LogEntry> CODEC = RecordCodecBuilder.create(
128
			i -> i.group(
129
					UUIDUtil.STRING_CODEC.fieldOf("id").forGetter(DownloadQueue.LogEntry::id),
130
					Codec.STRING.fieldOf("url").forGetter(DownloadQueue.LogEntry::url),
131
					ExtraCodecs.INSTANT_ISO8601.fieldOf("time").forGetter(DownloadQueue.LogEntry::time),
132
					Codec.STRING.optionalFieldOf("hash").forGetter(DownloadQueue.LogEntry::hash),
133
					Codec.mapEither(Codec.STRING.fieldOf("error"), DownloadQueue.FileInfoEntry.CODEC.fieldOf("file")).forGetter(DownloadQueue.LogEntry::errorOrFileInfo)
134
				)
135
				.apply(i, DownloadQueue.LogEntry::new)
136
		);
137
	}
138
}
139

Decompiled Minecraft 26.3 source of DownloadQueue (net/minecraft/server/packs/DownloadQueue.java) with Mojang official mappings, stored verbatim on the site.