class DbMarketDataSnapshotMaster extends AbstractDocumentDbMaster<MarketDataSnapshotDocument> implements MarketDataSnapshotMaster {

  public MarketDataSnapshotSearchResult search(final MarketDataSnapshotSearchRequest request) {
    ArgumentChecker.notNull(request, "request");
    ArgumentChecker.notNull(request.getPagingRequest(), "request.pagingRequest");
    ArgumentChecker.notNull(request.getVersionCorrection(), "request.versionCorrection");
    s_logger.debug("search {}", request);
    
    final VersionCorrection vc = request.getVersionCorrection().withLatestFixed(now());
    final MarketDataSnapshotSearchResult result = new MarketDataSnapshotSearchResult(vc);
    
    final List<ObjectId> snapshotIds = request.getSnapshotIds();
    if (snapshotIds != null && snapshotIds.size() == 0) {
      result.setPaging(Paging.of(request.getPagingRequest(), 0));
      return result;
    }
    
    final DbMapSqlParameterSource args = createParameterSource();
    args.addTimestamp("version_as_of_instant", vc.getVersionAsOf());
    args.addTimestamp("corrected_to_instant", vc.getCorrectedTo());
    args.addValueNullIgnored("name", getDialect().sqlWildcardAdjustValue(request.getName()));
    args.addValueNullIgnored("snapshot_type", request.getType() == null ? null : request.getType().getName());
    args.addValue("details", request.isIncludeData());
    if (snapshotIds != null) {
      StringBuilder buf = new StringBuilder(snapshotIds.size() * 10);
      for (ObjectId snapshotId : snapshotIds) {
        checkScheme(snapshotId);
        buf.append(extractOid(snapshotId)).append(", ");
      }
      buf.setLength(buf.length() - 2);
      args.addValue("sql_search_object_ids", buf.toString());
    }
    args.addValue("sort_order", ORDER_BY_MAP.get(request.getSortOrder()));
    args.addValue("paging_offset", request.getPagingRequest().getFirstItem());
    args.addValue("paging_fetch", request.getPagingRequest().getPagingSize());
    
    String[] sql = {getElSqlBundle().getSql("Search", args), getElSqlBundle().getSql("SearchCount", args)};
    doSearch(request.getPagingRequest(), sql, args, new MarketDataSnapshotDocumentExtractor(request.isIncludeData()), result);
    return result;
  }

  public  DbMarketDataSnapshotMaster(DbConnector dbConnector);

}