Search in sources :

Example 1 with DataSourceFilter

use of com.laytonsmith.persistence.DataSourceFilter 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)

Aggregations

DaemonManager (com.laytonsmith.PureUtilities.DaemonManager)1 DataSource (com.laytonsmith.persistence.DataSource)1 DataSourceException (com.laytonsmith.persistence.DataSourceException)1 DataSourceFilter (com.laytonsmith.persistence.DataSourceFilter)1 ReadOnlyException (com.laytonsmith.persistence.ReadOnlyException)1 ConnectionMixinFactory (com.laytonsmith.persistence.io.ConnectionMixinFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1