Random access to php file and save object to file

I have a csv file with sorting entries in the first field . I managed to create a function that performs a binary search through this file, using fseek for random access through the file.

However, this is still a rather slow process, since when I search for some position in the file, I really need to look left while looking at \ n characted, so I can make sure that I read the whole line (after the whole line being read, I can check the value of the first field mentioned above).

Here is a function that returns a string containing the character at position x:


function fgetLineContaining( $fh, $x ) {
        if( $x  125145411) // 12514511 is the last pos in my file
            return "";
        // now go as much left as possible, until newline is found
        // or beginning of the file
        while( $x > 0 && $c != "\n" && $c != "\r") {
            fseek($fh, $x);
            $x--; // go left in the file
            $c =  fgetc( $fh );
        }
        $x+=2; // skip newline char
        fseek( $fh, $x );
        return fgets( $fh, 1024 ); // return the line from the beginning until \n
    }

While this works as expected, I have to be sad that my csv file has ~ 1.5Mil lines, and these left-handed distortions slow down to a large extent.

, x ?

, , , . php ?

+3
1

, SQLite MySQL ( , ). "" SQL.

, SQL. ? ? , ? SQL ?

, , - (~ 100 ?) RAM. , , CVS, SQLite, , .

, PHP ( , ). ($big_array[$offset]).

, PHP . , PHP C ++.

+1

Source: https://habr.com/ru/post/1772190/


All Articles