public synchronized void gatherLatestDatapointsLocking(long catId, int history) {
    waitForLock();
    mDatapointCache.populateLatest(catId, history);
    TimeSeries ts = getSeriesByIdNonlocking(catId);
    if (ts == null) {
      unlock();
      return;
    }

    if (ts.getDbRow().getSynthetic() == false) {
      ts.clearSeries();

      EntryDbTable.Row entry = mDbh.fetchLastCategoryEntry(catId);
      if (entry != null) {
        ArrayList<Datapoint> l = mDatapointCache.getLast(catId, history);
        if (l == null
            || l.size() < 1
            || entry.getTimestamp() > l.get(0).mMillis
            || entry.getValue() != l.get(0).mValue.y) {
          mDatapointCache.clearCache(catId);
          mDatapointCache.populateLatest(catId, history);
          l = mDatapointCache.getLast(catId, history);
        }

        l = aggregateDatapoints(l, ts.getDbRow().getType());
        ts.setDatapoints(null, l, null, true);
      }
    }

    unlock();
  }