code: private LogMetadata updateMetadata(LogMetadata currentMetadata, LedgerHandle newLedger, boolean clearEmptyLedgers) throws DurableDataLogException {
          boolean create = currentMetadata == null;
          if (create) {
              // This is the first ledger ever in the metadata.
              currentMetadata = new LogMetadata(newLedger.getId());
          } else {
              currentMetadata = currentMetadata.addLedger(newLedger.getId());
              if (clearEmptyLedgers) {
                  // Remove those ledgers from the metadata that are empty.
                  currentMetadata = currentMetadata.removeEmptyLedgers(Ledgers.MIN_FENCE_LEDGER_COUNT);
              }
          }
  
          try {
              persistMetadata(currentMetadata, create);
          } catch (DurableDataLogException ex) {
              try {
                  Ledgers.delete(newLedger.getId(), this.bookKeeper);
              } catch (Exception deleteEx) {
                  log.warn("{}: Unable to delete newly created ledger {}.", this.traceObjectId, newLedger.getId(), deleteEx);
                  ex.addSuppressed(deleteEx);
              }
  
              throw ex;
          }
  
          log.info("{} Metadata updated ({}).", this.traceObjectId, currentMetadata);
          return currentMetadata;
      }
docs: Updates the metadata and persists it as a result of adding a new Ledger.
  
  @param currentMetadata   The current metadata.
  @param newLedger         The newly added Ledger.
  @param clearEmptyLedgers If true, the new metadata will not not contain any pointers to empty Ledgers. Setting this
  to true will not remove a pointer to the last few ledgers in the Log (controlled by
  Ledgers.MIN_FENCE_LEDGER_COUNT), even if they are indeed empty (this is so we don't interfere
  with any ongoing fencing activities as another instance of this Log may not have yet been
  fenced out).
  @return A new instance of the LogMetadata, which includes the new ledger.
  @throws DurableDataLogException If an Exception occurred.