<commit_before><?php

namespace Flatbase\Handler;

use Flatbase\Query\InsertQuery;
use Flatbase\Query\Query;
use Flatbase\Query\ReadQuery;

class InsertQueryHandler extends QueryHandler
{
    public function handle(InsertQuery $query)
    {
        // Validate the query
        $this->validateQuery($query);

        // Read the existing data
        $readQuery = new ReadQuery();
        $readQuery->setCollection($query->getCollection());
        $existing = $this->flatbase->execute($readQuery);

        // Append the new item to it
        $existing->append($query->getValues());

        // Write back to the storage
        $this->write($query->getCollection(), (array) $existing);
    }

    protected function validateQuery(Query $query)
    {
        parent::validateQuery($query);

        if (!$query->getValues()) {<commit_msg>Refactored insert queries to not require deserializing of the existing file. Improves speed of writes by around 10x<commit_after><?php

namespace Flatbase\Handler;

use Flatbase\Query\InsertQuery;
use Flatbase\Query\Query;
use Flatbase\Query\ReadQuery;

class InsertQueryHandler extends QueryHandler
{
    public function handle(InsertQuery $query)
    {
        // Validate the query
        $this->validateQuery($query);

        $file = $this->flatbase->getStorage()->storageDir . '/' . $query->getCollection();
        $serialized = file_get_contents($file);
        $this->appendToSerializedString($serialized, $query->getValues());

        file_put_contents($file, $serialized);
    }

    protected function appendToSerializedString(&$serialized, $item)
    {
        // Calculate the number of items in the serialized string
        $colonsEncountered = 0;
        $count = '';
        for ($i=0; $i<=10; $i++) {
            if ($colonsEncountered === 2) {
                break;
            }
            if ($colonsEncountered === 1 && $serialized[$i] !== ':') {
                $count .= $serialized[$i];
            }
            if ($serialized[$i] === ':') {
                $colonsEncountered++;
            }
        }

        // Add the new item to the end of the serialized string, just before the closing brace
        $serialized = substr_replace($serialized, ('i:' . $count . ';' . serialize($item) . ''), (strlen($serialized) - 1), 0);

        // Update the item count at the start of the serialized string
        $oldDecleration = 'a:' . $count . ':';
        $newDecleration = 'a:' . ($count+1) . ':';
        $serialized = substr_replace($serialized, $newDecleration, 0, strlen($oldDecleration));

        return $serialized;
    }

    protected function validateQuery(Query $query)
    {
        parent::validateQuery($query);

        if (!$query->getValues()) {
{'fraction_non_alphanumeric': 0.13674588665447898, 'fraction_numerical': 0.0043875685557586835, 'mean_word_length': 2.6047430830039526, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 21, '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': '3204079', 'n_tokens_mistral': 781, 'n_tokens_neox': 747, 'n_words': 188}