Search in sources :

Example 1 with FileStorageMigrationException

use of oap.storage.migration.FileStorageMigrationException in project oap by oaplatform.

the class FsPersistenceBackend method migration.

@SneakyThrows
private Path migration(Path path) {
    return Threads.synchronously(lock, () -> {
        JsonMetadata oldV = new JsonMetadata(Binder.json.unmarshal(new TypeReference<Map<String, Object>>() {
        }, path));
        Persisted fn = Persisted.valueOf(path);
        log.debug("migration {}", fn);
        val migration = Lists.find2(migrations, m -> m.fromVersion() == fn.version);
        if (migration == null)
            throw new FileStorageMigrationException("migration from version " + fn + " not found");
        Path name = fn.toVersion(migration.fromVersion() + 1);
        JsonMetadata newV = migration.run(oldV);
        long writeLen = -1;
        while (name.toFile().length() != writeLen) {
            try (val out = new CountingOutputStream(IoStreams.out(name, PLAIN, DEFAULT_BUFFER, false, true))) {
                Binder.json.marshal(out, newV.underlying);
                writeLen = out.getCount();
            }
        }
        Files.delete(path);
        return name;
    });
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) CountingOutputStream(com.google.common.io.CountingOutputStream) JsonMetadata(oap.storage.migration.JsonMetadata) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ToString(lombok.ToString) FileStorageMigrationException(oap.storage.migration.FileStorageMigrationException) SneakyThrows(lombok.SneakyThrows)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 CountingOutputStream (com.google.common.io.CountingOutputStream)1 Path (java.nio.file.Path)1 SneakyThrows (lombok.SneakyThrows)1 ToString (lombok.ToString)1 lombok.val (lombok.val)1 FileStorageMigrationException (oap.storage.migration.FileStorageMigrationException)1 JsonMetadata (oap.storage.migration.JsonMetadata)1