@Override
 public TableImpl getTable(String name) throws IllegalArgumentException {
   Utility.checkTableNameIsCorrect(name);
   if (tables.containsKey(name)) {
     TableImpl table = tables.get(name);
     if (table == null) {
       DatabaseException corruptionReason = corruptTables.get(name);
       throw new IllegalArgumentException(corruptionReason.getMessage(), corruptionReason);
     }
     return table;
   } else {
     return null;
   }
 }
  @Override
  public TableImpl createTable(String name) throws IllegalArgumentException {
    Utility.checkTableNameIsCorrect(name);
    Path tablePath = databaseRoot.resolve(name);

    if (tables.containsKey(name) && tables.get(name) != null) {
      return null;
    }

    TableImpl newTable;
    try {
      newTable = TableImpl.createTable(tablePath);
    } catch (DatabaseException exc) {
      throw new IllegalArgumentException(exc.getMessage(), exc);
    }
    tables.put(name, newTable);
    return newTable;
  }
  /**
   * Scans database directory and reads all tables from it.
   *
   * @throws DatabaseException
   */
  private void reloadTables() throws DatabaseException {
    tables.clear();

    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(databaseRoot)) {
      for (Path tablePath : dirStream) {
        String tableName = tablePath.getFileName().toString();

        try {
          TableImpl table = TableImpl.getTable(tablePath);
          tables.put(tableName, table);
        } catch (DatabaseException exc) {
          // mark as corrupt
          tables.put(tableName, null);
          corruptTables.put(
              tableName,
              (exc instanceof TableCorruptException
                  ? (TableCorruptException) exc
                  : new TableCorruptException(tableName, exc.getMessage(), exc)));
        }
      }
    } catch (IOException exc) {
      throw new DatabaseException("Failed to scan database directory", exc);
    }
  }