Add a preference for the JGit text buffer size
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / RepositoryInitializer.java
blob6b28f74df6b9f451987e7819ccf22208ca3b3dee
1 /*******************************************************************************
2 * Copyright (c) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
11 * Contributors:
12 * Thomas Wolf -- factored out of Activator
13 *******************************************************************************/
14 package org.eclipse.egit.core;
16 import org.eclipse.core.resources.IWorkspace;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
20 import org.eclipse.core.runtime.preferences.IPreferencesService;
21 import org.eclipse.core.runtime.preferences.InstanceScope;
22 import org.eclipse.egit.core.internal.CoreText;
23 import org.eclipse.egit.core.internal.hosts.GitHosts;
24 import org.eclipse.egit.core.internal.indexdiff.IndexDiffCache;
25 import org.eclipse.jgit.diff.RawText;
26 import org.eclipse.jgit.storage.file.WindowCacheConfig;
27 import org.eclipse.jgit.util.SystemReader;
28 import org.osgi.framework.FrameworkUtil;
29 import org.osgi.framework.ServiceRegistration;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
33 import org.osgi.service.component.annotations.Reference;
35 /**
36 * An OSGi component triggered when the workspace is ready that sets up the
37 * central EGit caches.
39 @Component
40 public class RepositoryInitializer {
42 private IPreferencesService preferencesService;
44 private ServiceRegistration<RepositoryCache> registration;
46 private IEclipsePreferences gitCorePreferences;
48 private IPreferenceChangeListener prefsListener;
50 @Reference
51 void setPreferencesService(IPreferencesService service) {
52 this.preferencesService = service;
55 @Reference
56 void setWorkspace(@SuppressWarnings("unused") IWorkspace workspace) {
57 // Needed indirectly by the preferences service
60 @Activate
61 void start() {
62 try {
63 reconfigureWindowCache(preferencesService);
64 } catch (RuntimeException | ExceptionInInitializerError e) {
65 Activator.logError(CoreText.Activator_ReconfigureWindowCacheError,
66 e);
68 gitCorePreferences = InstanceScope.INSTANCE
69 .getNode(Activator.PLUGIN_ID);
70 GitHosts.loadFromPreferences(gitCorePreferences);
71 updateTextBufferSize();
72 prefsListener = event -> {
73 if (GitCorePreferences.core_gitServers.equals(event.getKey())) {
74 GitHosts.loadFromPreferences(gitCorePreferences);
76 if (GitCorePreferences.core_textBufferSize.equals(event.getKey())) {
77 updateTextBufferSize();
80 gitCorePreferences.addPreferenceChangeListener(prefsListener);
81 registration = FrameworkUtil.getBundle(getClass()).getBundleContext()
82 .registerService(RepositoryCache.class,
83 RepositoryCache.INSTANCE, null);
86 @Deactivate
87 void shutDown() {
88 registration.unregister();
89 if (gitCorePreferences != null) {
90 if (prefsListener != null) {
91 gitCorePreferences
92 .removePreferenceChangeListener(prefsListener);
93 prefsListener = null;
95 gitCorePreferences = null;
97 RepositoryUtil.INSTANCE.clear();
98 IndexDiffCache.INSTANCE.dispose();
99 RepositoryCache.INSTANCE.clear();
102 private void updateTextBufferSize() {
103 int bufferSize = preferencesService.getInt(Activator.PLUGIN_ID,
104 GitCorePreferences.core_textBufferSize, -1, null);
105 if (bufferSize >= 0) {
106 // Guard against broken preferences that give large values. This
107 // buffer size should not be arbitrarily large. JGit ensures a
108 // minimum of 8 KiB.
109 RawText.setBufferSize(Math.min(bufferSize, 128 * 1024));
114 * Update the settings for the global window cache of the workspace.
116 public static void reconfigureWindowCache() {
117 reconfigureWindowCache(Platform.getPreferencesService());
120 private static void reconfigureWindowCache(IPreferencesService prefs) {
121 WindowCacheConfig c = new WindowCacheConfig();
122 c.setPackedGitLimit(prefs.getInt(Activator.PLUGIN_ID,
123 GitCorePreferences.core_packedGitLimit, 0, null));
124 c.setPackedGitWindowSize(prefs.getInt(Activator.PLUGIN_ID,
125 GitCorePreferences.core_packedGitWindowSize, 0, null));
126 if (SystemReader.getInstance().isWindows()) {
127 c.setPackedGitMMAP(false);
128 } else {
129 c.setPackedGitMMAP(prefs.getBoolean(Activator.PLUGIN_ID,
130 GitCorePreferences.core_packedGitMMAP, false, null));
132 c.setDeltaBaseCacheLimit(prefs.getInt(Activator.PLUGIN_ID,
133 GitCorePreferences.core_deltaBaseCacheLimit, 0, null));
134 c.setStreamFileThreshold(prefs.getInt(Activator.PLUGIN_ID,
135 GitCorePreferences.core_streamFileThreshold, 0, null));
136 c.setExposeStatsViaJmx(false);
137 c.install();