Skip to content

Commit 05cebe9

Browse files
committed
initialDbFile provisioning
1 parent b04a402 commit 05cebe9

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616

1717
package io.objectbox;
1818

19+
import org.greenrobot.essentials.io.IoUtils;
20+
21+
import java.io.BufferedInputStream;
22+
import java.io.BufferedOutputStream;
1923
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.FileNotFoundException;
26+
import java.io.FileOutputStream;
27+
import java.io.InputStream;
28+
import java.io.OutputStream;
2029
import java.lang.reflect.Method;
2130
import java.util.ArrayList;
2231
import java.util.List;
@@ -26,6 +35,7 @@
2635

2736
import io.objectbox.annotation.apihint.Experimental;
2837
import io.objectbox.annotation.apihint.Internal;
38+
import io.objectbox.exception.DbException;
2939
import io.objectbox.ideasonly.ModelUpdate;
3040

3141
/**
@@ -80,6 +90,7 @@ public class BoxStoreBuilder {
8090
TxCallback failedReadTxAttemptCallback;
8191

8292
final List<EntityInfo> entityInfoList = new ArrayList<>();
93+
private Factory<InputStream> initialDbFileFactory;
8394

8495
/** Not for application use. */
8596
public static BoxStoreBuilder createDebugWithoutModel() {
@@ -306,6 +317,22 @@ public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback failedReadTxAttemp
306317
return this;
307318
}
308319

320+
@Experimental
321+
public BoxStoreBuilder initialDbFile(final File initialDbFile) {
322+
return initialDbFile(new Factory<InputStream>() {
323+
@Override
324+
public InputStream provide() throws FileNotFoundException {
325+
return new FileInputStream(initialDbFile);
326+
}
327+
});
328+
}
329+
330+
@Experimental
331+
public BoxStoreBuilder initialDbFile(Factory<InputStream> initialDbFileFactory) {
332+
this.initialDbFileFactory = initialDbFileFactory;
333+
return this;
334+
}
335+
309336
/**
310337
* Builds a {@link BoxStore} using any given configuration.
311338
*/
@@ -314,9 +341,35 @@ public BoxStore build() {
314341
name = dbName(name);
315342
directory = getDbDir(baseDirectory, name);
316343
}
344+
checkProvisionInitialDbFile();
317345
return new BoxStore(this);
318346
}
319347

348+
private void checkProvisionInitialDbFile() {
349+
if (initialDbFileFactory != null) {
350+
String dataDir = BoxStore.getCanonicalPath(directory);
351+
File file = new File(dataDir, "data.mdb");
352+
if (!file.exists()) {
353+
InputStream in = null;
354+
OutputStream out = null;
355+
try {
356+
in = initialDbFileFactory.provide();
357+
if (in == null) {
358+
throw new DbException("Factory did not provide a resource");
359+
}
360+
in = new BufferedInputStream(in);
361+
out = new BufferedOutputStream(new FileOutputStream(file));
362+
IoUtils.copyAllBytes(in, out);
363+
} catch (Exception e) {
364+
throw new DbException("Could not provision initial data file", e);
365+
} finally {
366+
IoUtils.safeClose(out);
367+
IoUtils.safeClose(in);
368+
}
369+
}
370+
}
371+
}
372+
320373
static File getDbDir(@Nullable File baseDirectoryOrNull, @Nullable String nameOrNull) {
321374
String name = dbName(nameOrNull);
322375
if (baseDirectoryOrNull != null) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2017 ObjectBox Ltd. All rights reserved.
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+
*/
16+
17+
package io.objectbox;
18+
19+
import io.objectbox.annotation.apihint.Experimental;
20+
21+
22+
@Experimental
23+
/**
24+
* Generic Factory that provides a resource on demand (if and when it is required).
25+
*/
26+
public interface Factory<T> {
27+
T provide() throws Exception;
28+
}

0 commit comments

Comments
 (0)