Skip to content

jsondb-core 1.1.93

Latest

Choose a tag to compare

@FarooqKhan FarooqKhan released this 22 Nov 08:49
· 2 commits to master since this release
573a036

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, changeEncryption and restore) throws InvalidJsonDbApiUsageException. JsonDBTemplate gains a public constructor that takes a JsonDBConfig:

    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, long or int) inserted or upserted without an id now get the next number after the highest id in the collection, starting at 1. For long and int, 0 means no id. Until now such a document failed with InvalidJsonDbApiUsageException ("Failed to invoke setter method … due to wrong arguments") for a Long id, or was stored under 0 for an int id, so the next one failed as a duplicate. If you have an int/long id document stored under 0, upsert of 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. String ids 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 on JsonDBConfig.

  • The database folder can be given as a java.nio.file.Path, on any file system, not only the default one: every JsonDBTemplate and JsonDBConfig constructor that takes the folder as a String has a Path twin. 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() and restore() take a Path too, 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. JsonDBOperations gains backup(Path) and restore(Path, boolean), so a class of your own that implements it must add them. Code that passes a literal null as the folder or the backup path no longer compiles, because the call is ambiguous between the String and Path versions. (#17)

Deprecations

  • The methods that take or return a java.io.File have Path versions and will be removed in a future release: JsonDBConfig.getDbFilesLocation() (use getDbFilesPath(); it throws UnsupportedOperationException for a folder that is not on the default file system), Util.delete(File), Util.stampVersion(JsonDBConfig, File, String), the File constructors of JsonReader and JsonWriter, and the File methods of JsonDbArchive, including resolveBackupZipFile(String) and resolveRestoreZipFile(String). Unlike Util.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 (compatibilityMode false), 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", and createCollection() 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>

Maven Central