<commit_before>--- Provides a data storage facility with an ability to get sub-stores. So you can write
-- directly to this store, overwriting all children, or you can have more partial control
-- at children level. This minimizes accidently overwriting.
-- The big cost here is that we may leave keys that can't be removed.
-- @classmod DataStoreStage

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local BaseObject = require("BaseObject")
local Table = require("Table")
local DataStoreWriter = require("DataStoreWriter")
local DataStoreDeleteToken = require("DataStoreDeleteToken")

local DataStoreStage = setmetatable({}, BaseObject)
DataStoreStage.ClassName = "DataStoreStage"
DataStoreStage.__index = DataStoreStage

function DataStoreStage.new(loadName, loadParent)
	local self = setmetatable(BaseObject.new(), DataStoreStage)

	-- LoadParent is optional, used for loading
	self._loadName = loadName
	self._loadParent = loadParent

	self._takenKeys = {} -- [name] = true
	self._stores = {}

	return self
end

function DataStoreStage:Load(name, defaultValue)
	if not self._loadParent then
		error("[DataStoreStage.Load] - Failed to load, no loadParent!")
	end
	if not self._loadName then
		error("[DataStoreStage.Load] - Failed to load, no loadName!")
	end

	return self._loadParent:Load(self._loadName, {}):Then(function(data)
		if data[name] == nil then
			return defaultValue
		else
			return data[name]
		end
	end)
end

function DataStoreStage:Delete(name)
	if self._takenKeys[name] then
		error(("[DataStoreStage] - Already have a writer for %q"):format(name))
	end

	self:_doStore(name, nil)
end

function DataStoreStage:Store(name, value)
	if self._takenKeys[name] then
		error(("[DataStoreStage] - Already have a writer for %q"):format(name))
	end

	if value == nil then
		value = DataStoreDeleteToken
	end

	self:_doStore(name, value)
end

function DataStoreStage:GetSubStore(name)<commit_msg>Datastore loads data that is already written if written<commit_after>--- Provides a data storage facility with an ability to get sub-stores. So you can write
-- directly to this store, overwriting all children, or you can have more partial control
-- at children level. This minimizes accidently overwriting.
-- The big cost here is that we may leave keys that can't be removed.
-- @classmod DataStoreStage

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local BaseObject = require("BaseObject")
local DataStoreDeleteToken = require("DataStoreDeleteToken")
local DataStoreWriter = require("DataStoreWriter")
local Promise = require("Promise")
local Table = require("Table")

local DataStoreStage = setmetatable({}, BaseObject)
DataStoreStage.ClassName = "DataStoreStage"
DataStoreStage.__index = DataStoreStage

function DataStoreStage.new(loadName, loadParent)
	local self = setmetatable(BaseObject.new(), DataStoreStage)

	-- LoadParent is optional, used for loading
	self._loadName = loadName
	self._loadParent = loadParent

	self._takenKeys = {} -- [name] = true
	self._stores = {} -- [name] = dataSubStore

	return self
end

function DataStoreStage:Load(name, defaultValue)
	if not self._loadParent then
		error("[DataStoreStage.Load] - Failed to load, no loadParent!")
	end
	if not self._loadName then
		error("[DataStoreStage.Load] - Failed to load, no loadName!")
	end

	if self._dataToSave and self._dataToSave[name] ~= nil then
		if self._dataToSave[name] == DataStoreDeleteToken then
			return Promise.resolved(defaultValue)
		else
			return Promise.resolved(self._dataToSave[name])
		end
	end

	return self._loadParent:Load(self._loadName, {}):Then(function(data)
		if self._dataToSave and self._dataToSave[name] ~= nil then
			if self._dataToSave[name] == DataStoreDeleteToken then
				return defaultValue
			else
				return self._dataToSave[name]
			end
		elseif self._stores[name] then
			if self._stores[name]:HasWritableData() then
				local writer = self._stores[name]:GetNewWriter()
				local original = Table.DeepCopy(data[name] or {})
				writer:WriteMerge(original)
				return original
			end
		end

		if data[name] == nil then
			return defaultValue
		else
			return data[name]
		end
	end)
end

function DataStoreStage:Delete(name)
	if self._takenKeys[name] then
		error(("[DataStoreStage] - Already have a writer for %q"):format(name))
	end

	self:_doStore(name, nil)
end

function DataStoreStage:Store(name, value)
	if self._takenKeys[name] then
		error(("[DataStoreStage] - Already have a writer for %q"):format(name))
	end

	if value == nil then
		value = DataStoreDeleteToken
	end

	self:_doStore(name, value)
end

function DataStoreStage:GetSubStore(name)
{'fraction_non_alphanumeric': 0.09944396920444824, 'fraction_numerical': 0.0, 'mean_word_length': 5.08984375, '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': '4812677', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1391, 'n_words': 427}