org.ojai.store.QueryCondition Java Examples

The following examples show how to use org.ojai.store.QueryCondition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns number of albums according to the specified language.
 *
 * @param language language code.
 * @return number of albums with specified language code.
 */
public long getTotalNumByLanguage(String language) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        QueryCondition languageEqualsCondition = connection.newCondition()
                .is("language", QueryCondition.Op.EQUAL, language)
                .build();

        Query query = connection.newQuery()
                .select("_id")
                .where(languageEqualsCondition)
                .build();

        DocumentStream documentStream = store.findQuery(query);
        long totalNum = 0;
        for (Document ignored : documentStream) {
            totalNum++;
        }

        log.debug("Counting '{}' albums by language '{}' took {}", totalNum, language, stopwatch);

        return totalNum;
    });
}
 
Example #2
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of Artist rates by user identifier.
 *
 * @param userId user's identifier.
 * @return list of Artist rates.
 */
public List<ArtistRate> getByUserId(String userId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("user_id", QueryCondition.Op.EQUAL, userId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<ArtistRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            ArtistRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by user id '{}' took {}", rates.size(), userId, stopwatch);

        return rates;
    });
}
 
Example #3
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of Artist rates by artist identifier.
 *
 * @param artistId artist's identifier.
 * @return list of Artist rates.
 */
public List<ArtistRate> getByArtistId(String artistId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("document_id", QueryCondition.Op.EQUAL, artistId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<ArtistRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            ArtistRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by artist id '{}' took {}", rates.size(), artistId, stopwatch);

        return rates;
    });
}
 
Example #4
Source File: MapRJsonOriginSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private QueryCondition.Op conditionOp(boolean starting) {
  if (starting) {
    return QueryCondition.Op.GREATER_OR_EQUAL;
  } else {
    return QueryCondition.Op.GREATER;
  }

}
 
Example #5
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns list of Album rates by album identifier.
 *
 * @param albumId album's identifier.
 * @return list of Album rates.
 */
public List<AlbumRate> getByAlbumId(String albumId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("document_id", QueryCondition.Op.EQUAL, albumId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<AlbumRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            AlbumRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by album id '{}' took {}", rates.size(), albumId, stopwatch);

        return rates;
    });
}
 
Example #6
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns list of Album rates by user identifier.
 *
 * @param userId user's identifier.
 * @return list of Album rates.
 */
public List<AlbumRate> getByUserId(String userId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        Query query = connection.newQuery().where(
                connection.newCondition()
                        .is("user_id", QueryCondition.Op.EQUAL, userId)
                        .build()
        ).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        List<AlbumRate> rates = new ArrayList<>();
        for (Document document : documentStream) {
            AlbumRate rate = mapOjaiDocument(document);
            if (rate != null) {
                rates.add(rate);
            }
        }

        log.debug("Get '{}' rates by user id '{}' took {}", rates.size(), userId, stopwatch);

        return rates;
    });
}
 
Example #7
Source File: AlbumRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns Album rate according to the specified user identifier and album identifier.
 *
 * @param userId  user identifier.
 * @param albumId album identifier.
 * @return album rate.
 */
public AlbumRate getRate(String userId, String albumId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        QueryCondition condition = connection.newCondition()
                .and()
                .is("user_id", QueryCondition.Op.EQUAL, userId)
                .is("document_id", QueryCondition.Op.EQUAL, albumId)
                .close()
                .build();

        Query query = connection.newQuery().where(condition).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        Iterator<Document> documentIterator = documentStream.iterator();

        if (!documentIterator.hasNext()) {
            return null;
        }

        log.debug("Get rate by album id '{}' and user id '{}' took {}", albumId, userId, stopwatch);

        return mapOjaiDocument(documentIterator.next());
    });
}
 
Example #8
Source File: ArtistRateDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Returns Artist rate according to the specified user identifier and artist identifier.
 *
 * @param userId   user identifier.
 * @param artistId artist identifier.
 * @return artist rate.
 */
public ArtistRate getRate(String userId, String artistId) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        QueryCondition condition = connection.newCondition()
                .and()
                .is("user_id", QueryCondition.Op.EQUAL, userId)
                .is("document_id", QueryCondition.Op.EQUAL, artistId)
                .close()
                .build();

        Query query = connection.newQuery().where(condition).build();

        // Fetch all OJAI Documents from this store according to the built query
        DocumentStream documentStream = store.findQuery(query);
        Iterator<Document> documentIterator = documentStream.iterator();

        if (!documentIterator.hasNext()) {
            return null;
        }

        log.debug("Get rate by artist id '{}' and user id '{}' took {}", artistId, userId, stopwatch);

        return mapOjaiDocument(documentIterator.next());
    });
}
 
Example #9
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Finds albums, which titles start with the specified name entry.
 *
 * @param nameEntry specifies query criteria.
 * @param limit     specified limit.
 * @param fields    specifies fields that will be fetched.
 * @return list of albums which titles start with the specified name entry.
 */
public List<Album> getByNameStartsWith(String nameEntry, long limit, String... fields) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery();

        // Select only specified field
        if (fields != null && fields.length > 0) {
            query.select(fields);
        } else {
            query.select("*");
        }

        // Build Query Condition to fetch documents by name entry
        String nameStartsWithPattern = nameEntry + "%";
        QueryCondition nameStartsWithCondition = connection.newCondition()
                .like("name", nameStartsWithPattern)
                .build();

        // Add Condition and specified limit to the Query
        query.where(nameStartsWithCondition)
                .limit(limit)
                .build();

        DocumentStream documentStream = store.findQuery(query);
        List<Album> albums = new ArrayList<>();
        for (Document doc : documentStream) {
            albums.add(mapOjaiDocument(doc));
        }

        log.debug("Get '{}' albums by name entry: '{}' with limit: '{}', fields: '{}'. Elapsed time: {}",
                albums.size(), nameEntry, limit, (fields != null) ? Arrays.asList(fields) : "[]", stopwatch);

        return albums;
    });
}
 
Example #10
Source File: ArtistDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Finds artists, which names start with the specified name entry.
 *
 * @param nameEntry specifies query criteria.
 * @param limit     specified limit.
 * @param fields    specifies fields that will be fetched.
 * @return list of artists which names start with the specified name entry.
 */
public List<Artist> getByNameStartsWith(String nameEntry, long limit, String... fields) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();
        Query query = connection.newQuery();

        // Select only specified field
        if (fields != null && fields.length > 0) {
            query.select(fields);
        } else {
            query.select("*");
        }

        // Build Query Condition to fetch documents by name entry
        String nameStartsWithPattern = nameEntry + "%";
        QueryCondition nameStartsWithCondition = connection.newCondition()
                .like("name", nameStartsWithPattern)
                .build();

        // Add Condition and specified limit to the Query
        query.where(nameStartsWithCondition)
                .limit(limit)
                .build();

        DocumentStream documentStream = store.findQuery(query);
        List<Artist> artists = new ArrayList<>();
        for (Document doc : documentStream) {
            artists.add(mapOjaiDocument(doc));
        }

        log.debug("Get '{}' artists by name entry: '{}' with limit: '{}', fields: '{}'. Elapsed time: {}",
                artists.size(), nameEntry, limit, (fields != null) ? Arrays.asList(fields) : "[]", stopwatch);

        return artists;
    });
}
 
Example #11
Source File: AlbumDao.java    From mapr-music with Apache License 2.0 4 votes vote down vote up
/**
 * Returns list of albums by language code.
 *
 * @param offset  offset value.
 * @param limit   limit value.
 * @param options define the order of documents.
 * @param lang    language code.
 * @param fields  list of fields that will present in document.
 * @return list of albums with specified language code.
 */
public List<Album> getByLanguage(long offset, long limit, List<SortOption> options, String lang, String... fields) {
    return processStore((connection, store) -> {

        Stopwatch stopwatch = Stopwatch.createStarted();

        Query query = connection.newQuery();

        // Select only specified field
        if (fields != null && fields.length > 0) {
            query.select(fields);
        } else {
            query.select("*");
        }

        // Build Query Condition to fetch documents by specified language
        QueryCondition languageEqualsCondition = connection.newCondition()
                .is("language", QueryCondition.Op.EQUAL, lang)
                .build();

        // Add Condition to the Query
        query.where(languageEqualsCondition);

        // Add ordering if sort options specified
        if (options != null && !options.isEmpty()) {
            for (SortOption opt : options) {
                SortOrder ojaiOrder = (SortOption.Order.DESC == opt.getOrder()) ? SortOrder.DESC : SortOrder.ASC;
                for (String field : opt.getFields()) {
                    query = query.orderBy(field, ojaiOrder);
                }
            }
        }

        // Add specified offset and limit to the Query
        query.offset(offset)
                .limit(limit)
                .build();

        DocumentStream documentStream = store.findQuery(query);
        List<Album> albums = new ArrayList<>();
        for (Document doc : documentStream) {
            albums.add(mapOjaiDocument(doc));
        }

        log.debug("Get list of '{}' documents from '{}' table by language: '{}' with offset: '{}', limit: '{}', " +
                        "sortOptions: '{}', fields: '{}'. Elapsed time: {}", albums.size(), tablePath, lang, offset,
                limit, options, (fields != null) ? Arrays.asList(fields) : "[]", stopwatch);

        return albums;
    });
}
 
Example #12
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndReplace(final String id, final QueryCondition condition, final Document r)
    throws StoreException {
  return store.checkAndReplace(id, condition, r);
}
 
Example #13
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndDelete(final String id, final QueryCondition condition) throws StoreException {
  return store.checkAndDelete(id, condition);
}
 
Example #14
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndMutate(final String id, final QueryCondition condition, final DocumentMutation m)
    throws StoreException {
  return store.checkAndMutate(id, condition, m);
}
 
Example #15
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public DocumentStream find(final QueryCondition c, final FieldPath... paths) throws StoreException {
  return store.find(c, paths);
}
 
Example #16
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public DocumentStream find(final QueryCondition c, final String... paths) throws StoreException {
  return store.find(c, paths);
}
 
Example #17
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public DocumentStream find(final QueryCondition c) throws StoreException {
  return store.find(c);
}
 
Example #18
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final String id, final QueryCondition c, final FieldPath... paths) throws StoreException {
  return store.findById(id, c, paths);
}
 
Example #19
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final String id, final QueryCondition c, final String... paths) throws StoreException {
  return store.findById(id, c, paths);
}
 
Example #20
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final String id, final QueryCondition c) throws StoreException {
  return store.findById(id, c);
}
 
Example #21
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndReplace(final Value _id, final QueryCondition condition, final Document doc)
    throws StoreException {
  return store.checkAndReplace(_id, condition, doc);
}
 
Example #22
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndDelete(final Value _id, final QueryCondition condition) throws StoreException {
  return store.checkAndDelete(_id, condition);
}
 
Example #23
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public boolean checkAndMutate(final Value _id, final QueryCondition condition, final DocumentMutation mutation)
    throws StoreException {
  return store.checkAndUpdate(_id, condition, mutation);
}
 
Example #24
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final Value _id, final QueryCondition condition, final FieldPath... fieldPaths)
    throws StoreException {
  return store.findById(_id, condition, fieldPaths);
}
 
Example #25
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final Value _id, final QueryCondition condition, final String... fieldPaths)
    throws StoreException {
  return store.findById(_id, condition, fieldPaths);
}
 
Example #26
Source File: ForwardingStore.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document findById(final Value _id, final QueryCondition condition) throws StoreException {
  return store.findById(_id, condition);
}
 
Example #27
Source File: JsonDriver.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public QueryCondition newCondition() {
  throw new UnsupportedOperationException();
}
 
Example #28
Source File: JsonConnection.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public QueryCondition newCondition() {
  return driver.newCondition();
}
 
Example #29
Source File: OjaiCodec.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Decodes an instance of &lt;T&gt; into an OJAI {@link QueryCondition}
 *
 * @param encodedCondition an instance of &lt;T&gt; containing encoded OJAI QueryCondition
 * @return an OJAI {@link QueryCondition}
 * @throws DecodingException
 */
public QueryCondition decodeCondition(@NonNullable T encodedCondition) throws DecodingException;
 
Example #30
Source File: OjaiCodec.java    From ojai with Apache License 2.0 2 votes vote down vote up
/**
 * Encodes an OJAI {@link QueryCondition} into an instance of &lt;T&gt;
 *
 * @param condition OJAI QueryCondition to encode
 * @return an instance of &lt;T&gt;
 * @throws EncodingException
 */
public T encodeCondition(@NonNullable QueryCondition condition) throws EncodingException;