@@ -78,6 +78,9 @@ public class BoxStoreBuilder { |
78 | 78 | /** Ignored by BoxStore */ |
79 | 79 | private String name; |
80 | 80 |
|
| 81 | + /** If non-null, using an in-memory database with this identifier. */ |
| 82 | + private String inMemory; |
| 83 | + |
81 | 84 | /** Defaults to {@link #DEFAULT_MAX_DB_SIZE_KBYTE}. */ |
82 | 85 | long maxSizeInKByte = DEFAULT_MAX_DB_SIZE_KBYTE; |
83 | 86 |
|
@@ -145,14 +148,15 @@ public BoxStoreBuilder(byte[] model) { |
145 | 148 | } |
146 | 149 |
|
147 | 150 | /** |
148 | | - * Name of the database, which will be used as a directory for DB files. |
| 151 | + * Name of the database, which will be used as a directory for database files. |
149 | 152 | * You can also specify a base directory for this one using {@link #baseDirectory(File)}. |
150 | | - * Cannot be used in combination with {@link #directory(File)}. |
| 153 | + * Cannot be used in combination with {@link #directory(File)} and {@link #inMemory(String)}. |
151 | 154 | * <p> |
152 | 155 | * Default: "objectbox", {@link #DEFAULT_NAME} (unless {@link #directory(File)} is used) |
153 | 156 | */ |
154 | 157 | public BoxStoreBuilder name(String name) { |
155 | 158 | checkIsNull(directory, "Already has directory, cannot assign name"); |
| 159 | + checkIsNull(inMemory, "Already set to in-memory database, cannot assign name"); |
156 | 160 | if (name.contains("/") || name.contains("\\")) { |
157 | 161 | throw new IllegalArgumentException("Name may not contain (back) slashes. " + |
158 | 162 | "Use baseDirectory() or directory() to configure alternative directories"); |
@@ -175,11 +179,14 @@ public BoxStoreBuilder name(String name) { |
175 | 179 | * .directory(BoxStore.IN_MEMORY_PREFIX + "notes-db") |
176 | 180 | * .build(); |
177 | 181 | * }</pre> |
| 182 | + * Alternatively, use {@link #inMemory(String)}. |
178 | 183 | * <p> |
179 | | - * Can not be used in combination with {@link #name(String)} or {@link #baseDirectory(File)}. |
| 184 | + * Can not be used in combination with {@link #name(String)}, {@link #baseDirectory(File)} |
| 185 | + * or {@link #inMemory(String)}. |
180 | 186 | */ |
181 | 187 | public BoxStoreBuilder directory(File directory) { |
182 | 188 | checkIsNull(name, "Already has name, cannot assign directory"); |
| 189 | + checkIsNull(inMemory, "Already set to in-memory database, cannot assign directory"); |
183 | 190 | if (!android) { |
184 | 191 | checkIsNull(baseDirectory, "Already has base directory, cannot assign directory"); |
185 | 192 | } |
@@ -190,14 +197,29 @@ public BoxStoreBuilder directory(File directory) { |
190 | 197 | /** |
191 | 198 | * In combination with {@link #name(String)}, this lets you specify the location of where the DB files should be |
192 | 199 | * stored. |
193 | | - * Cannot be used in combination with {@link #directory(File)}. |
| 200 | + * Cannot be used in combination with {@link #directory(File)} or {@link #inMemory(String)}. |
194 | 201 | */ |
195 | 202 | public BoxStoreBuilder baseDirectory(File baseDirectory) { |
196 | 203 | checkIsNull(directory, "Already has directory, cannot assign base directory"); |
| 204 | + checkIsNull(inMemory, "Already set to in-memory database, cannot assign base directory"); |
197 | 205 | this.baseDirectory = baseDirectory; |
198 | 206 | return this; |
199 | 207 | } |
200 | 208 |
|
| 209 | + /** |
| 210 | + * Switches to an in-memory database using the given name as its identifier. |
| 211 | + * <p> |
| 212 | + * Can not be used in combination with {@link #name(String)}, {@link #directory(File)} |
| 213 | + * or {@link #baseDirectory(File)}. |
| 214 | + */ |
| 215 | + public BoxStoreBuilder inMemory(String identifier) { |
| 216 | + checkIsNull(name, "Already has name, cannot switch to in-memory database"); |
| 217 | + checkIsNull(directory, "Already has directory, cannot switch to in-memory database"); |
| 218 | + checkIsNull(baseDirectory, "Already has base directory, cannot switch to in-memory database"); |
| 219 | + inMemory = identifier; |
| 220 | + return this; |
| 221 | + } |
| 222 | + |
201 | 223 | /** |
202 | 224 | * Use to check conflicting properties are not set. |
203 | 225 | * If not null, throws {@link IllegalStateException} with the given message. |
@@ -209,17 +231,18 @@ private static void checkIsNull(@Nullable Object value, String errorMessage) { |
209 | 231 | } |
210 | 232 |
|
211 | 233 | /** |
212 | | - * On Android, you can pass a Context to set the base directory using this method. |
213 | | - * This will conveniently configure the storage location to be in the files directory of your app. |
| 234 | + * Use on Android to pass a <a href="https://developer.android.com/reference/android/content/Context">Context</a> |
| 235 | + * for loading the native library and, if {@link #inMemory(String)} was not called before, creating the base |
| 236 | + * directory for database files in the |
| 237 | + * <a href="https://developer.android.com/reference/android/content/Context#getFilesDir()">files directory of the app</a>. |
214 | 238 | * <p> |
215 | 239 | * In more detail, this assigns the base directory (see {@link #baseDirectory}) to |
216 | 240 | * {@code context.getFilesDir() + "/objectbox/"}. |
217 | | - * Thus, when using the default name (also "objectbox" unless overwritten using {@link #name(String)}), the default |
218 | | - * location of DB files will be "objectbox/objectbox/" inside the app files directory. |
219 | | - * If you specify a custom name, for example with {@code name("foobar")}, it would become |
220 | | - * "objectbox/foobar/". |
| 241 | + * Thus, when using the default name (also "objectbox", unless overwritten using {@link #name(String)}), the default |
| 242 | + * location of database files will be "objectbox/objectbox/" inside the app's files directory. |
| 243 | + * If a custom name is specified, for example with {@code name("foobar")}, it would become "objectbox/foobar/". |
221 | 244 | * <p> |
222 | | - * Alternatively, you can also use {@link #baseDirectory} or {@link #directory(File)} instead. |
| 245 | + * Alternatively, use {@link #baseDirectory(File)} or {@link #directory(File)}. |
223 | 246 | */ |
224 | 247 | public BoxStoreBuilder androidContext(Object context) { |
225 | 248 | //noinspection ConstantConditions Annotation does not enforce non-null. |
@@ -228,17 +251,21 @@ public BoxStoreBuilder androidContext(Object context) { |
228 | 251 | } |
229 | 252 | this.context = getApplicationContext(context); |
230 | 253 |
|
231 | | - File baseDir = getAndroidBaseDir(context); |
232 | | - if (!baseDir.exists()) { |
233 | | - baseDir.mkdir(); |
234 | | - if (!baseDir.exists()) { // check baseDir.exists() because of potential concurrent processes |
235 | | - throw new RuntimeException("Could not init Android base dir at " + baseDir.getAbsolutePath()); |
| 254 | + // Only create directories if not already an in-memory database. |
| 255 | + // Note: this will still create directories if this is called before switching to an in-memory database. |
| 256 | + if (inMemory == null) { |
| 257 | + File baseDir = getAndroidBaseDir(context); |
| 258 | + if (!baseDir.exists()) { |
| 259 | + baseDir.mkdir(); |
| 260 | + if (!baseDir.exists()) { // check baseDir.exists() because of potential concurrent processes |
| 261 | + throw new RuntimeException("Could not init Android base dir at " + baseDir.getAbsolutePath()); |
| 262 | + } |
236 | 263 | } |
| 264 | + if (!baseDir.isDirectory()) { |
| 265 | + throw new RuntimeException("Android base dir is not a dir: " + baseDir.getAbsolutePath()); |
| 266 | + } |
| 267 | + baseDirectory = baseDir; |
237 | 268 | } |
238 | | - if (!baseDir.isDirectory()) { |
239 | | - throw new RuntimeException("Android base dir is not a dir: " + baseDir.getAbsolutePath()); |
240 | | - } |
241 | | - baseDirectory = baseDir; |
242 | 269 | android = true; |
243 | 270 | return this; |
244 | 271 | } |
@@ -524,7 +551,7 @@ public BoxStoreBuilder debugRelations() { |
524 | 551 | * {@link DbException} are thrown during query execution). |
525 | 552 | * |
526 | 553 | * @param queryAttempts number of attempts a query find operation will be executed before failing. |
527 | | - * Recommended values are in the range of 2 to 5, e.g. a value of 3 as a starting point. |
| 554 | + * Recommended values are in the range of 2 to 5, e.g. a value of 3 as a starting point. |
528 | 555 | */ |
529 | 556 | @Experimental |
530 | 557 | public BoxStoreBuilder queryAttempts(int queryAttempts) { |
@@ -603,11 +630,15 @@ byte[] buildFlatStoreOptions(String canonicalPath) { |
603 | 630 | * Builds a {@link BoxStore} using any given configuration. |
604 | 631 | */ |
605 | 632 | public BoxStore build() { |
| 633 | + if (inMemory != null) { |
| 634 | + directory = new File(BoxStore.IN_MEMORY_PREFIX + inMemory); |
| 635 | + } |
606 | 636 | if (directory == null) { |
607 | | - name = dbName(name); |
608 | 637 | directory = getDbDir(baseDirectory, name); |
609 | 638 | } |
610 | | - checkProvisionInitialDbFile(); |
| 639 | + if (inMemory == null) { |
| 640 | + checkProvisionInitialDbFile(); |
| 641 | + } |
611 | 642 | return new BoxStore(this); |
612 | 643 | } |
613 | 644 |
|
|