/** * Build a LocalPage from a database record representation. * Classes that extend class this should override this method. * * @param record a database record * @return a LocalPage representation of the given database record * @throws DaoException if the record is not a Page */ protected LocalPage buildLocalPage(Record record) throws DaoException { return buildLocalPage(record, new DaoFilter()); }
@Override public int getIdByTitle(String title, Language language, NameSpace nameSpace) throws DaoException { if (titlesToIds==null){ buildTitlesToIds(); } return titlesToIds.get(Title.longHashCode(language, title, nameSpace)); }
@Override public Map<Integer, LocalPage> getByIds(Language language, Collection<Integer> pageIds) throws DaoException { if (pageIds == null || pageIds.isEmpty()) { return null; } Map<Integer, LocalPage> map = new HashMap<Integer, LocalPage>(); for (Integer pageId : pageIds){ map.put(pageId, getById(language, pageId)); } return map; }
final int n = getCount(new DaoFilter()); DSLContext context = getJooq(); try { Cursor<Record> cursor = context.select(). from(Tables.LOCAL_PAGE). fetchLazy(getFetchSize()); final TLongIntHashMap map = new TLongIntHashMap( Constants.DEFAULT_CAPACITY, freeJooq(context);
@Override public LocalPage getById(Language language, int pageId) throws DaoException { DSLContext context = getJooq(); try { Record record = context.select(). from(Tables.LOCAL_PAGE). where(Tables.LOCAL_PAGE.PAGE_ID.eq(pageId)). and(Tables.LOCAL_PAGE.LANG_ID.eq(language.getId())). limit(1). fetchOne(); return buildLocalPage(record); } finally { freeJooq(context); } }
@Override public Set<LocalId> getIds(final DaoFilter daoFilter) throws DaoException { DSLContext context = getJooq(); try { Collection<Condition> conditions = new ArrayList<Condition>(); where(conditions). limit(daoFilter.getLimitOrInfinity()). fetchLazy(getFetchSize()); Set<LocalId> ids = new HashSet<LocalId>(); for (Record2<Short, Long> record : result) { freeJooq(context); throw e;
@Override public LocalPageDao get(String name, Config config, Map<String, String> runtimeParams) throws ConfigurationException { if (!config.getString("type").equals("sql")) { return null; } try { LocalPageSqlDao dao = new LocalPageSqlDao( getConfigurator().get( WpDataSource.class, config.getString("dataSource")) ); String cachePath = getConfig().get().getString("dao.sqlCachePath"); File cacheDir = new File(cachePath); if (!cacheDir.isDirectory()) { cacheDir.mkdirs(); } dao.useCache(cacheDir); return dao; } catch (DaoException e) { throw new ConfigurationException(e); } } }
@Override public int getCount(DaoFilter daoFilter) throws DaoException{ DSLContext context = getJooq(); try { Collection<Condition> conditions = new ArrayList<Condition>(); if (daoFilter.getLangIds() != null) { conditions.add(Tables.LOCAL_PAGE.LANG_ID.in(daoFilter.getLangIds())); } if (daoFilter.getNameSpaceIds() != null) { conditions.add(Tables.LOCAL_PAGE.NAME_SPACE.in(daoFilter.getNameSpaceIds())); } if (daoFilter.isRedirect() != null) { conditions.add(Tables.LOCAL_PAGE.IS_REDIRECT.in(daoFilter.isRedirect())); } if (daoFilter.isDisambig() != null) { conditions.add(Tables.LOCAL_PAGE.IS_DISAMBIG.in(daoFilter.isDisambig())); } return context.selectCount(). from(Tables.LOCAL_PAGE). where(conditions). fetchOne().value1(); } finally { freeJooq(context); } }
@Override public LocalPage getByTitle(Language lang, String title) throws DaoException { return getByTitle(lang, NameSpace.ARTICLE, title); }
@Override public void save(LocalPage page) throws DaoException { insert( page.getLanguage().getId(), page.getLocalId(), page.getTitle().getCanonicalTitle(), page.getNameSpace().getArbitraryId(), page.isRedirect(), page.isDisambig() ); }
@Override public LocalPage getByTitle(Title title, NameSpace nameSpace) throws DaoException { DSLContext context = getJooq(); try { Record record = context.select(). from(Tables.LOCAL_PAGE). where(Tables.LOCAL_PAGE.TITLE.eq(title.getCanonicalTitle())). and(Tables.LOCAL_PAGE.LANG_ID.eq(title.getLanguage().getId())). and(Tables.LOCAL_PAGE.NAME_SPACE.eq(nameSpace.getArbitraryId())). limit(1). fetchOne(); return buildLocalPage(record); } finally { freeJooq(context); } }
@Override public Iterable<LocalPage> get(final DaoFilter daoFilter) throws DaoException { DSLContext context = getJooq(); try { Collection<Condition> conditions = new ArrayList<Condition>(); where(conditions). limit(daoFilter.getLimitOrInfinity()). fetchLazy(getFetchSize()); return new SimpleSqlDaoIterable<LocalPage>(result, context) { @Override freeJooq(context); throw e;
@Override public Map<Title, LocalPage> getByTitles(Language language, Collection<Title> titles, NameSpace nameSpace) throws DaoException { if (titles == null || titles.isEmpty()) { return null; } Map<Title, LocalPage> map = new HashMap<Title, LocalPage>(); for (Title title : titles){ map.put(title, getByTitle(title, nameSpace)); } return map; }
@Override public LocalPage getByTitle(Language language, NameSpace ns, String title) throws DaoException { return getByTitle(new Title(title, language), ns); }
@Override public LocalPage getById(LocalId localId) throws DaoException { return getById(localId.getLanguage(), localId.getId()); }
@Override public int getIdByTitle(Title title) throws DaoException { if (titlesToIds==null){ buildTitlesToIds(); } return titlesToIds.get(title.longHashCode()); }
@Override public LocalPage transform(Record r) { try { return buildLocalPage(r, daoFilter); } catch (DaoException e) { LOG.warn(e.getMessage(), e); return null; } } };
protected LocalPage buildLocalPage(Record record, DaoFilter daoFilter) throws DaoException { if (record == null) { return null; } Language lang = Language.getById(record.getValue(Tables.LOCAL_PAGE.LANG_ID)); if (redirectSqlDao != null // either null or false // If true, we don't want to resolve redirects because they're all redirects && (daoFilter.isRedirect() == null || !daoFilter.isRedirect()) && record.getValue(Tables.LOCAL_PAGE.IS_REDIRECT)) { LocalPage page = getById(lang, redirectSqlDao.resolveRedirect( lang, record.getValue(Tables.LOCAL_PAGE.PAGE_ID))); if (daoFilter.isValidLocalPage(page)) { return page; } } Title title = new Title( record.getValue(Tables.LOCAL_PAGE.TITLE), true, LanguageInfo.getByLanguage(lang)); NameSpace nameSpace = NameSpace.getNameSpaceByArbitraryId(record.getValue(Tables.LOCAL_PAGE.NAME_SPACE)); return new LocalPage( lang, record.getValue(Tables.LOCAL_PAGE.PAGE_ID), title, nameSpace, record.getValue(Tables.LOCAL_PAGE.IS_REDIRECT), record.getValue(Tables.LOCAL_PAGE.IS_DISAMBIG) ); }