Search in sources :

Example 6 with FileSnapshot

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

the class FileSyncZipDataWriter method end.

public void end(Batch batch, boolean inError) {
    try {
        if (!inError) {
            if (zos == null) {
                zos = new ZipOutputStream(stagedResource.getOutputStream());
            }
            FileSyncZipScript script = createFileSyncZipScript(batch.getTargetNodeId());
            script.buildScriptStart(batch);
            Map<String, LastEventType> entries = new HashMap<String, LastEventType>();
            for (FileSnapshot snapshot : snapshotEvents) {
                FileTriggerRouter triggerRouter = fileSyncService.getFileTriggerRouter(snapshot.getTriggerId(), snapshot.getRouterId());
                if (triggerRouter != null) {
                    LastEventType eventType = snapshot.getLastEventType();
                    FileTrigger fileTrigger = triggerRouter.getFileTrigger();
                    String targetBaseDir = ((triggerRouter.getTargetBaseDir() == null) ? null : triggerRouter.getTargetBaseDir().replace('\\', '/'));
                    if (StringUtils.isBlank(targetBaseDir)) {
                        targetBaseDir = ((fileTrigger.getBaseDir() == null) ? null : fileTrigger.getBaseDir().replace('\\', '/'));
                    }
                    targetBaseDir = StringEscapeUtils.escapeJava(targetBaseDir);
                    StringBuilder entryName = new StringBuilder(Long.toString(batch.getBatchId()));
                    entryName.append("/");
                    if (!snapshot.getRelativeDir().equals(".")) {
                        entryName.append(snapshot.getRelativeDir()).append("/");
                    }
                    entryName.append(snapshot.getFileName());
                    File file = fileTrigger.createSourceFile(snapshot);
                    if (file.isDirectory()) {
                        entryName.append("/");
                    }
                    String targetFile = "targetBaseDir + \"/\" + targetRelativeDir + \"/\" + targetFileName";
                    LastEventType previousEventForEntry = entries.get(entryName.toString());
                    boolean process = true;
                    if (previousEventForEntry != null) {
                        if ((previousEventForEntry == eventType) || (previousEventForEntry == LastEventType.CREATE && eventType == LastEventType.MODIFY)) {
                            process = false;
                        }
                    }
                    if (process) {
                        if (eventType != LastEventType.DELETE) {
                            if (file.exists()) {
                                byteCount += file.length();
                                ZipEntry entry = new ZipEntry(entryName.toString());
                                entry.setSize(file.length());
                                entry.setTime(file.lastModified());
                                zos.putNextEntry(entry);
                                if (file.isFile()) {
                                    FileInputStream fis = new FileInputStream(file);
                                    try {
                                        IOUtils.copy(fis, zos);
                                    } finally {
                                        IOUtils.closeQuietly(fis);
                                    }
                                }
                                zos.closeEntry();
                                entries.put(entryName.toString(), eventType);
                            } else {
                                log.warn("Could not find the {} file to package for synchronization.  Skipping it.", file.getAbsolutePath());
                            }
                        }
                        script.buildScriptFileSnapshot(batch, snapshot, triggerRouter, fileTrigger, file, targetBaseDir, targetFile);
                    }
                } else {
                    log.error("Could not locate the file trigger ({}) router ({}) to process a snapshot event.  The event will be ignored", snapshot.getTriggerId(), snapshot.getRouterId());
                }
            }
            script.buildScriptEnd(batch);
            ZipEntry entry = new ZipEntry(batch.getBatchId() + "/" + script.getScriptFileName(batch));
            zos.putNextEntry(entry);
            IOUtils.write(script.getScript().toString(), zos);
            zos.closeEntry();
            entry = new ZipEntry(batch.getBatchId() + "/batch-info.txt");
            zos.putNextEntry(entry);
            IOUtils.write(batch.getChannelId(), zos);
            zos.closeEntry();
        }
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) LastEventType(org.jumpmind.symmetric.model.FileSnapshot.LastEventType) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) ZipOutputStream(java.util.zip.ZipOutputStream) IoException(org.jumpmind.exception.IoException) File(java.io.File)

Example 7 with FileSnapshot

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

the class FileSyncZipDataWriter method write.

public void write(CsvData data) {
    DataEventType eventType = data.getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE) {
        Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
        Map<String, String> oldColumnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.OLD_DATA);
        FileSnapshot snapshot = new FileSnapshot();
        snapshot.setTriggerId(columnData.get("TRIGGER_ID"));
        snapshot.setRouterId(columnData.get("ROUTER_ID"));
        snapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
        snapshot.setCrc32Checksum(Long.parseLong(columnData.get("CRC32_CHECKSUM")));
        String oldChecksum = oldColumnData.get("CRC32_CHECKSUM");
        if (StringUtils.isNotBlank(oldChecksum)) {
            snapshot.setOldCrc32Checksum(Long.parseLong(oldChecksum));
        }
        snapshot.setFileSize(Long.parseLong(columnData.get("FILE_SIZE")));
        snapshot.setLastUpdateBy(columnData.get("LAST_UPDATE_BY"));
        snapshot.setFileName(columnData.get("FILE_NAME"));
        snapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
        snapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
        snapshotEvents.add(snapshot);
    } else if (eventType == DataEventType.RELOAD) {
        String targetNodeId = context.getBatch().getTargetNodeId();
        Node targetNode = nodeService.findNode(targetNodeId);
        List<FileTriggerRouter> fileTriggerRouters = fileSyncService.getFileTriggerRoutersForCurrentNode();
        for (FileTriggerRouter fileTriggerRouter : fileTriggerRouters) {
            if (fileTriggerRouter.isEnabled() && fileTriggerRouter.isInitialLoadEnabled() && fileTriggerRouter.getRouter().getNodeGroupLink().getTargetNodeGroupId().equals(targetNode.getNodeGroupId())) {
                DirectorySnapshot directorySnapshot = fileSyncService.getDirectorySnapshot(fileTriggerRouter);
                snapshotEvents.addAll(directorySnapshot);
            }
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) Node(org.jumpmind.symmetric.model.Node) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with FileSnapshot

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

the class DirectorySnapshot method diff.

public DirectorySnapshot diff(DirectorySnapshot anotherSnapshot) {
    DirectorySnapshot differences = new DirectorySnapshot(anotherSnapshot.getFileTriggerRouter());
    for (FileSnapshot anotherFile : anotherSnapshot) {
        boolean found = false;
        for (FileSnapshot file : this) {
            if (anotherFile.sameFile(file)) {
                found = true;
                if ((file.getLastEventType() == LastEventType.MODIFY || file.getLastEventType() == LastEventType.CREATE) && anotherFile.getLastEventType() == LastEventType.CREATE) {
                    file.setLastEventType(LastEventType.MODIFY);
                    anotherFile.setLastEventType(LastEventType.MODIFY);
                }
                if (!anotherFile.equals(file)) {
                    differences.add(anotherFile);
                }
            }
        }
        if (!found) {
            differences.add(anotherFile);
        }
    }
    for (FileSnapshot file : this) {
        boolean found = false;
        for (FileSnapshot anotherFile : anotherSnapshot) {
            if (anotherFile.sameFile(file)) {
                found = true;
            }
        }
        if (file.getLastEventType() != LastEventType.DELETE && !found) {
            FileSnapshot copy = new FileSnapshot(file);
            copy.setLastEventType(LastEventType.DELETE);
            differences.add(copy);
        }
    }
    return differences;
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot)

Example 9 with FileSnapshot

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

the class DirectorySnapshot method merge.

protected void merge(DirectorySnapshot snapshot) {
    Set<FileSnapshot> toAdd = new HashSet<FileSnapshot>();
    Set<FileSnapshot> toRemove = new HashSet<FileSnapshot>();
    for (FileSnapshot fileChange : snapshot) {
        for (FileSnapshot file : this) {
            if (fileChange.getFileName().equals(file.getFileName())) {
                toRemove.add(file);
                if (fileChange.getLastEventType() == LastEventType.MODIFY) {
                    toAdd.add(fileChange);
                }
            }
        }
    }
    for (FileSnapshot fileChange : toRemove) {
        if (fileChange.getLastEventType() == LastEventType.CREATE) {
            toAdd.add(fileChange);
        }
    }
    this.removeAll(toRemove);
    this.addAll(toAdd);
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) HashSet(java.util.HashSet)

Example 10 with FileSnapshot

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

the class FileTriggerFileModifiedListener method addSnapshot.

protected void addSnapshot(File file, LastEventType lastEventType, boolean isDir) {
    fileCount++;
    processInfo.incrementCurrentDataCount();
    FileSnapshot fileSnapshot = new FileSnapshot(fileTriggerRouter, file, lastEventType, useCrc);
    DirectorySnapshot modifiedDir = modifiedDirs.get(fileSnapshot.getRelativeDir());
    if (!isDir && modifiedDir != null) {
        // This file belongs to a directory that had a file add/delete, so we will process the directory later
        modifiedDir.add(fileSnapshot);
    } else {
        long lastModified = fileSnapshot.getFileModifiedTime();
        if ((fromDate != null && lastModified > fromDate.getTime()) && lastModified <= toDate.getTime()) {
            if (isDir) {
                // This is a directory that had a file add/delete, so we'll need to look for deletes later
                modifiedDirs.put(fileSnapshot.getRelativeDir() + "/" + fileSnapshot.getFileName(), new DirectorySnapshot(fileTriggerRouter));
            } else {
                snapshot.add(fileSnapshot);
                changeCount++;
                if (snapshot.size() >= fileModifiedCallback.getCommitSize()) {
                    commit();
                }
            }
        }
    }
    if (System.currentTimeMillis() - ts > 60000) {
        log.info("File tracker has been processing for {} seconds.  The following stats have been gathered: {}", new Object[] { (System.currentTimeMillis() - startTime) / 1000, "{ fileCount=" + fileCount + ", fileChangeCount=" + changeCount + " }" });
        ts = System.currentTimeMillis();
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot)

Aggregations

FileSnapshot (org.jumpmind.symmetric.model.FileSnapshot)11 FileTriggerRouter (org.jumpmind.symmetric.model.FileTriggerRouter)5 File (java.io.File)4 FileTrigger (org.jumpmind.symmetric.model.FileTrigger)4 ArrayList (java.util.ArrayList)3 Table (org.jumpmind.db.model.Table)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 IOException (java.io.IOException)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 IoException (org.jumpmind.exception.IoException)1 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)1