Search in sources :

Example 1 with FileSnapshot

use of org.jumpmind.symmetric.model.FileSnapshot in project symmetric-ds by JumpMind.

the class AbstractFileParsingRouter method deleteFileIfNecessary.

public void deleteFileIfNecessary(DataMetaData dataMetaData) {
    Data data = dataMetaData.getData();
    Table snapshotTable = dataMetaData.getTable();
    if (data.getDataEventType() == DataEventType.INSERT || data.getDataEventType() == DataEventType.UPDATE) {
        List<File> filesToDelete = new ArrayList<File>();
        Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
        FileSnapshot fileSnapshot = new FileSnapshot();
        fileSnapshot.setTriggerId(columnData.get("TRIGGER_ID"));
        fileSnapshot.setRouterId(columnData.get("ROUTER_ID"));
        fileSnapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
        fileSnapshot.setFileName(columnData.get("FILE_NAME"));
        fileSnapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
        fileSnapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
        FileTriggerRouter triggerRouter = getEngine().getFileSyncService().getFileTriggerRouter(fileSnapshot.getTriggerId(), fileSnapshot.getRouterId());
        if (triggerRouter != null) {
            FileTrigger fileTrigger = triggerRouter.getFileTrigger();
            if (fileTrigger.isDeleteAfterSync()) {
                File file = fileTrigger.createSourceFile(fileSnapshot);
                if (!file.isDirectory()) {
                    filesToDelete.add(file);
                    if (fileTrigger.isSyncOnCtlFile()) {
                        File ctlFile = getEngine().getFileSyncService().getControleFile(file);
                        filesToDelete.add(ctlFile);
                    }
                }
            } else if (getEngine().getParameterService().is(ParameterConstants.FILE_SYNC_DELETE_CTL_FILE_AFTER_SYNC, false)) {
                File file = fileTrigger.createSourceFile(fileSnapshot);
                if (!file.isDirectory()) {
                    if (fileTrigger.isSyncOnCtlFile()) {
                        File ctlFile = getEngine().getFileSyncService().getControleFile(file);
                        filesToDelete.add(ctlFile);
                    }
                }
            }
        }
        if (filesToDelete != null && filesToDelete.size() > 0) {
            for (File file : filesToDelete) {
                if (file != null && file.exists()) {
                    log.debug("Deleting the '{}' file", file.getAbsolutePath());
                    boolean deleted = FileUtils.deleteQuietly(file);
                    if (!deleted) {
                        log.warn("Failed to 'delete on sync' the {} file", file.getAbsolutePath());
                    }
                }
                file = null;
            }
            filesToDelete = null;
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) Table(org.jumpmind.db.model.Table) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) File(java.io.File)

Example 2 with FileSnapshot

use of org.jumpmind.symmetric.model.FileSnapshot in project symmetric-ds by JumpMind.

the class FileSyncService method save.

public void save(List<FileSnapshot> changes) {
    if (changes != null) {
        ISqlTransaction sqlTransaction = null;
        try {
            sqlTransaction = sqlTemplate.startSqlTransaction();
            for (FileSnapshot fileSnapshot : changes) {
                save(sqlTransaction, fileSnapshot);
            }
            sqlTransaction.commit();
        } catch (Error ex) {
            if (sqlTransaction != null) {
                sqlTransaction.rollback();
            }
            throw ex;
        } catch (RuntimeException ex) {
            if (sqlTransaction != null) {
                sqlTransaction.rollback();
            }
            throw ex;
        } finally {
            close(sqlTransaction);
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) TargetError(bsh.TargetError) EvalError(bsh.EvalError)

Example 3 with FileSnapshot

use of org.jumpmind.symmetric.model.FileSnapshot in project symmetric-ds by JumpMind.

the class FileSyncService method acknowledgeFiles.

public void acknowledgeFiles(OutgoingBatch outgoingBatch) {
    log.debug("Acknowledging file_sync outgoing batch-{}", outgoingBatch.getBatchId());
    ISqlReadCursor<Data> cursor = engine.getDataService().selectDataFor(outgoingBatch.getBatchId(), outgoingBatch.getChannelId());
    Data data = null;
    List<File> filesToDelete = new ArrayList<File>();
    Table snapshotTable = platform.getTableFromCache(TableConstants.getTableName(tablePrefix, TableConstants.SYM_FILE_SNAPSHOT), false);
    for (int i = 0; i < outgoingBatch.getInsertEventCount(); i++) {
        data = cursor.next();
        if (data != null && (data.getDataEventType() == DataEventType.INSERT || data.getDataEventType() == DataEventType.UPDATE)) {
            Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
            FileSnapshot fileSnapshot = new FileSnapshot();
            fileSnapshot.setTriggerId(columnData.get("TRIGGER_ID"));
            fileSnapshot.setRouterId(columnData.get("ROUTER_ID"));
            fileSnapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
            fileSnapshot.setFileName(columnData.get("FILE_NAME"));
            fileSnapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
            fileSnapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
            FileTriggerRouter triggerRouter = this.getFileTriggerRouter(fileSnapshot.getTriggerId(), fileSnapshot.getRouterId());
            if (triggerRouter != null) {
                FileTrigger fileTrigger = triggerRouter.getFileTrigger();
                if (fileTrigger.isDeleteAfterSync()) {
                    File file = fileTrigger.createSourceFile(fileSnapshot);
                    if (!file.isDirectory()) {
                        filesToDelete.add(file);
                        if (fileTrigger.isSyncOnCtlFile()) {
                            filesToDelete.add(new File(file.getAbsolutePath() + ".ctl"));
                        }
                    }
                }
            }
        }
    }
    if (cursor != null) {
        cursor.close();
        cursor = null;
    }
    if (filesToDelete != null && filesToDelete.size() > 0) {
        for (File file : filesToDelete) {
            if (file != null && file.exists()) {
                log.debug("Deleting the '{}' file", file.getAbsolutePath());
                boolean deleted = FileUtils.deleteQuietly(file);
                if (!deleted) {
                    log.warn("Failed to 'delete on sync' the {} file", file.getAbsolutePath());
                }
            }
            file = null;
        }
        filesToDelete = null;
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) File(java.io.File)

Example 4 with FileSnapshot

use of org.jumpmind.symmetric.model.FileSnapshot in project symmetric-ds by JumpMind.

the class FileSyncZipDataWriter method start.

public void start(Batch batch) {
    this.batch = batch;
    this.statistics.put(batch, new Statistics());
    this.snapshotEvents = new ArrayList<FileSnapshot>();
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) Statistics(org.jumpmind.util.Statistics)

Example 5 with FileSnapshot

use of org.jumpmind.symmetric.model.FileSnapshot in project symmetric-ds by JumpMind.

the class FileTriggerTrackerTest method testTakeSnapshotRecursiveTestDelete.

@Test
public void testTakeSnapshotRecursiveTestDelete() throws Exception {
    FileTrigger fileTrigger = new FileTrigger(directory.getAbsolutePath(), true, null, null);
    Router router = new Router();
    FileTriggerRouter fileTriggerRouter = new FileTriggerRouter(fileTrigger, router);
    FileTriggerTracker tracker = new FileTriggerTracker(fileTriggerRouter, null);
    tracker.trackChanges();
    FileUtils.deleteQuietly(fileInDirectory1);
    DirectorySnapshot snapshot = tracker.trackChanges();
    assertEquals(1, snapshot.size());
    FileSnapshot change = snapshot.get(0);
    assertEquals(change.getFileName(), FileSyncUtils.getRelativePath(fileInDirectory1, directory));
    assertEquals(change.getLastEventType(), LastEventType.DELETE);
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) Router(org.jumpmind.symmetric.model.Router) Test(org.junit.Test)

Aggregations

FileSnapshot (org.jumpmind.symmetric.model.FileSnapshot)12 FileTriggerRouter (org.jumpmind.symmetric.model.FileTriggerRouter)6 File (java.io.File)5 FileTrigger (org.jumpmind.symmetric.model.FileTrigger)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 Table (org.jumpmind.db.model.Table)2 IoException (org.jumpmind.exception.IoException)2 CsvData (org.jumpmind.symmetric.io.data.CsvData)2 Data (org.jumpmind.symmetric.model.Data)2 EvalError (bsh.EvalError)1 TargetError (bsh.TargetError)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1