PHP code example of simpletools / db-indexedfile

1. Go to this page and download the library: Download simpletools/db-indexedfile library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simpletools / db-indexedfile example snippets


use \Simpletools\Db\IndexedFile;

$indexedFile = new IndexedFile\File();

$indexedFile = new IndexedFile\File('/path/to/my/db.jsonl');

//the default IndexStore
IndexedFile\File::indexStoreClass('Simpletools\Db\IndexedFile\IndexStore\ArrayIndexStore');

$indexedFile->insert('key',["foo"=>"bar"]);

$indexedFile->insertIgnore('key',["foo"=>"bar"]);

$value = $indexedFile->read('key');

foreach($indexedFile->iterate() as $key => $value)
{
    var_dump($key,$value);
}

$indexedFile->upsert('key',function($row){
    if($row)
    {
        $row->counter++;
        return $row;
    }
    else
    {
        return [
            'counter'   => 0
        ];
    }
});

$indexedFile->remove('key');

$indexedFile->truncate();

$indexedFile = new IndexedFile\File('/path/to/my/db.jsonl',true);

$indexedFile->sort('title','ASC', 'string');

$indexedFile->sort('count','DESC', 'int');

$indexedFile->sort('count','DESC', 'int', true);

$indexedFile->sort('title','ASC', 'string',true, __DIR__.'/unsorted.csv', __DIR__.'/sorted.csv');

$indexedFile->sort('price','DESC', 'int');

$indexedFile->insertIgnore($data['id'],[
    'title' => $data['title'],
    'price' => $data['price']
]); 

$indexedFile->runSort();

foreach ($indexedFile->sortIterate() as $groupId => $row)
{
  echo 'Position: '. $row->_sort->position.' Percent: '.$row->_sort->position.' Index key: '. $groupId .' Price: '. $row->price."\n";
}