Search in sources :

Example 1 with SkipIndexAttributes

use of com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes in project ats-framework by Axway.

the class DatabaseSnapshot method stripIndexAttributes.

/**
 * After we have loaded all table indexes, here we remove the not wanted attributes.
 *
 * The cleaner way would be to not even load all the not wanted attributes, but in such case
 * we would have to pass all those attributes down to all DB provided implementations.
 *
 * @param tables
 */
private void stripIndexAttributes(List<TableDescription> tables) {
    // cycle all tables
    for (TableDescription table : tables) {
        SkipIndexAttributes skipIndexAttributes = this.skipIndexAttributesPerTable.get(table.getName().toLowerCase());
        if (skipIndexAttributes != null) {
            // there is some index attribute to be skipped for this table
            // cycle all indexes of this table
            Map<String, String> indexes = table.getIndexes();
            for (String indexName : indexes.keySet()) {
                List<String> attributes = skipIndexAttributes.getAttributesToSkip(indexName.toLowerCase());
                if (attributes != null) {
                    // there is some attribute to be skipped for this index
                    // split the index description string into tokens of attributes
                    String indexDescription = indexes.get(indexName);
                    String[] tokens = indexDescription.split(",");
                    // we will update the index description without the stripped attributes
                    indexDescription = "";
                    boolean firstTime = true;
                    for (String token : tokens) {
                        if (!StringUtils.isNullOrEmpty(token)) {
                            boolean attributeFound = false;
                            for (String attribute : attributes) {
                                if (token.trim().toLowerCase().startsWith(attribute.toLowerCase() + "=")) {
                                    attributeFound = true;
                                    break;
                                }
                            }
                            if (!attributeFound) {
                                if (firstTime) {
                                    firstTime = false;
                                    indexDescription += token.trim();
                                } else {
                                    indexDescription += ", " + token.trim();
                                }
                            }
                        }
                    }
                    indexes.put(indexName, indexDescription);
                }
            }
        }
    }
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription)

Example 2 with SkipIndexAttributes

use of com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes in project ats-framework by Axway.

the class DatabaseSnapshot method skipIndexAttributes.

/**
 * Allows skipping attributes of some index of some table
 *
 * @param table the table
 * @param index the index
 * @param attribute the index attribute name
 */
@PublicAtsApi
public void skipIndexAttributes(String table, String index, String attribute) {
    SkipIndexAttributes skipIndexAttributes = this.skipIndexAttributesPerTable.get(table.toLowerCase());
    if (skipIndexAttributes == null) {
        skipIndexAttributes = new SkipIndexAttributes(table.toLowerCase());
        this.skipIndexAttributesPerTable.put(table.toLowerCase(), skipIndexAttributes);
    }
    skipIndexAttributes.setAttributeToSkip(index.toLowerCase(), attribute);
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with SkipIndexAttributes

use of com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes in project ats-framework by Axway.

the class DatabaseSnapshotBackupUtils method loadFromFile.

/**
 * Load a snapshot from a file
 *
 * @param newSnapshotName the name of the new snapshot
 * @param snapshot the snapshot instance to fill with new data
 * @param sourceFile the backup file name
 * @return the XML document
 */
public Document loadFromFile(String newSnapshotName, DatabaseSnapshot snapshot, String sourceFile) {
    log.info("Load database snapshot from file " + sourceFile + " - START");
    // first clean up the current instance, in case some snapshot was taken before
    snapshot.tables.clear();
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(sourceFile));
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error reading database snapshot backup file " + sourceFile, e);
    }
    Element databaseNode = doc.getDocumentElement();
    if (!DatabaseSnapshotUtils.NODE_DB_SNAPSHOT.equals(databaseNode.getNodeName())) {
        throw new DatabaseSnapshotException("Bad backup file. Root node name is expeced to be '" + DatabaseSnapshotUtils.NODE_DB_SNAPSHOT + "', but it is '" + databaseNode.getNodeName() + "'");
    }
    if (StringUtils.isNullOrEmpty(newSnapshotName)) {
        snapshot.name = databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME);
    } else {
        // user wants to change the snapshot name
        snapshot.name = newSnapshotName;
    }
    // the timestamps
    snapshot.metadataTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME));
    snapshot.contentTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME));
    // the tables
    List<Element> tableNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_TABLE);
    for (Element tableNode : tableNodes) {
        snapshot.tables.add(TableDescription.fromXmlNode(snapshot.name, tableNode));
    }
    // any skip table content rules
    snapshot.skipContentPerTable.clear();
    List<Element> skipContentNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_CONTENT);
    for (Element skipContentNode : skipContentNodes) {
        SkipContent skipContent = SkipContent.fromXmlNode(skipContentNode);
        snapshot.skipContentPerTable.put(skipContent.getTable().toLowerCase(), skipContent);
    }
    // any skip table column rules
    snapshot.skipColumnsPerTable.clear();
    List<Element> skipColumnNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_COLUMNS);
    for (Element skipColumnNode : skipColumnNodes) {
        SkipColumns skipColumns = SkipColumns.fromXmlNode(skipColumnNode);
        snapshot.skipColumnsPerTable.put(skipColumns.getTable().toLowerCase(), skipColumns);
    }
    // any skip index attribute rules
    snapshot.skipIndexAttributesPerTable.clear();
    List<Element> skipIndexAttributesNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_INDEX_ATTRIBUTES);
    for (Element skipIndexAttributesNode : skipIndexAttributesNodes) {
        SkipIndexAttributes skipIndexAttributes = SkipIndexAttributes.fromXmlNode(skipIndexAttributesNode);
        snapshot.skipIndexAttributesPerTable.put(skipIndexAttributes.getTable().toLowerCase(), skipIndexAttributes);
    }
    // any skip table row rules
    snapshot.skipRowsPerTable.clear();
    List<Element> skipRowNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_ROWS);
    for (Element skipRowNode : skipRowNodes) {
        SkipRows skipRows = SkipRows.fromXmlNode(skipRowNode);
        snapshot.skipRowsPerTable.put(skipRows.getTable().toLowerCase(), skipRows);
    }
    log.info("Load database snapshot from file " + sourceFile + " - END");
    return doc;
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) Element(org.w3c.dom.Element) SkipContent(com.axway.ats.action.dbaccess.snapshot.rules.SkipContent) Document(org.w3c.dom.Document) File(java.io.File) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) SkipColumns(com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)

Example 4 with SkipIndexAttributes

use of com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes in project ats-framework by Axway.

the class DatabaseSnapshotBackupUtils method saveToFile.

/**
 * Save a snapshot into a file
 * @param snapshot the snapshot to save
 * @param backupFile the backup file name
 * @return the XML document
 */
public Document saveToFile(DatabaseSnapshot snapshot, String backupFile) {
    log.info("Save database snapshot into file " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element dbNode = doc.createElement(DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME, snapshot.name);
    // the timestamp comes when user takes a snapshot from database
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
    // the timestamp now
    snapshot.contentTimestamp = System.currentTimeMillis();
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
    doc.appendChild(dbNode);
    // append all table data
    for (TableDescription tableDescription : snapshot.tables) {
        Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
        // append table meta data
        dbNode.appendChild(tableNode);
        tableDescription.toXmlNode(doc, tableNode);
        // check if table content is to be skipped
        SkipContent skipTableContentOption = snapshot.skipContentPerTable.get(tableDescription.getName().toLowerCase());
        if (skipTableContentOption != null) {
            // we skip the table content
            if (skipTableContentOption.isRememberNumberOfRows()) {
                // ... but we want to persist the number of rows
                int numberRows = snapshot.loadTableLength(snapshot.name, tableDescription, null, null);
                tableNode.setAttribute(DatabaseSnapshotUtils.ATTR_TABLE_NUMBER_ROWS, String.valueOf(numberRows));
            }
            continue;
        }
        // append table content
        List<String> valuesList = snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipColumnsPerTable, snapshot.skipRowsPerTable, null, null);
        for (String values : valuesList) {
            Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
            rowNode.setTextContent(StringUtils.escapeNonPrintableAsciiCharacters(values));
            tableNode.appendChild(rowNode);
        }
    }
    // append any skip table content rules
    for (SkipContent skipContent : snapshot.skipContentPerTable.values()) {
        skipContent.toXmlNode(doc, dbNode);
    }
    // append any skip table column rules
    for (SkipColumns skipColumns : snapshot.skipColumnsPerTable.values()) {
        skipColumns.toXmlNode(doc, dbNode);
    }
    // append any skip index attribute rules
    for (SkipIndexAttributes skipIndexAttributes : snapshot.skipIndexAttributesPerTable.values()) {
        skipIndexAttributes.toXmlNode(doc, dbNode);
    }
    // append any skip table row rules
    for (SkipRows skipRows : snapshot.skipRowsPerTable.values()) {
        skipRows.toXmlNode(doc, dbNode);
    }
    // save the XML file
    OutputStream fos = null;
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        fos = new FileOutputStream(new File(backupFile));
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(doc);
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error saving " + backupFile, e);
    } finally {
        IoUtils.closeStream(fos, "Error closing IO stream to file used for database snapshot backup " + backupFile);
    }
    log.info("Save database snapshot into file " + backupFile + " - END");
    return doc;
}
Also used : SkipIndexAttributes(com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes) SkipRows(com.axway.ats.action.dbaccess.snapshot.rules.SkipRows) XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) SkipContent(com.axway.ats.action.dbaccess.snapshot.rules.SkipContent) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) SkipColumns(com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)

Aggregations

SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)4 SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)2 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)2 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)2 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)2 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)2 File (java.io.File)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1