Search in sources :

Example 1 with ReadOnlyException

use of com.laytonsmith.persistence.ReadOnlyException in project CommandHelper by EngineHub.

the class Manager method refactor.

public static void refactor() {
    pl("This tool allows you to granularly move individual keys from one datasource to another." + " Unlike the merge tool, this works with individual keys, not necessarily keys that are" + " within a particular data source. There are three required inputs, the transfer key pattern," + " the input configuration file, and the output configuration file. Data is transferred from" + " one configuration to the other, that is, it is added in the new place, and removed in the old place." + " This tool is more complicated" + " than the merge tool, so consider using the other tool for simple tasks.\n\n");
    pl("Would you like to continue? [" + GREEN + "Y" + WHITE + "/" + RED + "N" + WHITE + "]");
    String choice = prompt();
    if ("Y".equals(choice)) {
        String filter;
        File input;
        File output;
        while (true) {
            while (true) {
                pl("What keys are you interested in transferring? The filter should be in the same format as the persistence.ini file, i.e." + " \"storage.test\" or \"storage.test.**\". If a wildcard is used, multiple keys may be moved, otherwise, only one will" + " be.");
                filter = prompt();
                break;
            }
            File def = MethodScriptFileLocations.getDefault().getPersistenceConfig();
            while (true) {
                pl("What is the input configuration (where keys will be read in from, then deleted)? Leave blank for the default, which is " + def.toString() + ". The path should be relative to " + jarLocation.toString());
                String sinput = prompt();
                if ("".equals(sinput.trim())) {
                    input = def;
                } else {
                    File temp = new File(sinput);
                    if (!temp.isAbsolute()) {
                        temp = new File(jarLocation, sinput);
                    }
                    input = temp;
                }
                if (!input.exists() || !input.isFile()) {
                    pl(RED + input.toString() + " isn't a file. Please enter an existing file.");
                } else {
                    break;
                }
            }
            while (true) {
                pl("What is the output configuration (where keys will be written to)? The path should be relative to " + jarLocation.toString());
                String soutput = prompt();
                if ("".equals(soutput.trim())) {
                    pl(RED + "The output cannot be empty");
                    continue;
                } else {
                    File temp = new File(soutput);
                    if (!temp.isAbsolute()) {
                        temp = new File(jarLocation, soutput);
                    }
                    output = temp;
                }
                if (!output.exists() || !output.isFile()) {
                    pl(RED + output.toString() + " isn't a file. Please enter an existing file.");
                } else {
                    break;
                }
            }
            pl("The filter is \"" + MAGENTA + filter + WHITE + "\".");
            pl("The input configuration is \"" + MAGENTA + input.toString() + WHITE + "\".");
            pl("The output configuration is \"" + MAGENTA + output.toString() + WHITE + "\".");
            pl("Is this correct? [" + GREEN + "Y" + WHITE + "/" + RED + "N" + WHITE + "]");
            if ("Y".equals(prompt())) {
                break;
            }
        }
        pl(YELLOW + "Now beginning transfer...");
        URI defaultURI;
        try {
            defaultURI = new URI("file://persistence.db");
        } catch (URISyntaxException ex) {
            throw new Error(ex);
        }
        ConnectionMixinFactory.ConnectionMixinOptions mixinOptions = new ConnectionMixinFactory.ConnectionMixinOptions();
        try {
            DaemonManager dm = new DaemonManager();
            mixinOptions.setWorkingDirectory(chDirectory);
            PersistenceNetwork pninput = new PersistenceNetwork(input, defaultURI, mixinOptions);
            PersistenceNetwork pnoutput = new PersistenceNetwork(output, defaultURI, mixinOptions);
            Pattern p = Pattern.compile(DataSourceFilter.toRegex(filter));
            Map<String[], String> inputData = pninput.getNamespace(new String[] {});
            boolean errors = false;
            int transferred = 0;
            int skipped = 0;
            for (String[] k : inputData.keySet()) {
                String key = StringUtils.Join(k, ".");
                if (p.matcher(key).matches()) {
                    pl(GREEN + "transferring " + YELLOW + key);
                    // from the input network
                    if (pnoutput.getKeySource(k).equals(pninput.getKeySource(k))) {
                        // Don't transfer it if it's the same source, otherwise we would
                        continue;
                    // end up just deleting it.
                    }
                    try {
                        pnoutput.set(dm, k, inputData.get(k));
                        transferred++;
                        try {
                            pninput.clearKey(dm, k);
                        } catch (ReadOnlyException ex) {
                            pl(RED + "Could not clear out original key for the value for \"" + MAGENTA + StringUtils.Join(k, ".") + RED + "\", as the input" + " file is set to read only.");
                            errors = true;
                        }
                    } catch (ReadOnlyException ex) {
                        pl(RED + "Could not write out the value for \"" + MAGENTA + StringUtils.Join(k, ".") + RED + "\", as the output" + " file is set to read only.");
                        errors = true;
                    }
                } else {
                    skipped++;
                }
            }
            pl(YELLOW + StringUtils.PluralTemplateHelper(transferred, "%d key was", "%d keys were") + " transferred.");
            pl(YELLOW + StringUtils.PluralTemplateHelper(skipped, "%d key was", "%d keys were") + " skipped.");
            if (errors) {
                pl(YELLOW + "Other than the errors listed above, all keys were transferred successfully.");
            } else {
                pl(GREEN + "Done!");
            }
            pl(GREEN + "If this is being done as part of an entire transfer process, don't forget to set " + output.toString() + " as your main Persistence Network configuration file.");
        } catch (IOException | DataSourceException ex) {
            Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) URISyntaxException(java.net.URISyntaxException) PersistenceNetwork(com.laytonsmith.persistence.PersistenceNetwork) IOException(java.io.IOException) DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) URI(java.net.URI) DataSourceException(com.laytonsmith.persistence.DataSourceException) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) File(java.io.File) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException)

Example 2 with ReadOnlyException

use of com.laytonsmith.persistence.ReadOnlyException in project CommandHelper by EngineHub.

the class Manager method cleardb.

public static void cleardb() {
    try {
        pl(RED + "Are you absolutely sure you want to clear out your database? " + BLINKON + "No backup is going to be made." + BLINKOFF);
        pl(WHITE + "This will completely wipe your persistence information out. (No other data will be changed)");
        pl("[YES/No]");
        String choice = prompt();
        if (choice.equals("YES")) {
            pl("Positive? [YES/No]");
            if (prompt().equals("YES")) {
                p("Ok, here we go... ");
                Set<String[]> keySet = persistenceNetwork.getNamespace(new String[] {}).keySet();
                DaemonManager dm = new DaemonManager();
                for (String[] key : keySet) {
                    try {
                        persistenceNetwork.clearKey(dm, key);
                    } catch (ReadOnlyException ex) {
                        pl(RED + "Read only data source found: " + ex.getMessage());
                    }
                }
                try {
                    dm.waitForThreads();
                } catch (InterruptedException e) {
                // 
                }
                pl("Done!");
            }
        } else if (choice.equalsIgnoreCase("yes")) {
            pl("No, you have to type YES exactly.");
        }
    } catch (DataSourceException | IOException ex) {
        pl(RED + ex.getMessage());
    }
}
Also used : DataSourceException(com.laytonsmith.persistence.DataSourceException) DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) IOException(java.io.IOException) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException)

Example 3 with ReadOnlyException

use of com.laytonsmith.persistence.ReadOnlyException in project CommandHelper by EngineHub.

the class Manager method hiddenKeys.

public static void hiddenKeys() {
    String action;
    while (true) {
        pl(WHITE + "Would you like to \"" + GREEN + "view" + WHITE + "\" or \"" + RED + "delete" + WHITE + "\" the hidden keys (default: view)? [view/delete]");
        action = prompt();
        if ("".equals(action)) {
            action = "view";
        }
        if ("view".equals(action) || "delete".equals(action)) {
            break;
        } else {
            pl(RED + "Invalid selection.");
        }
    }
    File configuration = MethodScriptFileLocations.getDefault().getPersistenceConfig();
    while (true) {
        pl("Currently, " + configuration.getAbsolutePath() + " is being used as the persistence config file, but you may" + " specify another (blank to use the default).");
        String file = prompt();
        if ("".equals(file)) {
            break;
        } else {
            File f = new File(file);
            if (f.exists()) {
                configuration = f;
                break;
            } else {
                pl(RED + "The file you specified doesn't seem to exist, please enter it again.");
            }
        }
    }
    pl(YELLOW + "Using " + configuration.getAbsolutePath() + " as our Persistence Network config.");
    File workingDirectory = MethodScriptFileLocations.getDefault().getConfigDirectory();
    while (true) {
        pl("Currently, " + workingDirectory.getAbsolutePath() + " is being used as the default \"working directory\" for the" + " persistence config file, but you may specify another (blank to use the default).");
        String file = prompt();
        if ("".equals(file)) {
            break;
        } else {
            File f = new File(file);
            if (f.exists()) {
                workingDirectory = f;
                break;
            } else {
                pl(RED + "The file you specified doesn't seem to exist, please enter it again.");
            }
        }
    }
    pl(YELLOW + "Using " + workingDirectory.getAbsolutePath() + " as our Persistence Network config working directory.");
    try {
        DataSourceFilter filter = new DataSourceFilter(configuration, new URI("sqlite://persistence.db"));
        ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
        options.setWorkingDirectory(workingDirectory);
        Set<URI> uris = filter.getAllConnections();
        boolean noneFound = true;
        int runningTotal = 0;
        for (URI uri : uris) {
            DataSource ds = DataSourceFactory.GetDataSource(uri, options);
            Map<String[], String> db = ds.getValues(ArrayUtils.EMPTY_STRING_ARRAY);
            Map<String[], String> map = new HashMap<>();
            DaemonManager dm = new DaemonManager();
            try {
                for (String[] key : db.keySet()) {
                    if (!filter.getConnection(key).equals(uri)) {
                        map.put(key, db.get(key));
                        if ("delete".equals(action)) {
                            ds.clearKey(dm, key);
                        }
                    }
                }
                runningTotal += map.size();
            } catch (ReadOnlyException ex) {
                pl(RED + "Cannot delete any keys from " + uri + " as it is marked as read only, so it is being skipped.");
            }
            if ("delete".equals(action)) {
                try {
                    dm.waitForThreads();
                } catch (InterruptedException ex) {
                // Ignored
                }
            }
            if (!map.isEmpty()) {
                noneFound = false;
                if ("view".equals(action)) {
                    pl("Found " + StringUtils.PluralTemplateHelper(map.size(), "one hidden key", "%d hidden keys") + " in data source " + MAGENTA + uri.toString());
                    for (String[] key : map.keySet()) {
                        pl("\t" + GREEN + StringUtils.Join(key, ".") + WHITE + ":" + CYAN + map.get(key));
                    }
                    if (ds.hasModifier(DataSource.DataSourceModifier.READONLY)) {
                        pl(YELLOW + "This data source is marked as read only, and the keys cannot be deleted from it by this utility.");
                    }
                    pl();
                }
            }
        }
        if (noneFound) {
            pl(GREEN + "Done searching, no hidden keys were found.");
        } else {
            if ("delete".equals(action)) {
                pl(GREEN + "Done, " + StringUtils.PluralTemplateHelper(runningTotal, "one hidden key was", "%d hidden keys were") + " deleted.");
            } else {
                pl(GREEN + "Found " + StringUtils.PluralTemplateHelper(runningTotal, "one hidden key", "%d hidden keys") + " in total.");
            }
        }
    } catch (URISyntaxException | IOException | DataSourceException ex) {
        pl(RED + ex.getMessage());
        ex.printStackTrace(StreamUtils.GetSystemErr());
    }
}
Also used : HashMap(java.util.HashMap) DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) DataSource(com.laytonsmith.persistence.DataSource) DataSourceException(com.laytonsmith.persistence.DataSourceException) DataSourceFilter(com.laytonsmith.persistence.DataSourceFilter) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) File(java.io.File) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException)

Example 4 with ReadOnlyException

use of com.laytonsmith.persistence.ReadOnlyException in project CommandHelper by EngineHub.

the class Manager method merge.

public static void merge() {
    cls();
    pl(GREEN + "Transferring a database takes all the keys from a database connection, and puts\n" + "them straight into another database. If there are key conflicts, this tool will prompt\n" + "you for an action.");
    ConnectionMixinFactory.ConnectionMixinOptions mixinOptions = new ConnectionMixinFactory.ConnectionMixinOptions();
    mixinOptions.setWorkingDirectory(chDirectory);
    DataSource source;
    DataSource destination;
    do {
        // the exact same value.
        do {
            // Get the source connection set up
            pl(YELLOW + "What is the source connection you would like to read the keys from?\n" + "(Type the connection exactly as you would in the persistence configuration,\n" + "aliases are not supported)");
            String ssource = prompt();
            try {
                source = DataSourceFactory.GetDataSource(ssource, mixinOptions);
                break;
            } catch (DataSourceException | URISyntaxException ex) {
                pl(RED + ex.getMessage());
            }
        } while (true);
        do {
            // Get the destination connection set up
            pl(YELLOW + "What is the destination connection?");
            String sdestination = prompt();
            try {
                destination = DataSourceFactory.GetDataSource(sdestination, mixinOptions);
                break;
            } catch (DataSourceException | URISyntaxException ex) {
                pl(RED + ex.getMessage());
            }
        } while (true);
        try {
            // Run through all the source's keys, and check to see that either the
            // destination's key doesn't exist, or the value at that key is the
            // exact same as the source's value
            boolean acceptAllDestination = false;
            boolean acceptAllSource = false;
            DaemonManager dm = new DaemonManager();
            for (String[] key : source.keySet(ArrayUtils.EMPTY_STRING_ARRAY)) {
                if (destination.hasKey(key)) {
                    if (!source.get(key).equals(destination.get(key))) {
                        String data;
                        // ask.
                        if (destination.get(key) != null) {
                            boolean useSource = false;
                            boolean useDestination = false;
                            if (acceptAllDestination || acceptAllSource) {
                                p(RED + "Conflict found for " + StringUtils.Join(key, ".") + ", using ");
                                if (useSource) {
                                    useSource = true;
                                    p("source");
                                } else {
                                    useDestination = true;
                                    p("destination");
                                }
                                pl(" value.");
                            } else {
                                pl(BG_RED + BRIGHT_WHITE + "The key " + StringUtils.Join(key, ".") + " has a different value" + " in the source and the destination: " + reset());
                                pl(WHITE + "Source: " + source.get(key));
                                pl(WHITE + "Destination: " + destination.get(key));
                                do {
                                    pl(YELLOW + "Would you like to keep " + CYAN + "S" + YELLOW + "ource, " + "keep " + GREEN + "D" + YELLOW + "estination, keep " + MAGENTA + "A" + YELLOW + "ll " + MAGENTA + "S" + YELLOW + "ource, or keep " + BLUE + "A" + YELLOW + "ll " + BLUE + "D" + YELLOW + "estination?");
                                    pl(WHITE + "[" + CYAN + "S" + WHITE + "/" + GREEN + "D" + WHITE + "/" + MAGENTA + "AS" + WHITE + "/" + BLUE + "AD" + WHITE + "]");
                                    String response = prompt();
                                    if ("AS".equalsIgnoreCase(response)) {
                                        acceptAllSource = true;
                                        useSource = true;
                                    } else if ("AD".equalsIgnoreCase(response)) {
                                        acceptAllDestination = true;
                                        useDestination = true;
                                    } else if ("S".equalsIgnoreCase(response)) {
                                        useSource = true;
                                    } else if ("D".equalsIgnoreCase(response)) {
                                        useDestination = true;
                                    } else {
                                        continue;
                                    }
                                    break;
                                } while (true);
                            }
                            if (useSource) {
                                data = source.get(key);
                            } else if (useDestination) {
                                data = destination.get(key);
                            } else {
                                throw new RuntimeException("Invalid state, both useSource and useDestination are false");
                            }
                        // Ok, now put the data in the destination
                        } else {
                            // Otherwise, just use the data in the source
                            data = source.get(key);
                        }
                        destination.set(dm, key, data);
                    }
                } else {
                    // Else there is no conflict, it's not in the destination.
                    destination.set(dm, key, source.get(key));
                }
            }
            try {
                dm.waitForThreads();
            } catch (InterruptedException ex) {
            // 
            }
            break;
        } catch (DataSourceException | ReadOnlyException | IOException ex) {
            pl(RED + ex.getMessage());
        }
    } while (true);
    pl(GREEN + "Done merging!");
}
Also used : DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DataSource(com.laytonsmith.persistence.DataSource) DataSourceException(com.laytonsmith.persistence.DataSourceException) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException)

Aggregations

DaemonManager (com.laytonsmith.PureUtilities.DaemonManager)4 DataSourceException (com.laytonsmith.persistence.DataSourceException)4 ReadOnlyException (com.laytonsmith.persistence.ReadOnlyException)4 IOException (java.io.IOException)4 ConnectionMixinFactory (com.laytonsmith.persistence.io.ConnectionMixinFactory)3 URISyntaxException (java.net.URISyntaxException)3 DataSource (com.laytonsmith.persistence.DataSource)2 File (java.io.File)2 URI (java.net.URI)2 TaskManager (com.laytonsmith.core.taskmanager.TaskManager)1 DataSourceFilter (com.laytonsmith.persistence.DataSourceFilter)1 PersistenceNetwork (com.laytonsmith.persistence.PersistenceNetwork)1 HashMap (java.util.HashMap)1 Pattern (java.util.regex.Pattern)1