Skip to content

Commit 642ff74

Browse files
In-memory: support memory: prefix in BoxStore #194
1 parent 3601414 commit 642ff74

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,6 +69,9 @@ public class BoxStore implements Closeable {
6969
@Nullable private static Object context;
7070
@Nullable private static Object relinker;
7171

72+
/** Prefix supplied with database directory to signal a file-less and in-memory database should be used. */
73+
public static final String IN_MEMORY_PREFIX = "memory:";
74+
7275
/** Change so ReLinker will update native library when using workaround loading. */
7376
public static final String JNI_VERSION = "3.7.1";
7477

@@ -318,6 +321,12 @@ public static boolean isSyncServerAvailable() {
318321
}
319322

320323
static String getCanonicalPath(File directory) {
324+
// Skip directory check if in-memory prefix is used.
325+
if (directory.getPath().startsWith(IN_MEMORY_PREFIX)) {
326+
// Just return the path as is (e.g. "memory:data"), safe to use for string-based open check as well.
327+
return directory.getPath();
328+
}
329+
321330
if (directory.exists()) {
322331
if (!directory.isDirectory()) {
323332
throw new DbException("Is not a directory: " + directory.getAbsolutePath());

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package io.objectbox;
1818

19+
import org.greenrobot.essentials.io.IoUtils;
20+
1921
import java.io.BufferedInputStream;
2022
import java.io.BufferedOutputStream;
2123
import java.io.File;
@@ -43,7 +45,6 @@
4345
import io.objectbox.exception.DbMaxReadersExceededException;
4446
import io.objectbox.flatbuffers.FlatBufferBuilder;
4547
import io.objectbox.ideasonly.ModelUpdate;
46-
import org.greenrobot.essentials.io.IoUtils;
4748

4849
/**
4950
* Configures and builds a {@link BoxStore} with reasonable defaults. To get an instance use {@code MyObjectBox.builder()}.
@@ -161,8 +162,21 @@ public BoxStoreBuilder name(String name) {
161162
}
162163

163164
/**
164-
* The directory where all DB files should be placed in.
165-
* Cannot be used in combination with {@link #name(String)}/{@link #baseDirectory(File)}.
165+
* The directory where all database files should be placed in.
166+
* <p>
167+
* If the directory does not exist, it will be created. Make sure the process has permissions to write to this
168+
* directory.
169+
* <p>
170+
* To switch to an in-memory database, use a file path with {@link BoxStore#IN_MEMORY_PREFIX} and an identifier
171+
* instead:
172+
* <p>
173+
* <pre>{@code
174+
* BoxStore inMemoryStore = MyObjectBox.builder()
175+
* .directory(BoxStore.IN_MEMORY_PREFIX + "notes-db")
176+
* .build();
177+
* }</pre>
178+
* <p>
179+
* Can not be used in combination with {@link #name(String)} or {@link #baseDirectory(File)}.
166180
*/
167181
public BoxStoreBuilder directory(File directory) {
168182
if (name != null) {

0 commit comments

Comments
 (0)