Search in sources :

Example 51 with MemoryARW

use of io.questdb.cairo.vm.api.MemoryARW in project questdb by bluestreak01.

the class EngineMigration method migrateEngineTo.

public static void migrateEngineTo(CairoEngine engine, int latestVersion, boolean force) {
    final FilesFacade ff = engine.getConfiguration().getFilesFacade();
    final CairoConfiguration configuration = engine.getConfiguration();
    int tempMemSize = 8;
    long mem = Unsafe.malloc(tempMemSize, MemoryTag.NATIVE_DEFAULT);
    try (MemoryARW virtualMem = Vm.getARWInstance(ff.getPageSize(), Integer.MAX_VALUE, MemoryTag.NATIVE_DEFAULT);
        Path path = new Path();
        MemoryMARW rwMemory = Vm.getMARWInstance()) {
        MigrationContext context = new MigrationContext(engine, mem, tempMemSize, virtualMem, rwMemory);
        path.of(configuration.getRoot());
        // check if all tables have been upgraded already
        path.concat(TableUtils.UPGRADE_FILE_NAME).$();
        final boolean existed = !force && ff.exists(path);
        long upgradeFd = openFileRWOrFail(ff, path);
        LOG.debug().$("open [fd=").$(upgradeFd).$(", path=").$(path).$(']').$();
        if (existed) {
            int currentVersion = TableUtils.readIntOrFail(ff, upgradeFd, 0, mem, path);
            if (currentVersion >= latestVersion) {
                LOG.info().$("table structures are up to date").$();
                ff.close(upgradeFd);
                upgradeFd = -1;
            }
        }
        if (upgradeFd != -1) {
            try {
                LOG.info().$("upgrading database [version=").$(latestVersion).I$();
                if (upgradeTables(context, latestVersion)) {
                    TableUtils.writeIntOrFail(ff, upgradeFd, 0, latestVersion, mem, path);
                }
            } finally {
                Vm.bestEffortClose(ff, LOG, upgradeFd, true, Integer.BYTES);
            }
        }
    } finally {
        Unsafe.free(mem, tempMemSize, MemoryTag.NATIVE_DEFAULT);
    }
}
Also used : Path(io.questdb.std.str.Path) MemoryMARW(io.questdb.cairo.vm.api.MemoryMARW) MemoryARW(io.questdb.cairo.vm.api.MemoryARW)

Example 52 with MemoryARW

use of io.questdb.cairo.vm.api.MemoryARW in project questdb by bluestreak01.

the class Mig506 method migrate.

static void migrate(MigrationContext migrationContext) {
    // Update transaction file
    // Before there was 1 int per symbol and list of removed partitions
    // Now there is 2 ints per symbol and 4 longs per each non-removed partition
    MigrationActions.LOG.info().$("rebuilding tx file [table=").$(migrationContext.getTablePath()).I$();
    Path path = migrationContext.getTablePath();
    final FilesFacade ff = migrationContext.getFf();
    int pathDirLen = path.length();
    path.concat(TXN_FILE_NAME).$();
    if (!ff.exists(path)) {
        MigrationActions.LOG.error().$("tx file does not exist, nothing to migrate [path=").$(path).I$();
        return;
    }
    EngineMigration.backupFile(ff, path, migrationContext.getTablePath2(), TXN_FILE_NAME, 417);
    MigrationActions.LOG.debug().$("opening for rw [path=").$(path).I$();
    try (MemoryMARW txMem = migrationContext.createRwMemoryOf(ff, path.$())) {
        long tempMem8b = migrationContext.getTempMemory(8);
        MemoryARW txFileUpdate = migrationContext.getTempVirtualMem();
        txFileUpdate.jumpTo(0);
        int symbolColumnCount = txMem.getInt(MigrationActions.TX_OFFSET_MAP_WRITER_COUNT_505);
        for (int i = 0; i < symbolColumnCount; i++) {
            final int symbolCount = txMem.getInt(MigrationActions.prefixedBlockOffset(MigrationActions.TX_OFFSET_MAP_WRITER_COUNT_505, i + 1L, Integer.BYTES));
            txFileUpdate.putInt(symbolCount);
            txFileUpdate.putInt(symbolCount);
        }
        // Set partition segment size as 0 for now
        long partitionSegmentOffset = txFileUpdate.getAppendOffset();
        txFileUpdate.putInt(0);
        final int partitionBy = TableUtils.readIntOrFail(ff, migrationContext.getMetadataFd(), TX_STRUCT_UPDATE_1_META_OFFSET_PARTITION_BY, tempMem8b, path);
        if (partitionBy != PartitionBy.NONE) {
            path.trimTo(pathDirLen);
            writeAttachedPartitions(ff, tempMem8b, path, txMem, partitionBy, symbolColumnCount, txFileUpdate);
        }
        long updateSize = txFileUpdate.getAppendOffset();
        long partitionSegmentSize = updateSize - partitionSegmentOffset - Integer.BYTES;
        txFileUpdate.putInt(partitionSegmentOffset, (int) partitionSegmentSize);
        // Save txFileUpdate to tx file starting at LOCAL_TX_OFFSET_MAP_WRITER_COUNT + 4
        long writeOffset = MigrationActions.TX_OFFSET_MAP_WRITER_COUNT_505 + Integer.BYTES;
        txMem.jumpTo(writeOffset);
        for (int i = 0, size = 1; i < size && updateSize > 0; i++) {
            long writeSize = Math.min(updateSize, txFileUpdate.getPageSize());
            txMem.putBlockOfBytes(txFileUpdate.getPageAddress(i), writeSize);
            updateSize -= writeSize;
        }
        assert updateSize == 0;
    }
}
Also used : Path(io.questdb.std.str.Path) FilesFacade(io.questdb.std.FilesFacade) MemoryMARW(io.questdb.cairo.vm.api.MemoryMARW) MemoryARW(io.questdb.cairo.vm.api.MemoryARW)

Aggregations

MemoryARW (io.questdb.cairo.vm.api.MemoryARW)52 Test (org.junit.Test)46 Path (io.questdb.std.str.Path)4 CairoException (io.questdb.cairo.CairoException)2 MemoryMARW (io.questdb.cairo.vm.api.MemoryMARW)2 TestBinarySequence (io.questdb.griffin.engine.TestBinarySequence)2 TestRecord (io.questdb.cairo.TestRecord)1 RecordMetadata (io.questdb.cairo.sql.RecordMetadata)1 MemoryCMRImpl (io.questdb.cairo.vm.MemoryCMRImpl)1 MemoryMA (io.questdb.cairo.vm.api.MemoryMA)1 MemoryMR (io.questdb.cairo.vm.api.MemoryMR)1 FilesFacade (io.questdb.std.FilesFacade)1 StringSink (io.questdb.std.str.StringSink)1