@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final Data data = checkData(qc); final String path = path(1, qc); final Item item = toItem(exprs[2], qc); final Options opts = toOptions(3, Q_OPTIONS, new Options(), qc); final Updates updates = qc.resources.updates(); final IntList docs = data.resources.docs(path); int d = 0; // delete binary resources final IOFile bin = data.meta.binary(path); if (bin == null || bin.isDir()) throw BXDB_REPLACE_X.get(info, path); if (item instanceof Bin) { updates.add(new DBStore(data, path, item, info), qc); } else { if (bin.exists()) updates.add(new DBDelete(data, path, info), qc); final NewInput input = checkInput(item, token(path)); if (docs.isEmpty() || docs.get(0) == 0) { // no replacement of first document (because of TableDiskAccess#insert, used > 0, pre = 0) updates.add(new DBAdd(data, input, opts, qc, info), qc); } else { updates.add(new ReplaceDoc(docs.get(0), data, input, opts, qc, info), qc); d = 1; } } // delete old documents final int ds = docs.size(); for (; d < ds; d++) updates.add(new DeleteNode(docs.get(d), data, info), qc); return null; }
/** * Returns the sorted names of all available databases and, optionally, backups. Filters for * {@code name} if not {@code null} with glob support. * * @param db return databases? * @param backup return backups? * @param name name filter (may be {@code null}) * @return database and backups list */ private StringList list(final boolean db, final boolean backup, final String name) { final Pattern pt; if (name != null) { final String nm = REGEX.matcher(name).matches() ? IOFile.regex(name) : name.replaceAll("([" + REGEXCHARS + "])", "\\\\$1"); pt = Pattern.compile(nm, Prop.CASE ? 0 : Pattern.CASE_INSENSITIVE); } else { pt = null; } final IOFile[] children = soptions.dbpath().children(); final StringList list = new StringList(children.length); final HashSet<String> map = new HashSet<>(children.length); for (final IOFile f : children) { final String fn = f.name(); String add = null; if (backup && fn.endsWith(IO.ZIPSUFFIX)) { final String nn = ZIPPATTERN.split(fn)[0]; if (!nn.equals(fn)) add = nn; } else if (db && f.isDir() && fn.indexOf('.') == -1) { add = fn; } // add entry if it matches the pattern, and has not already been added if (add != null && (pt == null || pt.matcher(add).matches()) && map.add(add)) { list.add(add); } } return list.sort(false); }
/** * Returns a list of all databases. * * @param ctx database context * @return list of databases */ public static StringList list(final Context ctx) { final StringList db = new StringList(); for (final IOFile f : ctx.mprop.dbpath().children()) { final String name = f.name(); if (f.isDir() && !name.startsWith(".")) db.add(name); } return db.sort(false); }
@Override public B64 item(final QueryContext qc, final InputInfo ii) throws QueryException { checkCreate(qc); final IOFile root = new IOFile(toPath(0, qc).toString()); final ArchOptions opts = toOptions(1, Q_OPTIONS, new ArchOptions(), qc); final Iter entries; if (exprs.length > 2) { entries = qc.iter(exprs[2]); } else { final TokenList tl = new TokenList(); for (final String file : root.descendants()) tl.add(file); entries = StrSeq.get(tl).iter(); } final String format = opts.get(ArchOptions.FORMAT); final int level = level(opts); if (!root.isDir()) throw FILE_NO_DIR_X.get(info, root); try (final ArchiveOut out = ArchiveOut.get(format.toLowerCase(Locale.ENGLISH), info)) { out.level(level); try { while (true) { Item en = entries.next(); if (en == null) break; en = checkElemToken(en); final IOFile file = new IOFile(root, string(en.string(info))); if (!file.exists()) throw FILE_NOT_FOUND_X.get(info, file); if (file.isDir()) throw FILE_IS_DIR_X.get(info, file); add(en, new B64(file.read()), out, level, qc); } } catch (final IOException ex) { throw ARCH_FAIL_X.get(info, ex); } return new B64(out.finish()); } }
/** * Recursively populates the cache. * * @param root root directory * @param pc file cache * @throws InterruptedException interruption */ void add(final IOFile root, final ProjectCache pc) throws InterruptedException { // check if file cache was replaced or invalidated if (pc != cache) throw new InterruptedException(); final IOFile[] files = root.children(); for (final IOFile file : files) { if (file.name().equals(IO.IGNORESUFFIX)) return; } for (final IOFile file : files) { if (file.isDir()) { add(file, pc); } else { pc.add(file.path()); } } }
@Override public void action(final Object comp) { final String pth = path(); final IOFile io = new IOFile(pth); String inf = io.isDir() && io.children().length > 0 ? DIR_NOT_EMPTY : null; ok = !pth.isEmpty(); final SerialMethod mth = SerialMethod.valueOf(method.getSelectedItem()); final OptionsOption<? extends Options> opts = mth == SerialMethod.JSON ? SerializerOptions.JSON : mth == SerialMethod.CSV ? SerializerOptions.CSV : null; final boolean showmparams = opts != null; mparams.setEnabled(showmparams); if (ok) { gui.gopts.set(GUIOptions.INPUTPATH, pth); try { if (comp == method) { if (showmparams) { final Options mopts = options(null).get(opts); mparams.setToolTipText(tooltip(mopts)); mparams.setText(mopts.toString()); } else { mparams.setToolTipText(null); mparams.setText(""); } } Serializer.get(new ArrayOutput(), options(mth)); } catch (final IOException ex) { ok = false; inf = ex.getMessage(); } } info.setText(inf, ok ? Msg.WARN : Msg.ERROR); enableOK(buttons, B_OK, ok); }