Search in sources :

Example 1 with DaemonManager

use of com.laytonsmith.PureUtilities.DaemonManager 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 DaemonManager

use of com.laytonsmith.PureUtilities.DaemonManager 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 DaemonManager

use of com.laytonsmith.PureUtilities.DaemonManager 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 DaemonManager

use of com.laytonsmith.PureUtilities.DaemonManager in project CommandHelper by EngineHub.

the class TestPersistence method setUp.

@Before
public void setUp() throws Exception {
    testData.put(new String[] { "a", "b" }, "value1");
    testData.put(new String[] { "a", "b", "c1" }, "value2");
    testData.put(new String[] { "a", "b", "c2" }, "value3");
    options = new ConnectionMixinFactory.ConnectionMixinOptions();
    options.setWorkingDirectory(new File("."));
    dm = new DaemonManager();
    new File("folder").mkdir();
}
Also used : DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) ConnectionMixinFactory(com.laytonsmith.persistence.io.ConnectionMixinFactory) File(java.io.File) Before(org.junit.Before)

Example 5 with DaemonManager

use of com.laytonsmith.PureUtilities.DaemonManager in project CommandHelper by EngineHub.

the class Manager method doAddEdit.

public static boolean doAddEdit(String key, String valueScript) {
    try {
        Environment env = Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
        Construct c = MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex(valueScript, null, true)), env, null, null);
        String value = Construct.json_encode(c, Target.UNKNOWN);
        pl(CYAN + "Adding: " + WHITE + value);
        String[] k = key.split("\\.");
        DaemonManager dm = new DaemonManager();
        persistenceNetwork.set(dm, k, value);
        try {
            dm.waitForThreads();
        } catch (InterruptedException e) {
        // 
        }
        return true;
    } catch (Exception ex) {
        pl(RED + ex.getMessage());
        return false;
    }
}
Also used : DaemonManager(com.laytonsmith.PureUtilities.DaemonManager) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) URISyntaxException(java.net.URISyntaxException) ReadOnlyException(com.laytonsmith.persistence.ReadOnlyException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) DataSourceException(com.laytonsmith.persistence.DataSourceException) IOException(java.io.IOException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException)

Aggregations

DaemonManager (com.laytonsmith.PureUtilities.DaemonManager)8 IOException (java.io.IOException)7 DataSourceException (com.laytonsmith.persistence.DataSourceException)6 ReadOnlyException (com.laytonsmith.persistence.ReadOnlyException)6 ConnectionMixinFactory (com.laytonsmith.persistence.io.ConnectionMixinFactory)5 URISyntaxException (java.net.URISyntaxException)5 File (java.io.File)4 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)3 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)3 URI (java.net.URI)3 DataSource (com.laytonsmith.persistence.DataSource)2 PersistenceNetwork (com.laytonsmith.persistence.PersistenceNetwork)2 HashMap (java.util.HashMap)2 ArgumentParser (com.laytonsmith.PureUtilities.ArgumentParser)1 ArgumentSuite (com.laytonsmith.PureUtilities.ArgumentSuite)1 ClassDiscovery (com.laytonsmith.PureUtilities.ClassLoading.ClassDiscovery)1 ClassDiscoveryCache (com.laytonsmith.PureUtilities.ClassLoading.ClassDiscoveryCache)1 CommandExecutor (com.laytonsmith.PureUtilities.CommandExecutor)1 RSAEncrypt (com.laytonsmith.PureUtilities.Common.RSAEncrypt)1 CString (com.laytonsmith.core.constructs.CString)1