예제 #1
0
파일: DbReplace.java 프로젝트: LukasK/basex
  @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;
  }
예제 #2
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final Data data = checkData(qc);
   final String path = path(1, qc);
   if (data.inMemory()) return Bln.FALSE;
   final IOFile io = data.meta.binary(path);
   return Bln.get(io.exists() && !io.isDir());
 }
예제 #3
0
  @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());
    }
  }