@Override public WikidataStatement transform(Record r) { try { return buildStatement(r); } catch (DaoException e) { LOG.warn(e.getMessage(), e); return null; } } };
@Override public WikidataEntity getProperty(int id) throws DaoException { Map<Integer, WikidataEntity> properties = getProperties(); // should be cached! if (properties == null || properties.size() == 0) { return getEntityWithoutCache(WikidataEntity.Type.PROPERTY, id); } else { return properties.get(id); } }
@Override public Map<String, List<LocalWikidataStatement>> getLocalStatements(LocalPage page) throws DaoException { int conceptId = getItemId(page); if (conceptId < 0) { return new HashMap<String, List<LocalWikidataStatement>>(); } return getLocalStatements(getRealLang(page.getLanguage()), WikidataEntity.Type.ITEM, conceptId); }
@Override public Map<String, List<LocalWikidataStatement>> getLocalStatements(Language lang, WikidataEntity.Type type, int id) throws DaoException { lang = getRealLang(lang); WikidataFilter filter = new WikidataFilter.Builder() .withEntityType(type) .withEntityId(id) .build(); Map<String, List<LocalWikidataStatement>> local = new HashMap<String, List<LocalWikidataStatement>>(); for (WikidataStatement st : get(filter)) { LocalWikidataStatement lws = getLocalStatement(lang, st); if (!local.containsKey(lws.getProperty())) { local.put(lws.getProperty(), new ArrayList<LocalWikidataStatement>()); } local.get(lws.getProperty()).add(lws); } return local; }
properties = new ConcurrentHashMap<Integer, WikidataEntity>(); LOG.info("creating wikidata properties cache. This only happens once..."); DSLContext context = getJooq(); try { Result<Record1<Integer>> result = context properties.put(id, getEntityWithoutCache(WikidataEntity.Type.PROPERTY, id)); freeJooq(context);
@Override public String getLabel(Language language, WikidataEntity.Type type, int id) throws DaoException { if (type == WikidataEntity.Type.PROPERTY) { WikidataEntity prop = getProperty(id); // should be cached, fast if (prop.getLabels().isEmpty()) { LOG.warn("no labels for property " + id); DSLContext jooq = getJooq(); try { for (Language l : Arrays.asList(language, FALLBACK_LANGUAGE)) { freeJooq(jooq);
private WikidataEntity getEntityWithoutCache(WikidataEntity.Type type, int id) throws DaoException { WikidataEntity entity = new WikidataEntity(type, id); DSLContext jooq = getJooq(); try { Result<Record2<Short, String>> result = jooq .withEntityId(id) .build(); for (WikidataStatement st : get(filter)) { if (st != null) { entity.getStatements().add(st); freeJooq(jooq);
List<String> values = new ArrayList<String>(); for (WikidataValue value : filter.getValues()) { values.add(encodeValue(value)); if (type == null) { type = value.getTypeName(); DSLContext jooq = getJooq(); try { where(conditions).fetchLazy(getFetchSize());
@Override public void save(WikidataStatement item) throws DaoException { insert( item.getId(), item.getItem().getType().code, item.getItem().getId(), item.getProperty().getId(), item.getValue().getTypeName().toLowerCase(), encodeValue(item.getValue()), item.getRank().ordinal() ); }
@Override public Iterable<WikidataStatement> getByValue(String propertyName, WikidataValue value) throws DaoException { Set<Integer> propIds = new HashSet<Integer>(); for (WikidataEntity e : getPropertyByName(propertyName)) { propIds.add(e.getId()); } if (propIds.isEmpty()) { return new ArrayList<WikidataStatement>(); } WikidataFilter filter = new WikidataFilter.Builder() .withPropertyIds(propIds) .withValue(value) .build(); return get(filter); }
@Override public WikidataDao get(String name, Config config, Map<String, String> runtimeParams) throws ConfigurationException { if (!config.getString("type").equals("sql")) { return null; } try { WikidataSqlDao dao = new WikidataSqlDao( getConfigurator().get( WpDataSource.class, config.getString("dataSource")), getConfigurator().get(LocalPageDao.class), getConfigurator().get(UniversalPageDao.class) ); 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 List<WikidataStatement> getStatements(LocalPage page) throws DaoException { int conceptId = upDao.getUnivPageId(page); if (conceptId < 0) { return new ArrayList<WikidataStatement>(); } WikidataFilter filter = new WikidataFilter.Builder() .withEntityType(WikidataEntity.Type.ITEM) .withEntityId(conceptId) .build(); return IteratorUtils.toList(get(filter).iterator()); }
@Override public Set<Integer> conceptsWithValue(String propertyName, WikidataValue value) throws DaoException { Set<Integer> concepts = new HashSet<Integer>(); for (WikidataStatement st : getByValue(propertyName, value)) { if (st.getItem().getType() == WikidataEntity.Type.ITEM) { concepts.add(st.getItem().getId()); } } return concepts; }
@Override public Set<LocalId> pagesWithValue(String propertyName, WikidataValue value, Language language) throws DaoException { Set<LocalId> ids = new HashSet<LocalId>(); for (int conceptId : conceptsWithValue(propertyName, value)) { UniversalPage up = upDao.getById(conceptId); if (up != null && up.isInLanguage(language)) { ids.add(new LocalId(language, up.getLocalId(language))); } } return ids; } @Override
@Override public WikidataEntity getItem(int id) throws DaoException { return getEntityWithoutCache(WikidataEntity.Type.ITEM, id); }
@Override public Map<String, List<LocalWikidataStatement>> getLocalStatements(Language lang, WikidataEntity.Type type, int id) throws DaoException { lang = getRealLang(lang); WikidataFilter filter = new WikidataFilter.Builder() .withEntityType(type) .withEntityId(id) .build(); Map<String, List<LocalWikidataStatement>> local = new HashMap<String, List<LocalWikidataStatement>>(); for (WikidataStatement st : get(filter)) { LocalWikidataStatement lws = getLocalStatement(lang, st); if (!local.containsKey(lws.getProperty())) { local.put(lws.getProperty(), new ArrayList<LocalWikidataStatement>()); } local.get(lws.getProperty()).add(lws); } return local; }
properties = new ConcurrentHashMap<Integer, WikidataEntity>(); LOG.info("creating wikidata properties cache. This only happens once..."); DSLContext context = getJooq(); try { Result<Record1<Integer>> result = context properties.put(id, getEntityWithoutCache(WikidataEntity.Type.PROPERTY, id)); freeJooq(context);
@Override public String getLabel(Language language, WikidataEntity.Type type, int id) throws DaoException { if (type == WikidataEntity.Type.PROPERTY) { WikidataEntity prop = getProperty(id); // should be cached, fast if (prop.getLabels().isEmpty()) { LOG.warn("no labels for property " + id); DSLContext jooq = getJooq(); try { for (Language l : Arrays.asList(language, FALLBACK_LANGUAGE)) { freeJooq(jooq);
private WikidataEntity getEntityWithoutCache(WikidataEntity.Type type, int id) throws DaoException { WikidataEntity entity = new WikidataEntity(type, id); DSLContext jooq = getJooq(); try { Result<Record2<Short, String>> result = jooq .withEntityId(id) .build(); for (WikidataStatement st : get(filter)) { if (st != null) { entity.getStatements().add(st); freeJooq(jooq);
List<String> values = new ArrayList<String>(); for (WikidataValue value : filter.getValues()) { values.add(encodeValue(value)); if (type == null) { type = value.getTypeName(); DSLContext jooq = getJooq(); try { where(conditions).fetchLazy(getFetchSize());