Skip to content

Commit 946d87c

Browse files
SyncServerBuilder: add flags, history and worker threads options
1 parent 126d341 commit 946d87c

File tree

1 file changed

+83
-8
lines changed

1 file changed

+83
-8
lines changed

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerBuilder.java

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.objectbox.sync.Sync;
2929
import io.objectbox.sync.SyncCredentials;
3030
import io.objectbox.sync.SyncCredentialsToken;
31+
import io.objectbox.sync.SyncFlags;
3132
import io.objectbox.sync.listener.SyncChangeListener;
3233

3334
/**
@@ -46,6 +47,11 @@ public class SyncServerBuilder {
4647
private @Nullable String clusterId;
4748
private final List<ClusterPeerInfo> clusterPeers = new ArrayList<>();
4849
private int clusterFlags;
50+
private long historySizeMaxKb;
51+
private long historySizeTargetKb;
52+
private int syncFlags;
53+
private int syncServerFlags;
54+
private int workerThreads;
4955

5056
/**
5157
* Use {@link Sync#server(BoxStore, String, SyncCredentials)} instead.
@@ -107,6 +113,9 @@ public SyncServerBuilder changeListener(SyncChangeListener changeListener) {
107113
* Enables cluster mode (requires the Cluster feature) and associates this cluster peer with the given ID.
108114
* <p>
109115
* Cluster peers need to share the same ID to be in the same cluster.
116+
*
117+
* @see #clusterPeer(String, SyncCredentials)
118+
* @see #clusterFlags(int)
110119
*/
111120
public SyncServerBuilder clusterId(String id) {
112121
checkNotNull(id, "Cluster ID must not be null");
@@ -156,6 +165,65 @@ public SyncServerBuilder clusterFlags(int flags) {
156165
return this;
157166
}
158167

168+
/**
169+
* Sets the maximum transaction history size.
170+
* <p>
171+
* Once the maximum size is reached, old transaction logs are deleted to stay below this limit. This is sometimes
172+
* also called "history pruning" in the context of Sync.
173+
* <p>
174+
* If not set or set to 0, defaults to no limit.
175+
*
176+
* @see #historySizeTargetKb(long)
177+
*/
178+
public SyncServerBuilder historySizeMaxKb(long historySizeMaxKb) {
179+
this.historySizeMaxKb = historySizeMaxKb;
180+
return this;
181+
}
182+
183+
/**
184+
* Sets the target transaction history size.
185+
* <p>
186+
* Once the maximum size ({@link #historySizeMaxKb(long)}) is reached, old transaction logs are deleted until this
187+
* size target is reached (lower than the maximum size). Using this target size typically lowers the frequency of
188+
* history pruning and thus may improve efficiency.
189+
* <p>
190+
* If not set or set to 0, defaults to {@link #historySizeMaxKb(long)}.
191+
*/
192+
public SyncServerBuilder historySizeTargetKb(long historySizeTargetKb) {
193+
this.historySizeTargetKb = historySizeTargetKb;
194+
return this;
195+
}
196+
197+
/**
198+
* Sets bit flags to adjust Sync behavior, like additional logging.
199+
*
200+
* @param syncFlags One or more of {@link SyncFlags}.
201+
*/
202+
public SyncServerBuilder syncFlags(int syncFlags) {
203+
this.syncFlags = syncFlags;
204+
return this;
205+
}
206+
207+
/**
208+
* Sets bit flags to configure the Sync server.
209+
*
210+
* @param syncServerFlags One or more of {@link SyncServerFlags}.
211+
*/
212+
public SyncServerBuilder syncServerFlags(int syncServerFlags) {
213+
this.syncServerFlags = syncServerFlags;
214+
return this;
215+
}
216+
217+
/**
218+
* Sets the number of workers for the main task pool.
219+
* <p>
220+
* If not set or set to 0, this uses a hardware-dependant default, e.g. 3 * CPU "cores".
221+
*/
222+
public SyncServerBuilder workerThreads(int workerThreads) {
223+
this.workerThreads = workerThreads;
224+
return this;
225+
}
226+
159227
/**
160228
* Builds and returns a Sync server ready to {@link SyncServer#start()}.
161229
* <p>
@@ -209,21 +277,28 @@ byte[] buildSyncServerOptions() {
209277
int authenticationMethodsOffset = buildAuthenticationMethods(fbb);
210278
int clusterPeersVectorOffset = buildClusterPeers(fbb);
211279

212-
// TODO Support remaining options
213280
// After collecting all offsets, create options
214281
SyncServerOptions.startSyncServerOptions(fbb);
215282
SyncServerOptions.addUrl(fbb, urlOffset);
216283
SyncServerOptions.addAuthenticationMethods(fbb, authenticationMethodsOffset);
217-
// SyncServerOptions.addSyncFlags();
218-
// SyncServerOptions.addSyncServerFlags();
284+
if (syncFlags > 0) {
285+
SyncServerOptions.addSyncFlags(fbb, syncFlags);
286+
}
287+
if (syncServerFlags > 0) {
288+
SyncServerOptions.addSyncFlags(fbb, syncServerFlags);
289+
}
219290
if (certificatePathOffset > 0) {
220291
SyncServerOptions.addCertificatePath(fbb, certificatePathOffset);
221292
}
222-
// SyncServerOptions.addWorkerThreads();
223-
// SyncServerOptions.addHistorySizeMaxKb();
224-
// SyncServerOptions.addHistorySizeTargetKb();
225-
// SyncServerOptions.addAdminUrl();
226-
// SyncServerOptions.addAdminThreads();
293+
if (workerThreads > 0) {
294+
SyncServerOptions.addWorkerThreads(fbb, workerThreads);
295+
}
296+
if (historySizeMaxKb > 0) {
297+
SyncServerOptions.addHistorySizeMaxKb(fbb, historySizeMaxKb);
298+
}
299+
if (historySizeTargetKb > 0) {
300+
SyncServerOptions.addHistorySizeTargetKb(fbb, historySizeTargetKb);
301+
}
227302
if (clusterIdOffset > 0) {
228303
SyncServerOptions.addClusterId(fbb, clusterIdOffset);
229304
}

0 commit comments

Comments
 (0)