Sync the chunk cache to storage.
493547
def Flush(self):
    """"""
    if self._dirty:
      self.Set(self.Schema.SIZE(self.size))
      if self.content_last is not None:
        self.Set(self.Schema.CONTENT_LAST, self.content_last)

    # Flushing the cache will write all chunks to the blob store.
    self.chunk_cache.Flush()
    super(AFF4ImageBase, self).Flush()
553004
function syncThroughChunks (prevChunk) {
    // all already synced?
    if (index > networkIndex) {
      return deferred.resolve()
    }

    function cachedGetHeader (id) {
      if (prevChunk === undefined ||
          !_.isNumber(id) ||
          Math.floor(id / 2016) !== index - 1) {
        return self.getHeader(id)
      }

      var idx = id % 2016
      var rawHeader = prevChunk.slice(idx * 80, (idx + 1) * 80)
      return Promise.resolve(_.extend(util.buffer2header(rawHeader), {
        height: id,
        hash: util.hashEncode(util.sha256x2(rawHeader))
      }))
    }

    // get not verified chunk for index
    self.connector.headersQuery(index * 2016, {count: 2016})
      .then(function (res) {
        var chunkHex = res.headers
        var prevHeaderPromise = index === 0
          ? {hash: util.zfill('', 64)} // special case for zero header
          : cachedGetHeader(index * 2016 - 1)

        return Promise.all([prevHeaderPromise, new Buffer(chunkHex, 'hex')])
      })
      .spread(function (prevHeader, rawChunk) {
        // compare hashPrevBlock of first header from chunk
        //  and derease index for blockchain reorg if not equal
        var firstHeader = util.buffer2header(rawChunk.slice(0, 80))
        if (firstHeader.hashPrevBlock !== prevHeader.hash) {
          index -= 1
          return timers.setImmediate(syncThroughChunks)
        }

        // remember for verifyHeader
        var prevHeaderHash = prevHeader.hash

        // calculate hash of last header from chunk for blockchain and storage
        var lastHash = util.hashEncode(util.sha256x2(rawChunk.slice(-80)))

        // calculate target for index
        return getTarget(index, headersChain, cachedGetHeader)
          .then(function (target) {
            // verify headers in chunk
            _.range(0, rawChunk.length, 80).forEach(function (offset) {
              var rawHeader = rawChunk.slice(offset, offset + 80)
              var currentHash = util.hashEncode(util.sha256x2(rawHeader))
              var currentHeader = util.buffer2header(rawHeader)

              verifyHeader(currentHash, currentHeader,
                           prevHeaderHash, prevHeader, target, self._isTestnet)

              prevHeaderHash = currentHash
              prevHeader = currentHeader
            })

            // set last hash to storage
            var promises = [self.storage.setLastHash(lastHash)]

            // truncate chunk hashes and headers if compact mode supported
            if (self.storage.compactMode) {
              promises.push(self.storage.truncateChunkHashes(index))
              promises.push(self.storage.truncateHeaders(0))
            }
            // truncate headers if compact mode not supported
            //  affected only when reorg needed
            if (!self.storage.compactMode) {
              promises.push(self.storage.truncateHeaders(index * 2016))
            }

            // wait all storage queries
            return Promise.all(promises)
          })
          .then(function () {
            // save chunk hash or headers
            if (self.storage.compactMode && rawChunk.length === 2016 * 80) {
              var chunkHash = util.hashEncode(util.sha256x2(rawChunk))
              return self.storage.putChunkHashes([chunkHash])

            } else {
              var headers = _.range(0, rawChunk.length, 80).map(function (offset) {
                return rawChunk.slice(offset, offset + 80).toString('hex')
              })
              return self.storage.putHeaders(headers)

            }
          })
          .then(function () {
            // update block hash and height for blockchain
            self.latest = {
              hash: lastHash,
              height: index * 2016 + rawChunk.length / 80 - 1
            }
            self.emit('newBlock', self.latest.hash, self.latest.height)

            // increase chunk index
            index += 1

            // sync next chunk
            timers.setImmediate(syncThroughChunks, rawChunk)
          })
      })
      .catch(function (err) { deferred.reject(err) })
  }
877401
private synchronized void refreshCachedData() throws IOException {
    IOUtils.closeStream(committedTxnId);
    
    File currentDir = journalStorage.getSingularStorageDir().getCurrentDir();
    this.lastPromisedEpoch = new PersistentLongFile(
        new File(currentDir, LAST_PROMISED_FILENAME), 0);
    this.lastWriterEpoch = new PersistentLongFile(
        new File(currentDir, LAST_WRITER_EPOCH), 0);
    this.committedTxnId = new BestEffortLongFile(
        new File(currentDir, COMMITTED_TXID_FILENAME),
        HdfsConstants.INVALID_TXID);
    metrics.lastWriterEpoch.set(lastWriterEpoch.get());
  }
301920
def update_hash(cls, filelike, digest):
    """"""
    block_size = digest.block_size * 1024
    for chunk in iter(lambda: filelike.read(block_size), b''):
      digest.update(chunk)
655124
function createChunkCache(chunks, capacity)
{
  var chunkCache;
  var onLoadComplete;
  var index;
  var length;

  // create a chunk cache
  chunkCache = new ChunkCache(capacity);

  // every time a chunk is loaded, add it to the cache
  // TODO: should the caching be based on most recently 'used' or most recently
  // 'loaded'?
  onLoadComplete = function(err, chunk)
  {
    if (!err)
    {
      chunkCache.put(chunk);
    }
  };

  // subscribe to the 'loadcomplete' event on all the chunks
  for (index = 0, length = chunks.length; index < length; index++)
  {
    chunks[index].on('loadcomplete', onLoadComplete);
  }

  // TODO: do we need to unsubscribe from the loadcomplete event at some point?

  return chunkCache;
}
1018443
def _GetChunkForReading(self, chunk):
    """"""
    offset = chunk * self._HASH_SIZE
    self.index.seek(offset)

    chunk_name = self.index.read(self._HASH_SIZE)

    try:
      return self.chunk_cache.Get(chunk_name)
    except KeyError:
      pass

    # We don't have this chunk already cached. The most common read
    # access pattern is contiguous reading so since we have to go to
    # the data store already, we read ahead to reduce round trips.
    self.index.seek(offset)
    readahead = []

    for _ in range(self._READAHEAD):
      name = self.index.read(self._HASH_SIZE)
      if name and name not in self.chunk_cache:
        readahead.append(rdf_objects.BlobID.FromBytes(name))

    self._ReadChunks(readahead)
    try:
      return self.chunk_cache.Get(chunk_name)
    except KeyError:
      raise aff4.ChunkNotFoundError("Cannot open chunk %s" % chunk)
772396
def refresh(self):
        """"""
        if self._source_tree.cache_is_stale():
            self._source_tree.refresh()
            logging.info('Refreshing object tree')
            self._init_cache()
            self.sync_cache_with_source_tree()
348939
private void setBufferToCurrentChunkIfPossible() {
      ChunkCacheKey key = new ChunkCacheKey(fileKey.getIndexName(), filename, currentLoadedChunk, chunkSize, affinitySegmentId);
      buffer = (byte[]) chunksCache.get(key);
      if (buffer == null) {
         currentLoadedChunk--;
         bufferPosition = chunkSize;
      }
      else {
         currentBufferSize = buffer.length;
      }
   }
56087
def sync(self):
        """"""
        self.elk.send(az_encode())
        self.elk.send(zd_encode())
        self.elk.send(zp_encode())
        self.elk.send(zs_encode())
        self.get_descriptions(TextDescriptions.ZONE.value)
60144
def write(self, data):
        """"""
        for chunk in chunks(data, 512):
            self.wait_to_write()
            self.comport.write(chunk)
        self.comport.flush()
272321
def _process_cache(self, d, path=()):
        """"""
        for k, v in d.iteritems():
            if not isinstance(v, dict):
                self.metrics.append((path + (k,), v))
            else:
                self._process_cache(v, path + (k,))
365426
def __sync(self):
    """"""
    pad_length = _BLOCK_SIZE - self.__reader.tell() % _BLOCK_SIZE
    if pad_length and pad_length != _BLOCK_SIZE:
      data = self.__reader.read(pad_length)
      if len(data) != pad_length:
        raise EOFError('Read %d bytes instead of %d' %
                       (len(data), pad_length))
687007
def FlushCache(self):
    """"""
    self._cache = b''
    self._cache_start_offset = None
    self._cache_end_offset = None
    self._ResetDecompressorState()
243285
def synchronize! updater
      source_hash[updater.filename] = source_hash[updater.filename].synchronize(updater)
    end
1168250
def rpc_chain_sync(server_state, new_block_height, finish_time):
    
    rpc_srv = server_state['rpc']
    if rpc_srv is not None:
        rpc_srv.cache_flush()
        rpc_srv.set_last_index_time(finish_time)
936952
def sync(self, vault_client):
        """"""
        if self.present:
            if not self.existing:
                LOG.info("Mounting %s backend on %s",
                         self.backend, self.path)
                self.actually_mount(vault_client)
            else:
                LOG.info("%s backend already mounted on %s",
                         self.backend, self.path)
        else:
            if self.existing:
                LOG.info("Unmounting %s backend on %s",
                         self.backend, self.path)
                self.unmount(vault_client)
            else:
                LOG.info("%s backend already unmounted on %s",
                         self.backend, self.path)

        if self.present and vault_client.version:
            self.sync_tunables(vault_client)
480289
def sync_in_files(self, force=False):
        """"""
        self.log('---- Sync Files ----')

        self.dstate = self.STATES.BUILDING

        for f in self.build_source_files:

            if self.source_fs.exists(f.record.path):
                # print f.path, f.fs_modtime, f.record.modified, f.record.source_hash, f.fs_hash
                if f.fs_is_newer or force:
                    self.log('Sync: {}'.format(f.record.path))
                    f.fs_to_record()

        self.commit()