Repository navigation
New features
-
A database can be opened read-only, e.g. on a read-only mount or to read another application's data safely. Collections load without lock files, so the folder does not have to be writable, and every operation that would change the database (insert, save, upsert, remove, the find-and-modify/remove operations, creating or dropping collections, schema updates,
changeEncryptionandrestore) throwsInvalidJsonDbApiUsageException.JsonDBTemplategains a public constructor that takes aJsonDBConfig:JsonDBConfig config = new JsonDBConfig(dbPath, List.of(User.class), null, false, null); config.setReadOnly(true); JsonDBTemplate jsonDBTemplate = new JsonDBTemplate(config);
-
Documents with a numeric
@Id(Long,Integer,longorint) inserted or upserted without an id now get the next number after the highest id in the collection, starting at 1. Forlongandint, 0 means no id. Until now such a document failed withInvalidJsonDbApiUsageException("Failed to invoke setter method … due to wrong arguments") for aLongid, or was stored under 0 for anintid, so the next one failed as a duplicate. If you have anint/longid document stored under 0,upsertof it now inserts a new document instead of updating it; give it another id. Numbers are not a database sequence: when the document with the highest id is removed, that id is handed out again.Stringids still get a random UUID. (#36) -
Ids for documents saved without one can come from your own
IdGenerator, set for one class or as the default for all. It gets the collection name and the document, e.g. to build an id from its fields:jsonDBTemplate.setIdGenerator(Order.class, (collection, order) -> "ORD-" + ((Order) order).getNumber()); jsonDBTemplate.setIdGenerator((collection, document) -> ulids.next()); // every other collection
JsonDB rejects an id that is null, of the wrong type or already in the collection with
InvalidJsonDbApiUsageException, before anything is saved. The same setters are onJsonDBConfig. -
The database folder can be given as a
java.nio.file.Path, on any file system, not only the default one: everyJsonDBTemplateandJsonDBConfigconstructor that takes the folder as aStringhas aPathtwin. For example, a database in memory with Jimfs:FileSystem fs = Jimfs.newFileSystem(Configuration.unix()); JsonDBTemplate jsonDBTemplate = new JsonDBTemplate(fs.getPath("/db"), List.of(User.class));
backup()andrestore()take aPathtoo, so a database in memory can be backed up to disk and the other way round. A merging restore of a database that is not on the default file system extracts the backup to a temporary folder inside the database folder instead of the system's temporary folder, and deletes it afterwards.JsonDBOperationsgainsbackup(Path)andrestore(Path, boolean), so a class of your own that implements it must add them. Code that passes a literalnullas the folder or the backup path no longer compiles, because the call is ambiguous between theStringandPathversions. (#17)
Deprecations
- The methods that take or return a
java.io.FilehavePathversions and will be removed in a future release:JsonDBConfig.getDbFilesLocation()(usegetDbFilesPath(); it throwsUnsupportedOperationExceptionfor a folder that is not on the default file system),Util.delete(File),Util.stampVersion(JsonDBConfig, File, String), theFileconstructors ofJsonReaderandJsonWriter, and theFilemethods ofJsonDbArchive, includingresolveBackupZipFile(String)andresolveRestoreZipFile(String). UnlikeUtil.delete(File),Util.delete(Path)deletes a symbolic link inside the folder instead of the contents of the folder it points to. (#17)
Bug fixes
- In strict mode (
compatibilityModefalse),updateCollectionSchema()could not migrate a collection whose file still had keys the class no longer has: the file failed to load, so the update threw "Collection ... not found. Create collection first", andcreateCollection()refused because the file exists. The update now runs on the file even when the collection did not load, and loads it afterwards. (#84) - A database folder that JsonDB cannot write to (e.g. a read-only mount, or files owned by another user) opened as an empty database: no collection loaded, and the only sign was an error in the log. Opening it now throws
InvalidJsonDbApiUsageException, saying the folder is not writable and pointing to the read-only mode. (#111)
Installation
<dependency>
<groupId>io.jsondb</groupId>
<artifactId>jsondb-core</artifactId>
<version>1.1.93</version>
</dependency>