/** Closes and deletes the input files. */ private void close() { str.close(); dat.close(); files.delete(); filed.delete(); sizes.delete(); }
/** * 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 void startUpdate(final MainOptions opts) throws IOException { if (!table.lock(true)) throw new BaseXException(Text.DB_PINNED_X, meta.name); if (opts.get(MainOptions.AUTOFLUSH)) { final IOFile uf = meta.updateFile(); if (uf.exists()) throw new BaseXException(Text.DB_UPDATED_X, meta.name); if (!uf.touch()) throw Util.notExpected("%: could not create lock file.", meta.name); } }
/** * Checks if the table of the specified database is locked. * * @param db name of database * @param ctx database context * @return result of check */ public static boolean locked(final String db, final Context ctx) { final IOFile table = MetaData.file(ctx.globalopts.dbpath(db), DATATBL); if (!table.exists()) return false; try (final RandomAccessFile file = new RandomAccessFile(table.file(), "rw")) { return file.getChannel().tryLock() == null; } catch (final ClosedChannelException ex) { return false; } catch (final OverlappingFileLockException | IOException ex) { return true; } }
@Override public synchronized void finishUpdate(final MainOptions opts) { // remove updating file final boolean auto = opts.get(MainOptions.AUTOFLUSH); if (auto) { final IOFile uf = meta.updateFile(); if (!uf.exists()) throw Util.notExpected("%: lock file does not exist.", meta.name); if (!uf.delete()) throw Util.notExpected("%: could not delete lock file.", meta.name); } // db:optimize(..., true) will close the database before this function is called if (!closed) { flush(auto); if (!table.lock(false)) throw Util.notExpected("Database '%': could not unlock.", meta.name); } }
/** * Lists all databases. * * @return success flag * @throws IOException I/O exception */ private boolean list() throws IOException { final Table table = new Table(); table.description = DATABASES_X; final boolean create = context.user.has(Perm.CREATE); table.header.add(T_NAME); table.header.add(RESOURCES); table.header.add(SIZE); if (create) table.header.add(INPUT_PATH); for (final String name : context.databases.listDBs()) { String file = null; long size = 0; int docs = 0; final MetaData meta = new MetaData(name, context); try { meta.read(); size = meta.dbsize(); docs = meta.ndocs; if (context.perm(Perm.READ, meta)) file = meta.original; } catch (final IOException ex) { file = ERROR; } // count number of raw files final IOFile dir = new IOFile(mprop.dbpath(name), M_RAW); final int bin = dir.descendants().size(); // create entry if (file != null) { final TokenList tl = new TokenList(4); tl.add(name); tl.add(docs + bin); tl.add(size); if (create) tl.add(file); table.contents.add(tl); } } out.println(table.sort().finish()); return true; }