docstr: Finds the next available `dataset_idx` given a key.
  
  Indices start a 0 and go up until the first unused index is found.
  
  Will attempt to reuse earlier indices if they are no longer in use. This balances between
  supporting multiple concurrent datasets being trained at once (multiple dataset indices),
  and avoiding overuse of disk space (reclaiming unused datasets when no longer needed).
  
  NOTE: this method is not thread-safe. You must wrap usage with `cache.lock` if using
  in a multi-threaded setting (see `prepare_data`).
docstr_tok: ["Finds","the","next","available","`","dataset_idx","`","given","a","key",".","Indices","start","a","0","and","go","up","until","the","first","unused","index","is","found",".","Will","attempt","to","reuse","earlier","indices","if","they","are","no","longer","in","use",".","This","balances","between","supporting","multiple","concurrent","datasets","being","trained","at","once","(","multiple","dataset","indices",")",",","and","avoiding","overuse","of","disk","space","(","reclaiming","unused","datasets","when","no","longer","needed",")",".","NOTE",":","this","method","is","not","thread-safe",".","You","must","wrap","usage","with","`","cache.lock","`","if","using","in","a","multi-threaded","setting","(","see","`","prepare_data","`",")","."]
code: def next_dataset_index(self, key):
      """Finds the next available `dataset_idx` given a key.
  
      Indices start a 0 and go up until the first unused index is found.
  
      Will attempt to reuse earlier indices if they are no longer in use. This balances between
      supporting multiple concurrent datasets being trained at once (multiple dataset indices),
      and avoiding overuse of disk space (reclaiming unused datasets when no longer needed).
  
      NOTE: this method is not thread-safe. You must wrap usage with `cache.lock` if using
      in a multi-threaded setting (see `prepare_data`).
      """
      idx = 0
      while True:
          last_key = self._dataset_to_key.get(idx)
          if self._keys_in_use[last_key] > 0:
              # Paths are in use, try the next index
              idx += 1
              continue
  
          self._dataset_to_key[idx] = key
          self._key_to_dataset[key] = idx
          return idx
  
file_path: horovod/horovod/spark/common/cache.py
imports: import collections
  import contextlib
  import threading
label_desc: Use image node_id 4 for calling the TrainingDataCache obj's underlying member method code with example usage: obj.next_dataset_index(key) and returns: idx