def download_data(filename, merge, java_application):
  """Download appstats data from memcache."""
  oldrecords = []
  oldfile = None



  if merge:
    try:
      oldfile = open(filename, 'rb')
    except __HOLE__:
      logging.info('No file to merge. Creating new file %s',
                   filename)
  if oldfile:
    logging.info('Merging with existing file %s', filename)
    oldrecords = loader.UnpickleFromFile(oldfile)
    oldfile.close()
  if oldrecords:

    last_timestamp = oldrecords[0].start_timestamp_milliseconds()
    records = loader.FromMemcache(filter_timestamp=last_timestamp,
                                  java_application=java_application)
  else:
    records = loader.FromMemcache(java_application=java_application)

  merged_records = records + oldrecords
  try:
    outfile = open(filename, 'wb')
  except IOError:
    logging.error('Cannot open %s', filename)
    return
  loader.PickleToFile(merged_records, outfile)
  outfile.close()
dataset/ETHPy150Open AppScale/appscale/AppServer/google/appengine/tools/download_appstats.py/download_data