<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * This file contains unit tests of the LinearHashTable class
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\tests;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\models\Model;
use seekquarry\yioop\library\UnitTest;
use seekquarry\yioop\library\data_structures as LDS;

/**
 * Used to test that the LinearHashTable class properly stores key value pairs,
 * handles insert, deletes, retrievals okay.
 *
 * @author Chris Pollett
 */
 class LinearHashTableTest extends UnitTest
{
    /**
     * Prefix of folders for linear hashing test
     */
    const TEST_DIR = '/test_files/linear_test';
    /**
     * Maintains all linear hash tables created for testing
     * @var array
     */
    public $table_dirs;
    /**
     * Sets up an array to keep track of what linear hash tables we've made
     * so that we can delete them when done a test.
     */
    public function setUp()
    {
        $this->table_dirs = [];
    }
    /**
     * Used to create a single hash table in the folder
     * TEST_DIR . $max_items_per_file which allows at most
     * $max_items_per_file to be stored in a bucket
     *
     * @param int $max_items_per_file number of items allowed to be stored in a
     *  bucket
     * @param array $format column definitions for the table; defaults to a
     *      string primary key and a BLOB value column
     * @return LDS\LinearHashTable freshly-constructed table
     * backed by the
     *      per-test directory
     */
    public function createTable($max_items_per_file, $format =
        ["PRIMARY KEY" => "KEY", "VALUE" => "BLOB"])
    {
        $table_dir = __DIR__ . self::TEST_DIR . $max_items_per_file;
        $this->table_dirs[] = $table_dir;
        return new LDS\LinearHashTable(
            $table_dir, $format, $max_items_per_file);
    }
    /**
     * Deletes all the Linear Hash tables in $this->table_dirs
     */
    public function tearDown()
    {
        $model = new Model();
        foreach ($this->table_dirs as $table_dir) {
            $model->db->unlinkRecursive($table_dir);
        }
        $this->table_dirs = [];
    }
    /**
     * This tests that packed records can be successfully unpacked
     * after being put into the linear hash table and retrieved.
     * This tests the LinearHashTable use-case for storing ETag data
     */
    public function packUnpackFormatTestCase()
    {
        $hash_table = $this->createTable(2,
            ["PRIMARY KEY" => "ID", "FNAME" => "TEXT",
             "LNAME" => "TEXT", "BDATE" => "INT",
             "META" => "SERIAL", "META2" => "BLOB"]);
        $rows = [
            ["ID" => 10, "FNAME" => "Bob", "LNAME"=> "Smith",
                "BDATE" => 5, "META" => "yo", "META2" => "no"],
            ["ID" => 20, "FNAME" => "Sally", "LNAME"=> "Salad",
                "BDATE" => 4, "META" => "old", "META2" => "glow"],
            ["ID" => 30, "FNAME" => "Jojo", "LNAME"=> "yolo",
                "BDATE" => 400, "META" => "dgdfld", "META2" => "low"]
        ];
        foreach ($rows as $row) {
            $hash_table->put($row);
        }
        $i = 0;
        foreach ($rows as $row) {
            $found_row = $hash_table->get($row['ID']);
            foreach ($row as $field => $value) {
                $found_value = $found_row[$field] ?? false;
                $this->assertEqual($value, $found_value,
                    "row $i field $field test");
            }
            $i++;
        }
        $hash_table = $this->createTable(3, ["PRIMARY KEY" => "URL",
            "ETAG" => "TEXT", "EXPIRES" => "INT"]);
        $rows = [
            ["URL" => "https://somewhere.com/",
                "ETAG" => "33a64df551425fcc55e4d42a148795d9f25f89d4",
                "EXPIRES"=> time()],
            ["URL" => "https://nowhere.com/", "ETAG" => "686897696a7c876b7e",
                "EXPIRES"=> time()+50],
        ];
        foreach ($rows as $row) {
            $hash_table->put($row);
        }
        foreach ($rows as $row) {
            $found_row = $hash_table->get($row['URL']);
            foreach ($row as $field => $value) {
                $found_value = $found_row[$field] ?? false;
                $this->assertEqual($value, $found_value,
                    "row $i field $field test");
            }
            $i++;
        }
        $row = ["URL" => "https://nowhere.com/",
            "ETAG" => "e686897696a7c876b7", "EXPIRES"=> time()+60];
        $hash_table->put($row);
        $found_row = $hash_table->get($row['URL']);
        foreach ($row as $field => $value) {
            $found_value = $found_row[$field] ?? false;
            $this->assertEqual($value, $found_value,
                "insert_same_key row field $field test");
        }
    }
    /**
     * Tests whether key value pairs inserted into the linear hash table
     * can subsequently be retrieved. This tests the hashed key case
     */
    public function insertHashKeyLookupTestCase()
    {
        $max_file_size = 2 << 10;
        for ($i = 1; $i < $max_file_size; $i *= 2) {
            $hash_table = $this->createTable($i);
            $insert_rows = [];
            for ($j = 0; $j < 258; $j++) {
                $insert_rows[] = ["KEY" => pack("J2", $j, 0),
                    "VALUE" => "test$i-$j"];
            }
            $hash_table->put($insert_rows, true);
            for ($j = 0; $j < 258; $j++) {
                $result = $hash_table->get(pack("J2", $j, 0), [], true);
                $this->assertEqual("test$i-$j", $result["VALUE"],
                    "epoch $i put $j get $j test");
            }
        }
    }
    /**
     * Tests whether key value pairs inserted into the linear hash table
     * can subsequently be retrieved. This tests the non-hashed key case
     */
    public function insertKeyLookupTestCase()
    {
        $hash_table = $this->createTable(4);
        $insert_rows = [];
        for ($j = 0; $j < 258; $j++) {
            $insert_rows[] = ["KEY" => $j, "VALUE" =>"test-$j"];
        }
        $hash_table->put($insert_rows);
        for ($j = 0; $j < 258; $j++) {
            $result = $hash_table->get($j);
            $this->assertEqual("test-$j", $result["VALUE"],
                "put $j get $j test");
        }
    }
    /**
     * Tests whether key value pairs can be deleted from the linear hash table
     * without destroying non-deleted data. This test is for pre-hashed keys
     */
    public function deleteHashKeyTestCase()
    {
        $max_file_size = 2 << 10;
        for ($i = 1; $i < $max_file_size; $i *= 2) {
            $hash_table = $this->createTable($i);
            $insert_rows = [];
            for ($j = 0; $j < 260; $j++) {
                $insert_rows[] = ["KEY" => pack("J2", $j, 0),
                    "VALUE" => "test$i-$j"];
            }
            $hash_table->put($insert_rows, true);
            for ($j = 65; $j < 195; $j++) {
                $hash_table->delete(pack("J2", $j, 0), true);
            }
            for ($j = 0; $j < 260; $j++) {
                $result = $hash_table->get(pack("J2", $j, 0), [], true);
                $value = $result["VALUE"] ?? false;
                if ($j < 65 || $j >= 195) {
                    $this->assertEqual("test$i-$j", $value,
                        "epoch $i put $j get $j test");
                } else {
                    $this->assertEqual(false, $value,
                        "epoch $i delete $j get false test");
                }
            }
        }
    }
    /**
     * Tests whether key value pairs can be deleted from the linear hash table
     * without destroying non-deleted data. This test is for non-pre-hashed keys
     */
    public function deleteKeyTestCase()
    {
        $hash_file = $this->createTable(4);
        $insert_rows = [];
        for ($j = 0; $j < 260; $j++) {
            $insert_rows[] = ["KEY" => $j, "VALUE" => "test-$j"];
        }
        $hash_file->put($insert_rows);
        $deletes = [];
        for ($j = 65; $j < 195; $j++) {
            $deletes[] = $j;
        }
        $hash_file->delete($deletes);
        for ($j = 0; $j < 260; $j++) {
            $result = $hash_file->get($j);
            $value = $result["VALUE"] ?? false;
            if ($j < 65 || $j >= 195) {
                $this->assertEqual("test-$j", $value, "put $j get $j test");
            } else {
                $this->assertEqual(false, $value, "delete $j get false test");
            }
        }
    }
}
