예제 #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
파일: List.java 프로젝트: JohnLeM/basex
 /**
  * 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);
 }
예제 #3
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());
 }
예제 #4
0
파일: Retrieve.java 프로젝트: ngrstad/basex
  @Override
  protected boolean run() throws IOException {
    final String path = MetaData.normPath(args[0]);
    if (path == null) return error(NAME_INVALID_X, args[0]);

    final IOFile bin = context.data().meta.binary(path);
    if (bin == null || !bin.exists() || bin.isDir()) return error(RES_NOT_FOUND_X, path);

    try {
      final BufferInput bi = new BufferInput(bin);
      try {
        for (int b; (b = bi.read()) != -1; ) out.write(b);
      } finally {
        bi.close();
      }
      return info(QUERY_EXECUTED_X, perf);
    } catch (final IOException ex) {
      return error(FILE_NOT_STORED_X, ex);
    }
  }
예제 #5
0
  @Override
  protected boolean run() {
    final boolean create = context.user.has(Perm.CREATE);
    String path = MetaData.normPath(args[0]);
    if (path == null || path.endsWith(".")) return error(NAME_INVALID_X, args[0]);

    if (in == null) {
      final IO io = IO.get(args[1]);
      if (!io.exists() || io.isDir()) return error(RES_NOT_FOUND_X, create ? io : args[1]);
      in = io.inputSource();
      // set/add name of document
      if ((path.isEmpty() || path.endsWith("/")) && !(io instanceof IOContent)) path += io.name();
    }

    // ensure that the final name is not empty
    if (path.isEmpty()) return error(NAME_INVALID_X, path);

    // ensure that the name is not empty and contains no trailing dots
    final Data data = context.data();
    if (data.inMemory()) return error(NO_MAINMEM);

    final IOFile file = data.meta.binary(path);
    if (path.isEmpty() || path.endsWith(".") || file == null || file.isDir())
      return error(NAME_INVALID_X, create ? path : args[0]);

    // start update
    if (!data.startUpdate()) return error(DB_PINNED_X, data.meta.name);

    try {
      store(in, file);
      return info(QUERY_EXECUTED_X_X, "", perf);
    } catch (final IOException ex) {
      return error(FILE_NOT_STORED_X, Util.message(ex));
    } finally {
      data.finishUpdate();
    }
  }