<commit_before>package com.beijunyi.parallelgit.filesystem.io;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.beijunyi.parallelgit.filesystem.GfsObjectService;
import com.beijunyi.parallelgit.utils.io.GitFileEntry;
import com.beijunyi.parallelgit.utils.io.ObjectSnapshot;
import com.beijunyi.parallelgit.utils.io.TreeSnapshot;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.FileMode;

import static org.eclipse.jgit.lib.FileMode.TREE;

public class DirectoryNode extends Node<TreeSnapshot> {

  private ConcurrentMap<String, Node> children;

  protected DirectoryNode(@Nullable AnyObjectId id, @Nonnull GfsObjectService gds) {
    super(id, gds);
  }

  @Nonnull
  public static DirectoryNode fromObject(@Nonnull AnyObjectId id, @Nonnull GfsObjectService gds) {
    return new DirectoryNode(id, gds);
  }

  @Nonnull
  public static DirectoryNode newDirectory(@Nonnull GfsObjectService gds) {
    DirectoryNode ret = new DirectoryNode(null, gds);
    ret.setupEmptyDirectory();
    return ret;
  }

  @Override
  public long getSize() throws IOException {
    return 0;
  }

  @Nonnull
  @Override
  public FileMode getMode() {
    return TREE;
  }

  @Override
  public void setMode(@Nonnull FileMode mode) {
    if(mode.equals(TREE))
      throw new IllegalArgumentException(mode.toString());
  }

  @Override
  public synchronized void reset(@Nonnull AnyObjectId id) {
    this.id = id;
    children = null;
  }

  @Nullable
  @Override
  public TreeSnapshot loadSnapshot() throws IOException {
    return id != null ? gds.readTree(id) : null;
  }

  @Nullable
  @Override
  public TreeSnapshot takeSnapshot(boolean persist, boolean allowEmpty) throws IOException {
    if(!isInitialized())
      return null;
    Map<String, GitFileEntry> entries = new HashMap<>();
    for(Map.Entry<String, Node> child : children.entrySet()) {
      Node node = child.getValue();
      ObjectSnapshot snapshot = node.takeSnapshot(persist, false);
      AnyObjectId id = snapshot != null ? snapshot.getId() : node.getObjectId();
      if(id != null)
        entries.put(child.getKey(), new GitFileEntry(id, node.getMode()));
    }
    TreeSnapshot ret = TreeSnapshot.capture(entries, false);
    if(persist)
      id = ret != null ? gds.write(ret) : null;<commit_msg>fixing take snapshot allow empty<commit_after>package com.beijunyi.parallelgit.filesystem.io;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.beijunyi.parallelgit.filesystem.GfsObjectService;
import com.beijunyi.parallelgit.utils.io.GitFileEntry;
import com.beijunyi.parallelgit.utils.io.ObjectSnapshot;
import com.beijunyi.parallelgit.utils.io.TreeSnapshot;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.FileMode;

import static org.eclipse.jgit.lib.FileMode.TREE;

public class DirectoryNode extends Node<TreeSnapshot> {

  private ConcurrentMap<String, Node> children;

  protected DirectoryNode(@Nullable AnyObjectId id, @Nonnull GfsObjectService gds) {
    super(id, gds);
  }

  @Nonnull
  public static DirectoryNode fromObject(@Nonnull AnyObjectId id, @Nonnull GfsObjectService gds) {
    return new DirectoryNode(id, gds);
  }

  @Nonnull
  public static DirectoryNode newDirectory(@Nonnull GfsObjectService gds) {
    DirectoryNode ret = new DirectoryNode(null, gds);
    ret.setupEmptyDirectory();
    return ret;
  }

  @Override
  public long getSize() throws IOException {
    return 0;
  }

  @Nonnull
  @Override
  public FileMode getMode() {
    return TREE;
  }

  @Override
  public void setMode(@Nonnull FileMode mode) {
    if(mode.equals(TREE))
      throw new IllegalArgumentException(mode.toString());
  }

  @Override
  public synchronized void reset(@Nonnull AnyObjectId id) {
    this.id = id;
    children = null;
  }

  @Nullable
  @Override
  public TreeSnapshot loadSnapshot() throws IOException {
    return id != null ? gds.readTree(id) : null;
  }

  @Nullable
  @Override
  public TreeSnapshot takeSnapshot(boolean persist, boolean allowEmpty) throws IOException {
    if(!isInitialized())
      return null;
    Map<String, GitFileEntry> entries = new HashMap<>();
    for(Map.Entry<String, Node> child : children.entrySet()) {
      Node node = child.getValue();
      ObjectSnapshot snapshot = node.takeSnapshot(persist, false);
      AnyObjectId id = snapshot != null ? snapshot.getId() : node.getObjectId();
      if(id != null)
        entries.put(child.getKey(), new GitFileEntry(id, node.getMode()));
    }
    TreeSnapshot ret = TreeSnapshot.capture(entries, allowEmpty);
    if(persist)
      id = ret != null ? gds.write(ret) : null;
{'fraction_non_alphanumeric': 0.10530546623794212, 'fraction_numerical': 0.00040192926045016077, 'mean_word_length': 4.222455403987408, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4159136', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1497, 'n_words': 357}