@Override
 public synchronized YieldCurveDefinitionDocument update(YieldCurveDefinitionDocument document) {
   ArgumentChecker.notNull(document, "document");
   ArgumentChecker.notNull(document.getYieldCurveDefinition(), "document.yieldCurveDefinition");
   final Currency currency = document.getYieldCurveDefinition().getCurrency();
   final String name = document.getYieldCurveDefinition().getName();
   final UniqueId uid = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
   if (!uid.equals(document.getUniqueId())) {
     throw new IllegalArgumentException("Invalid unique identifier");
   }
   final Pair<Currency, String> key = Pair.of(currency, name);
   final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
   if (value == null) {
     throw new DataNotFoundException("UID '" + uid + "' not found");
   }
   if (_sourceVersionCorrection.getVersionAsOf() != null) {
     // Don't need to keep the old values before the one needed by "versionAsOfInstant"
     final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
     value.headMap(oldestNeeded).clear();
   } else {
     // Don't need any old values
     value.clear();
   }
   Instant now = Instant.now();
   value.put(now, document.getYieldCurveDefinition());
   document.setUniqueId(uid);
   changeManager().entityChanged(ChangeType.CHANGED, uid.getObjectId(), null, null, now);
   return document;
 }
  // -------------------------------------------------------------------------
  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 MarketDataSnapshotSearchResult result = new MarketDataSnapshotSearchResult();

    final VersionCorrection vc = request.getVersionCorrection().withLatestFixed(now());
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource();
    args.addTimestamp("version_as_of_instant", vc.getVersionAsOf());
    args.addTimestamp("corrected_to_instant", vc.getCorrectedTo());
    args.addValueNullIgnored("name", getDialect().sqlWildcardAdjustValue(request.getName()));
    args.addValue("details", request.isIncludeData());
    args.addValue("paging_offset", request.getPagingRequest().getFirstItem());
    args.addValue("paging_fetch", request.getPagingRequest().getPagingSize());

    String[] sql = {
      getElSqlBundle().getSql("Search", args), getElSqlBundle().getSql("SearchCount", args)
    };
    searchWithPaging(
        request.getPagingRequest(),
        sql,
        args,
        new MarketDataSnapshotDocumentExtractor(request.isIncludeData()),
        result);
    return result;
  }
 private VersionCorrection getResolvedVersionCorrection() {
   VersionCorrection versionCorrection = getExecutionOptions().getVersionCorrection();
   if (!versionCorrection.containsLatest()) {
     return versionCorrection;
   }
   return versionCorrection.withLatestFixed(Instant.now());
 }
 /**
  * Builds a URI.
  *
  * @param baseUri the base URI, not null
  * @param objectId the object identifier, may be null
  * @param vc the version-correction, null means latest
  * @return the URI, not null
  */
 public static URI uriGet(URI baseUri, ObjectId objectId, VersionCorrection vc) {
   UriBuilder bld = UriBuilder.fromUri(baseUri).path("exchanges/{exchangeId}");
   if (vc != null) {
     bld.queryParam("versionAsOf", vc.getVersionAsOfString());
     bld.queryParam("correctedTo", vc.getCorrectedToString());
   }
   return bld.build(objectId);
 }
 /**
  * Builds a URI.
  *
  * @param baseUri the base URI, not null
  * @param vc the version-correction, null means latest
  * @param bundle the bundle, may be null
  * @return the URI, not null
  */
 public static URI uriSearch(URI baseUri, VersionCorrection vc, ExternalIdBundle bundle) {
   UriBuilder bld = UriBuilder.fromUri(baseUri).path("exchanges");
   if (vc != null) {
     bld.queryParam("versionAsOf", vc.getVersionAsOfString());
     bld.queryParam("correctedTo", vc.getCorrectedToString());
   }
   bld.queryParam("id", bundle.toStringList().toArray());
   return bld.build();
 }
 @Override
 public synchronized void remove(ObjectIdentifiable objectIdentifiable) {
   ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable");
   if (!getUniqueIdScheme().equals(objectIdentifiable.getObjectId().getScheme())) {
     throw new DataNotFoundException(
         "Scheme '"
             + objectIdentifiable.getObjectId().getScheme()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'");
   }
   final int i = objectIdentifiable.getObjectId().getValue().indexOf('_');
   if (i <= 0) {
     throw new DataNotFoundException(
         "Identifier '"
             + objectIdentifiable.getObjectId().getValue()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'");
   }
   final String name = objectIdentifiable.getObjectId().getValue().substring(0, i);
   final String iso = objectIdentifiable.getObjectId().getValue().substring(i + 1);
   final Currency currency;
   try {
     currency = Currency.of(iso);
   } catch (IllegalArgumentException e) {
     throw new DataNotFoundException(
         "Identifier '"
             + objectIdentifiable.getObjectId().getValue()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'",
         e);
   }
   final Pair<Currency, String> key = Pair.of(currency, name);
   if (_sourceVersionCorrection.getVersionAsOf() != null) {
     final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
     if (value == null) {
       throw new DataNotFoundException("Curve definition not found");
     }
     // Don't need to keep the old values before the one needed by "versionAsOfInstant"
     final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
     if (oldestNeeded != null) {
       value.headMap(oldestNeeded).clear();
     }
     // Store a null to indicate the delete
     value.put(Instant.now(), null);
   } else {
     if (_definitions.remove(key) == null) {
       throw new DataNotFoundException("Curve definition not found");
     }
   }
   changeManager()
       .entityChanged(
           ChangeType.REMOVED, objectIdentifiable.getObjectId(), null, null, Instant.now());
 }
 /**
  * Sets the function initialization identifier.
  *
  * @param id the identifier to bind
  */
 public void setFunctionInitId(final long id) {
   put(FUNCTION_INIT_ID_NAME, id);
   // TODO: Note that the behaviour below is closely coupled to the implementation of
   // initialization identifiers in CompiledFunctionService
   final Instant instant = Instant.ofEpochMilli(id);
   put(FUNCTION_INIT_TIMESTAMP_NAME, VersionCorrection.of(instant, instant));
 }
Ejemplo n.º 8
0
  // -------------------------------------------------------------------------
  @Test
  public void test_search_versionAsOf_below() {
    UserSearchRequest request = new UserSearchRequest();
    request.setVersionCorrection(VersionCorrection.ofVersionAsOf(_version1Instant.minusSeconds(5)));
    UserSearchResult test = _usrMaster.search(request);

    assertEquals(0, test.getDocuments().size());
  }
Ejemplo n.º 9
0
  @Test
  public void testGetConfigByOid() {
    final SimpleExchange target = new SimpleExchange();
    target.setName("Test");

    when(_underlying.getConfig(eq(SimpleExchange.class), eq(OID), eq(VC))).thenReturn(target);

    Response test =
        _resource.get(
            OID.toString(),
            SimpleExchange.class.getName(),
            null,
            VC.getVersionAsOfString(),
            VC.getCorrectedToString());
    assertEquals(Status.OK.getStatusCode(), test.getStatus());
    assertSame(target, test.getEntity());
  }
  // -------------------------------------------------------------------------
  @Test
  public void test_search_versionAsOf_below() {
    ConfigSearchRequest<Identifier> request = createIdentifierSearchRequest();
    request.setVersionCorrection(
        VersionCorrection.ofVersionAsOf(_version1aInstant.minusSeconds(5)));
    ConfigSearchResult<Identifier> test = _cfgMaster.search(request);

    assertEquals(0, test.getDocuments().size());
  }
Ejemplo n.º 11
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Test
  public void testSearch() {
    final SimpleExchange target = new SimpleExchange();
    target.setName("Test");
    Collection targetColl = ImmutableList.of(target);

    when(_underlying.getConfigs(eq(SimpleExchange.class), eq(NAME), eq(VC))).thenReturn(targetColl);

    Response test =
        _resource.search(
            SimpleExchange.class.getName(),
            VC.getVersionAsOfString(),
            VC.getCorrectedToString(),
            NAME);
    assertEquals(Status.OK.getStatusCode(), test.getStatus());
    assertEquals(FudgeListWrapper.of(targetColl), test.getEntity());
  }
 @Override
 public Region get(final ObjectId objectId, final VersionCorrection versionCorrection) {
   _count.getAndIncrement();
   Region result = null;
   if (_testRegion.getUniqueId().getObjectId().equals(objectId)
       && versionCorrection.equals(VersionCorrection.LATEST)) {
     result = _testRegion;
   }
   return result;
 }
Ejemplo n.º 13
0
  @Test
  public void test_search_versionAsOf_above() {
    UserSearchRequest request = new UserSearchRequest();
    request.setVersionCorrection(VersionCorrection.ofVersionAsOf(_version2Instant.plusSeconds(5)));
    UserSearchResult test = _usrMaster.search(request);

    assertEquals(3, test.getDocuments().size());
    assert101(test.getDocuments().get(0));
    assert102(test.getDocuments().get(1));
    assert202(test.getDocuments().get(2)); // new version
  }
 // -------------------------------------------------------------------------
 @GET
 @Path("exchanges")
 public Response search(
     @QueryParam("versionAsOf") String versionAsOf,
     @QueryParam("correctedTo") String correctedTo,
     @QueryParam("id") List<String> externalIdStrs) {
   final VersionCorrection vc = VersionCorrection.parse(versionAsOf, correctedTo);
   final ExternalIdBundle bundle = ExternalIdBundle.parse(externalIdStrs);
   Collection<? extends Exchange> result = getExchangeSource().getExchanges(bundle, vc);
   return responseOkFudge(FudgeListWrapper.of(result));
 }
 @Override
 public Collection<Region> get(
     final ExternalIdBundle bundle, final VersionCorrection versionCorrection) {
   _count.getAndIncrement();
   Collection<Region> result = Collections.emptyList();
   if (_testRegion.getExternalIdBundle().equals(bundle)
       && versionCorrection.equals(VersionCorrection.LATEST)) {
     result = Collections.singleton((Region) getTestRegion());
   }
   return result;
 }
 // -------------------------------------------------------------------------
 @Override
 public synchronized YieldCurveDefinition getDefinition(Currency currency, String name) {
   ArgumentChecker.notNull(currency, "currency");
   ArgumentChecker.notNull(name, "name");
   final TreeMap<Instant, YieldCurveDefinition> definitions =
       _definitions.get(Pair.of(currency, name));
   if (definitions == null) {
     return null;
   }
   final Map.Entry<Instant, YieldCurveDefinition> entry;
   if (_sourceVersionCorrection.getVersionAsOf() == null) {
     entry = definitions.lastEntry();
   } else {
     entry = definitions.floorEntry(_sourceVersionCorrection.getVersionAsOf());
   }
   if (entry == null) {
     return null;
   }
   return entry.getValue();
 }
Ejemplo n.º 17
0
 @Override
 public SecurityDocument get(ObjectIdentifiable objectId, VersionCorrection versionCorrection) {
   final RestTarget target = _targetSecurity.resolve(objectId.getObjectId().toString());
   if (versionCorrection != null && versionCorrection.getVersionAsOf() != null) {
     target.resolveQuery(
         "versionAsOf", Collections.singletonList(versionCorrection.getVersionAsOf().toString()));
   }
   if (versionCorrection != null && versionCorrection.getCorrectedTo() != null) {
     target.resolveQuery(
         "correctedTo", Collections.singletonList(versionCorrection.getCorrectedTo().toString()));
   }
   s_logger.debug("get-get to {}", target);
   final FudgeMsg message = getRestClient().getMsg(target);
   if (message == null) {
     s_logger.debug("get-recv NULL");
     throw new DataNotFoundException("Security with identifier " + objectId);
   }
   s_logger.debug("get-recv {}", message);
   return getFudgeDeserializer().fudgeMsgToObject(SecurityDocument.class, message);
 }
  @Test
  public void test_search_versionAsOf_above() {
    ConfigSearchRequest<Identifier> request = createIdentifierSearchRequest();
    request.setVersionCorrection(VersionCorrection.ofVersionAsOf(_version2Instant.plusSeconds(5)));
    ConfigSearchResult<Identifier> test = _cfgMaster.search(request);

    assertEquals(3, test.getDocuments().size());
    assert202(test.getDocuments().get(0)); // new version
    assert102(test.getDocuments().get(1));
    assert101(test.getDocuments().get(2));
  }
 @Override
 public synchronized YieldCurveDefinitionDocument addOrUpdate(
     YieldCurveDefinitionDocument document) {
   ArgumentChecker.notNull(document, "document");
   ArgumentChecker.notNull(document.getYieldCurveDefinition(), "document.yieldCurveDefinition");
   final Currency currency = document.getYieldCurveDefinition().getCurrency();
   final String name = document.getYieldCurveDefinition().getName();
   final Pair<Currency, String> key = Pair.of(currency, name);
   TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
   final UniqueId uid = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
   Instant now = Instant.now();
   if (value != null) {
     if (_sourceVersionCorrection.getVersionAsOf() != null) {
       // Don't need to keep the old values before the one needed by "versionAsOfInstant"
       final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
       if (oldestNeeded != null) {
         value.headMap(oldestNeeded).clear();
       }
     } else {
       // Don't need any old values
       value.clear();
     }
     value.put(now, document.getYieldCurveDefinition());
     changeManager().entityChanged(ChangeType.CHANGED, uid.getObjectId(), null, null, now);
   } else {
     value = new TreeMap<Instant, YieldCurveDefinition>();
     value.put(now, document.getYieldCurveDefinition());
     _definitions.put(key, value);
     changeManager()
         .entityChanged(
             ChangeType.ADDED,
             uid.getObjectId(),
             document.getVersionFromInstant(),
             document.getVersionToInstant(),
             now);
   }
   document.setUniqueId(uid);
   return document;
 }
Ejemplo n.º 20
0
  /**
   * Searches for a configuration document matching the specified name.
   *
   * <p>This will always return the latest correction of the version requested, ignoring any other
   * version constraints of the implementation.
   *
   * @param <T> the type of configuration element
   * @param clazz the configuration element type, not null
   * @param name the element name to search for, wildcards allowed, not null
   * @param versionAsOf the version to fetch, null means latest
   * @return the versioned configuration document matching the request, null if not found
   */
  public <T> ConfigDocument<T> getDocumentByName(
      final Class<T> clazz, final String name, final Instant versionAsOf) {
    ArgumentChecker.notNull(clazz, "clazz");
    ArgumentChecker.notNull(name, "name");

    ConfigSearchRequest<T> request = new ConfigSearchRequest<T>();
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VersionCorrection.ofVersionAsOf(versionAsOf));
    request.setName(name);
    request.setType(clazz);

    ConfigSearchResult<T> searchResult = getMaster().search(request);
    return searchResult.getFirstDocument();
  }
 @GET
 @Path("exchanges/{exchangeId}")
 public Response get(
     @PathParam("exchangeId") String idStr,
     @QueryParam("version") String version,
     @QueryParam("versionAsOf") String versionAsOf,
     @QueryParam("correctedTo") String correctedTo) {
   final ObjectId objectId = ObjectId.parse(idStr);
   if (version != null) {
     final Exchange result = getExchangeSource().getExchange(objectId.atVersion(version));
     return responseOkFudge(result);
   } else {
     final VersionCorrection vc = VersionCorrection.parse(versionAsOf, correctedTo);
     Exchange result = getExchangeSource().getExchange(objectId, vc);
     return responseOkFudge(result);
   }
 }
 @Override
 public YieldCurveDefinition getDefinition(
     final Currency currency, final String name, final VersionCorrection versionCorrection) {
   ArgumentChecker.notNull(currency, "currency");
   ArgumentChecker.notNull(name, "name");
   final TreeMap<Instant, YieldCurveDefinition> definitions =
       _definitions.get(Pair.of(currency, name));
   if (definitions == null) {
     return null;
   }
   final Map.Entry<Instant, YieldCurveDefinition> entry =
       definitions.floorEntry(versionCorrection.getVersionAsOf());
   if (entry == null) {
     return null;
   }
   return entry.getValue();
 }
 @SuppressWarnings("unchecked")
 @Override
 public Collection<V> get(
     final ExternalIdBundle bundle, final VersionCorrection versionCorrection) {
   ArgumentChecker.notNull(bundle, "bundle");
   ArgumentChecker.notNull(versionCorrection, "versionCorrection");
   if (versionCorrection.containsLatest()) {
     Collection<V> results = getUnderlying().get(bundle, versionCorrection);
     cacheItems(results);
     return results;
   }
   final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection);
   final Element e = _eidToUidCache.get(key);
   if (e != null) {
     if (e.getObjectValue() instanceof Collection) {
       final Collection<UniqueId> identifiers = (Collection<UniqueId>) e.getObjectValue();
       if (identifiers.isEmpty()) {
         return Collections.emptySet();
       } else {
         return get(identifiers).values();
       }
     }
   }
   final Collection<V> result = getUnderlying().get(bundle, versionCorrection);
   if (result.isEmpty()) {
     cacheIdentifiers(Collections.<UniqueId>emptyList(), key);
   } else {
     final List<UniqueId> uids = new ArrayList<UniqueId>(result.size());
     for (final V item : result) {
       uids.add(item.getUniqueId());
     }
     Collections.sort(uids);
     cacheIdentifiers(uids, key);
     cacheItems(result);
   }
   return result;
 }
 // -------------------------------------------------------------------------
 @Override
 public synchronized void setVersionCorrection(final VersionCorrection versionCorrection) {
   _sourceVersionCorrection = VersionCorrection.of(versionCorrection);
 }
  @Override
  public List<UniqueId> replaceVersions(
      ObjectIdentifiable objectIdentifiable,
      List<YieldCurveDefinitionDocument> replacementDocuments) {
    ArgumentChecker.notNull(replacementDocuments, "replacementDocuments");
    ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable");

    final Instant now = Instant.now();

    for (YieldCurveDefinitionDocument replacementDocument : replacementDocuments) {
      ArgumentChecker.notNull(replacementDocument, "document");
      ArgumentChecker.notNull(
          replacementDocument.getYieldCurveDefinition(), "document.yieldCurveDefinition");
      final Currency currency = replacementDocument.getYieldCurveDefinition().getCurrency();
      final String name = replacementDocument.getYieldCurveDefinition().getName();
      final UniqueId id = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
      ArgumentChecker.isTrue(id.equals(objectIdentifiable), "Invalid object identifier");
    }

    YieldCurveDefinitionDocument storedDocument = get(objectIdentifiable, null);
    if (storedDocument == null) {
      throw new DataNotFoundException("Document not found: " + objectIdentifiable);
    }
    final Currency currency = storedDocument.getYieldCurveDefinition().getCurrency();
    final String name = storedDocument.getYieldCurveDefinition().getName();
    Pair<Currency, String> key = Pair.of(currency, name);

    final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
    if (value == null) {
      throw new DataNotFoundException("OID '" + objectIdentifiable + "' not found");
    }
    if (_sourceVersionCorrection.getVersionAsOf() != null) {
      // Don't need to keep the old values before the one needed by "versionAsOfInstant"
      final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
      value.headMap(oldestNeeded).clear();
    } else {
      // Don't need any old values
      value.clear();
    }

    Instant lowestCurrentVersionFrom = value.firstKey();

    List<YieldCurveDefinitionDocument> orderedReplacementDocuments =
        MasterUtils.adjustVersionInstants(
            now, lowestCurrentVersionFrom, null, replacementDocuments);

    final Instant lowestVersionFrom = orderedReplacementDocuments.get(0).getVersionFromInstant();
    final Instant highestVersionTo =
        orderedReplacementDocuments
            .get(orderedReplacementDocuments.size() - 1)
            .getVersionToInstant();

    if (orderedReplacementDocuments.size() > 0) {
      value.subMap(lowestVersionFrom, true, highestVersionTo, false).clear();
    }

    for (YieldCurveDefinitionDocument replacementDocument : orderedReplacementDocuments) {
      value.put(
          replacementDocument.getVersionFromInstant(),
          replacementDocument.getYieldCurveDefinition());
      changeManager()
          .entityChanged(ChangeType.CHANGED, replacementDocument.getObjectId(), null, null, now);
    }
    return MasterUtils.mapToUniqueIDs(orderedReplacementDocuments);
  }
 @SuppressWarnings("unchecked")
 @Override
 public Map<ExternalIdBundle, Collection<V>> getAll(
     final Collection<ExternalIdBundle> bundles, VersionCorrection versionCorrection) {
   ArgumentChecker.notNull(bundles, "bundles");
   ArgumentChecker.notNull(versionCorrection, "versionCorrection");
   if (versionCorrection.containsLatest()) {
     return getUnderlying().getAll(bundles, versionCorrection);
   }
   final Map<ExternalIdBundle, Collection<V>> results =
       Maps.newHashMapWithExpectedSize(bundles.size());
   final Collection<ExternalIdBundle> misses = new ArrayList<ExternalIdBundle>(bundles.size());
   final Map<ExternalIdBundle, Collection<UniqueId>> lookupBundles =
       Maps.newHashMapWithExpectedSize(bundles.size());
   final Set<UniqueId> lookupIds = Sets.newHashSetWithExpectedSize(bundles.size());
   for (ExternalIdBundle bundle : bundles) {
     final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection);
     final Element e = _eidToUidCache.get(key);
     if (e != null) {
       if (e.getObjectValue() instanceof Collection) {
         final Collection<UniqueId> identifiers = (Collection<UniqueId>) e.getObjectValue();
         if (identifiers.isEmpty()) {
           results.put(bundle, Collections.<V>emptySet());
         } else {
           lookupBundles.put(bundle, identifiers);
           lookupIds.addAll(identifiers);
         }
         continue;
       }
     }
     misses.add(bundle);
   }
   if (!lookupIds.isEmpty()) {
     final Map<UniqueId, V> underlying = get(lookupIds);
     for (Map.Entry<ExternalIdBundle, Collection<UniqueId>> lookupBundle :
         lookupBundles.entrySet()) {
       final ArrayList<V> resultCollection = new ArrayList<V>(lookupBundle.getValue().size());
       for (UniqueId uid : lookupBundle.getValue()) {
         final V resultValue = underlying.get(uid);
         if (resultValue != null) {
           resultCollection.add(resultValue);
         }
       }
       resultCollection.trimToSize();
       results.put(lookupBundle.getKey(), resultCollection);
     }
   }
   if (!misses.isEmpty()) {
     final Map<ExternalIdBundle, Collection<V>> underlying =
         getUnderlying().getAll(misses, versionCorrection);
     for (ExternalIdBundle miss : misses) {
       final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(miss, versionCorrection);
       final Collection<V> result = underlying.get(miss);
       if ((result == null) || result.isEmpty()) {
         cacheIdentifiers(Collections.<UniqueId>emptyList(), key);
       } else {
         final List<UniqueId> uids = new ArrayList<>(result.size());
         for (final V item : result) {
           uids.add(item.getUniqueId());
         }
         Collections.sort(uids);
         cacheIdentifiers(uids, key);
         cacheItems(result);
         results.put(miss, result);
       }
     }
   }
   return results;
 }
 @Override
 @SuppressWarnings("unchecked")
 public Map<ExternalIdBundle, V> getSingle(
     final Collection<ExternalIdBundle> bundles, final VersionCorrection versionCorrection) {
   ArgumentChecker.notNull(bundles, "bundles");
   ArgumentChecker.notNull(versionCorrection, "versionCorrection");
   if (versionCorrection.containsLatest()) {
     return getUnderlying().getSingle(bundles, versionCorrection);
   }
   final Map<ExternalIdBundle, V> results = Maps.newHashMapWithExpectedSize(bundles.size());
   final Collection<ExternalIdBundle> misses = new ArrayList<ExternalIdBundle>(bundles.size());
   final Map<ExternalIdBundle, Collection<UniqueId>> hits =
       Maps.newHashMapWithExpectedSize(bundles.size());
   final Set<UniqueId> lookup = Sets.newHashSetWithExpectedSize(bundles.size());
   for (ExternalIdBundle bundle : bundles) {
     final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(bundle, versionCorrection);
     final Element e = _eidToUidCache.get(key);
     if (e != null) {
       if (e.getObjectValue() instanceof List) {
         final List<UniqueId> identifiers = (List<UniqueId>) e.getObjectValue();
         lookup.addAll(identifiers);
         hits.put(bundle, identifiers);
         continue;
       } else if (e.getObjectValue() instanceof UniqueId) {
         final UniqueId identifier = (UniqueId) e.getObjectValue();
         lookup.add(identifier);
         hits.put(bundle, Collections.singleton(identifier));
         continue;
       }
     }
     misses.add(bundle);
   }
   if (!lookup.isEmpty()) {
     final Map<UniqueId, V> underlying = getUnderlying().get(lookup);
     for (Map.Entry<ExternalIdBundle, Collection<UniqueId>> hit : hits.entrySet()) {
       final ExternalIdBundle bundle = hit.getKey();
       for (UniqueId uid : hit.getValue()) {
         final V result = underlying.get(uid);
         if (result != null) {
           results.put(bundle, result);
           break;
         }
       }
     }
   }
   if (!misses.isEmpty()) {
     final Map<ExternalIdBundle, ? extends V> underlying =
         getUnderlying().getSingle(misses, versionCorrection);
     for (ExternalIdBundle miss : misses) {
       final Pair<ExternalIdBundle, VersionCorrection> key = Pairs.of(miss, versionCorrection);
       final V result = underlying.get(miss);
       if (result == null) {
         cacheIdentifiers(Collections.<UniqueId>emptyList(), key);
       } else {
         cacheIdentifiers(result.getUniqueId(), key);
         cacheItem(result);
         results.put(miss, result);
       }
     }
   }
   return results;
 }
/** Test {@link MasterExchangeSource}. */
@Test(groups = TestGroup.UNIT)
public class MasterExchangeSourceTest {

  private static final ObjectId OID = ObjectId.of("A", "B");
  private static final UniqueId UID = UniqueId.of("A", "B", "V");
  private static final ExternalId ID = ExternalId.of("C", "D");
  private static final ExternalIdBundle BUNDLE = ExternalIdBundle.of(ID);
  private static final Instant NOW = Instant.now();
  private static final VersionCorrection VC =
      VersionCorrection.of(NOW.minusSeconds(2), NOW.minusSeconds(1));

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_1arg_nullMaster() throws Exception {
    new MasterExchangeSource(null);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_2arg_nullMaster() throws Exception {
    new MasterExchangeSource(null, null);
  }

  // -------------------------------------------------------------------------
  public void test_getExchange_UniqueId_noOverride_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(UID)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock);
    Exchange testResult = test.get(UID);
    verify(mock, times(1)).get(UID);

    assertEquals(example(), testResult);
  }

  public void test_getExchange_UniqueId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(OID, VC)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.get(UID);
    verify(mock, times(1)).get(OID, VC);

    assertEquals(example(), testResult);
  }

  @Test(expectedExceptions = DataNotFoundException.class)
  public void test_getExchange_UniqueId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    when(mock.get(OID, VC)).thenThrow(new DataNotFoundException(""));
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    try {
      test.get(UID);
    } finally {
      verify(mock, times(1)).get(OID, VC);
    }
  }

  // -------------------------------------------------------------------------
  public void test_getExchange_ObjectId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(OID, VC)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.get(OID, VC);
    verify(mock, times(1)).get(OID, VC);

    assertEquals(example(), testResult);
  }

  @Test(expectedExceptions = DataNotFoundException.class)
  public void test_getExchange_ObjectId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    when(mock.get(OID, VC)).thenThrow(new DataNotFoundException(""));
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    try {
      test.get(OID, VC);
    } finally {
      verify(mock, times(1)).get(OID, VC);
    }
  }

  // -------------------------------------------------------------------------
  public void test_getSingleExchange_ExternalId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(ID);
    request.setPagingRequest(PagingRequest.ONE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();
    result.getDocuments().add(new ExchangeDocument(example()));

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(ID);
    verify(mock, times(1)).search(request);

    assertEquals(example(), testResult);
  }

  public void test_getSingleExchange_ExternalId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(ID);
    request.setPagingRequest(PagingRequest.ONE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(ID);
    verify(mock, times(1)).search(request);

    assertEquals(null, testResult);
  }

  // -------------------------------------------------------------------------
  public void test_getSingleExchange_ExternalIdBundle_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(BUNDLE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();
    result.getDocuments().add(new ExchangeDocument(example()));

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(BUNDLE);
    verify(mock, times(1)).search(request);

    assertEquals(example(), testResult);
  }

  // -------------------------------------------------------------------------
  protected ManageableExchange example() {
    ManageableExchange exchange = new ManageableExchange();
    exchange.setUniqueId(UID);
    exchange.setName("NYSE");
    exchange.setRegionIdBundle(ExternalIdBundle.of(ExternalSchemes.countryRegionId(Country.US)));
    return exchange;
  }
}
Ejemplo n.º 29
0
  private Future<?> executeTestJob(DependencyGraphExecutorFactory<?> factory) {
    final InMemoryLKVMarketDataProvider marketDataProvider = new InMemoryLKVMarketDataProvider();
    final MarketDataProviderResolver marketDataProviderResolver =
        new SingleMarketDataProviderResolver(
            new SingletonMarketDataProviderFactory(marketDataProvider));
    final InMemoryFunctionRepository functionRepository = new InMemoryFunctionRepository();
    _functionCount.set(0);
    final MockFunction mockFunction =
        new MockFunction(new ComputationTarget("Foo")) {

          @Override
          public Set<ComputedValue> execute(
              FunctionExecutionContext executionContext,
              FunctionInputs inputs,
              ComputationTarget target,
              Set<ValueRequirement> desiredValues) {
            try {
              Thread.sleep(JOB_FINISH_TIME / (JOB_SIZE * 2));
            } catch (InterruptedException e) {
              throw new OpenGammaRuntimeException("Function interrupted", e);
            }
            _functionCount.incrementAndGet();
            return super.execute(executionContext, inputs, target, desiredValues);
          }
        };
    functionRepository.addFunction(mockFunction);
    final FunctionCompilationContext compilationContext = new FunctionCompilationContext();
    final CompiledFunctionService compilationService =
        new CompiledFunctionService(
            functionRepository, new CachingFunctionRepositoryCompiler(), compilationContext);
    compilationService.initialize();
    final FunctionResolver functionResolver = new DefaultFunctionResolver(compilationService);
    final MockSecuritySource securitySource = new MockSecuritySource();
    final MockPositionSource positionSource = new MockPositionSource();
    final ViewComputationCacheSource computationCacheSource =
        new InMemoryViewComputationCacheSource(FudgeContext.GLOBAL_DEFAULT);
    final FunctionInvocationStatisticsGatherer functionInvocationStatistics =
        new DiscardingInvocationStatisticsGatherer();
    final ViewProcessorQueryReceiver viewProcessorQueryReceiver = new ViewProcessorQueryReceiver();
    final ViewProcessorQuerySender viewProcessorQuerySender =
        new ViewProcessorQuerySender(InMemoryRequestConduit.create(viewProcessorQueryReceiver));
    final FunctionExecutionContext executionContext = new FunctionExecutionContext();
    final ComputationTargetResolver targetResolver =
        new DefaultComputationTargetResolver(securitySource, positionSource);
    final JobDispatcher jobDispatcher =
        new JobDispatcher(
            new LocalNodeJobInvoker(
                new LocalCalculationNode(
                    computationCacheSource,
                    compilationService,
                    executionContext,
                    targetResolver,
                    viewProcessorQuerySender,
                    Executors.newCachedThreadPool(),
                    functionInvocationStatistics)));
    final ViewPermissionProvider viewPermissionProvider = new DefaultViewPermissionProvider();
    final GraphExecutorStatisticsGathererProvider graphExecutorStatisticsProvider =
        new DiscardingGraphStatisticsGathererProvider();

    ViewDefinition viewDefinition = new ViewDefinition("TestView", UserPrincipal.getTestUser());
    viewDefinition.addViewCalculationConfiguration(
        new ViewCalculationConfiguration(viewDefinition, "default"));
    MockViewDefinitionRepository viewDefinitionRepository = new MockViewDefinitionRepository();
    viewDefinitionRepository.addDefinition(viewDefinition);

    final ViewProcessContext vpc =
        new ViewProcessContext(
            viewDefinitionRepository,
            viewPermissionProvider,
            marketDataProviderResolver,
            compilationService,
            functionResolver,
            positionSource,
            securitySource,
            new DefaultCachingComputationTargetResolver(
                new DefaultComputationTargetResolver(securitySource, positionSource),
                EHCacheUtils.createCacheManager()),
            computationCacheSource,
            jobDispatcher,
            viewProcessorQueryReceiver,
            factory,
            graphExecutorStatisticsProvider);
    final DependencyGraph graph = new DependencyGraph("Default");
    DependencyNode previous = null;
    for (int i = 0; i < JOB_SIZE; i++) {
      DependencyNode node = new DependencyNode(new ComputationTarget("Foo"));
      node.setFunction(mockFunction);
      if (previous != null) {
        node.addInputNode(previous);
      }
      graph.addDependencyNode(node);
      previous = node;
    }
    final Map<String, DependencyGraph> graphs = new HashMap<String, DependencyGraph>();
    graphs.put(graph.getCalculationConfigurationName(), graph);
    CompiledViewDefinitionWithGraphsImpl viewEvaluationModel =
        new CompiledViewDefinitionWithGraphsImpl(
            viewDefinition, graphs, new SimplePortfolio("Test Portfolio"), 0);
    ViewCycleExecutionOptions cycleOptions = new ViewCycleExecutionOptions();
    cycleOptions.setValuationTime(Instant.ofEpochMillis(1));
    cycleOptions.setMarketDataSpecification(new MarketDataSpecification());
    final SingleComputationCycle cycle =
        new SingleComputationCycle(
            UniqueId.of("Test", "Cycle1"),
            UniqueId.of("Test", "ViewProcess1"),
            vpc,
            viewEvaluationModel,
            cycleOptions,
            VersionCorrection.of(Instant.ofEpochMillis(1), Instant.ofEpochMillis(1)));
    return cycle.getDependencyGraphExecutor().execute(graph, cycle.getStatisticsGatherer());
  }