<commit_before>defmodule MixEx.Registry do
	use GenServer

	# Client API

	@doc """
	Starts the registry
	"""
	def start_link(opts \\ []) do
		GenServer.start_link(__MODULE__, :ok, opts)
	end

	@doc """
	Looks up the bucket pid for `name` stored in `server`.

	Returns `{:ok, pid}` if the bucket exists, `:error` otherwise.
	"""
	def lookup(server, name) do
		GenServer.call(server, {:lookup, name})
	end

	@doc """
	Ensures there is a bucket associated to the given `name` in `server`
	"""
	def create(server, name) do
		GenServer.cast(server, {:create, name})
	end

	@doc """
	Stops the registry
	"""
	def stop(server) do
		GenServer.cast(server, :stop)
	end

	# Server Callbacks

	def init(:ok) do
		{:ok, HashDict.new}
	end

	def handle_call({:lookup, name}, _from, names) do
		{:reply, HashDict.fetch(names, name), names}
	end

	def handle_call(:stop, _from, state) do
		{:stop, :normal, :ok, state}
	end

	def handle_cast({:create, name}, names) do
		if HashDict.has_key?(names, name) do
			{:noreply, names}
		else
			{:ok, bucket} = MixEx.Bucket.start_link
			{:noreply, HashDict.put(names, name, bucket)}
		end
	end
end<commit_msg>Monitor buckets from registry and store reference to name mapping<commit_after>defmodule MixEx.Registry do
	use GenServer

	# Client API

	@doc """
	Starts the registry
	"""
	def start_link(opts \\ []) do
		GenServer.start_link(__MODULE__, :ok, opts)
	end

	@doc """
	Looks up the bucket pid for `name` stored in `server`.

	Returns `{:ok, pid}` if the bucket exists, `:error` otherwise.
	"""
	def lookup(server, name) do
		GenServer.call(server, {:lookup, name})
	end

	@doc """
	Ensures there is a bucket associated to the given `name` in `server`
	"""
	def create(server, name) do
		GenServer.cast(server, {:create, name})
	end

	@doc """
	Stops the registry
	"""
	def stop(server) do
		GenServer.cast(server, :stop)
	end

	# Server Callbacks

	def init(:ok) do
		names = HashDict.new
		refs = HashDict.new
		{:ok, {names, refs}}
	end

	def handle_call({:lookup, name}, _from, {names, _}) do
		{:reply, HashDict.fetch(names, name), names}
	end

	def handle_call(:stop, _from, state) do
		{:stop, :normal, :ok, state}
	end

	def handle_cast({:create, name}, {names, refs}) do
		if HashDict.has_key?(names, name) do
			{:noreply, {names, refs}}
		else
			{:ok, pid} = MixEx.Bucket.start_link
			ref = Process.monitor(pid)
			refs = HashDict.put(refs, ref, name)
			names = HashDict.put(names, name, pid)
			{:noreply, {names, refs}}
		end
	end
end
{'fraction_non_alphanumeric': 0.14429260450160772, 'fraction_numerical': 0.0, 'mean_word_length': 3.958167330677291, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2188879', 'n_tokens_mistral': 983, 'n_tokens_neox': 906, 'n_words': 280}