@ 2020-10-18T02:29:31Z <?php
$sql = "SELECT * FROM 'main' WHERE 'destination' = 'Helsinki' AND 'price' > 120"; //example of sql query
//the following assumes the above SQL query has been parsed into fields and conditions
//main header file. Stored as JSON.
$main_db_header_contents = '[{"rowlength":60},{"id":{"length":3,"datatype":"integer"}},{"name_sname":{"length":9,"datatype":"String"}},{"identity":{"length":9,"datatype":"integer"}},{"address":{"length":8,"datatype":"String"}},{"region":{"length":7,"datatype":"String"}},{"hours":{"length":5,"datatype":"Time"}},{"destination":{"length":8,"datatype":"String"}},{"price":{"length":3,"datatype":"integer"}}]'; //json string. fairly immutable so it can be cached on server
$main_db_header = json_decode($main_db_header_contents,true);
//$row_length = $main_db_header[0]['rowlength']; //id, length, datatype
//var_dump($row_length);
//main database file. Columns are fixed length and padded. Stored as strings only. ID field must be sorted alphabetically.
//When fields in this file are updated/added to, their respective index file must be too
$main_db_file_contents = "001|Al-Farabi|123456789|Baghdad |Iraq |21:02|Ottawa |120;002|Smith |123456788|Ottawa |Canada |22:03|Helsinki|131;003|Locke |123456787|Helsinki|Finland|23:01|Istanbul|122;004|Jones |123456786|Istanbul|Turkey |21:02|Helsinki|123;005|Steinman |123455785|Havana |Cuba |22:03|Havana |104;006|Booker |123456784|Moscow |Russia |23:01|Helsinki|115;"; //fixed-length field values
//Destination index file with record id and destination value. Sorted alphabetically.
//E.g., The record with the id of 004 has the destination value of 'Moscow '.
$destination_index_file_contents = "005|Havana ;002|Helsinki;004|Helsinki;006|Helsinki;003|Istanbul;001|Ottawa ";
//Destination index file. Sorted alphabetically despite being numberic.
//E.g., The record with the id of 005 has the price value of 104.
$price_index_file_contents = "005|104;006|115;001|120;003|122;004|123;002|131";
function apply_condition($index_file_contents, $ptype, $value) {
$found_matches = [];
$index_file_contents_split = explode(";", $index_file_contents);
foreach($index_file_contents_split as $key => $props){
$pairs = explode("|",$props);
$id = $pairs[0];
$val = trim($pairs[1]); //removes trailing spaces for matching non-fiexed-length input value
if ($ptype === "=") {
if ($val === $value) {
array_push($found_matches,$id); //add to new array of matches
}
} elseif ($ptype === ">") {
if ($val > $value) {
array_push($found_matches,$id);//add to new array of matches
}
}
}
return $found_matches;
}
$parsed_destinations = apply_condition($destination_index_file_contents, "=", "Helsinki");
$parsed_prices = apply_condition($price_index_file_contents, ">", "120");
$parsed_ids = array_intersect($parsed_destinations, $parsed_prices);
sort($parsed_ids); //sort ascending
//now go through $main_db_file_contents and get selected fields in records matching the ids in $parsed_ids.
$id_length = $main_db_header[1]['id']['length']; //id, length, datatype
$destination_offset = get_field_offset("destination", $main_db_header);
$price_offset = get_field_offset("price", $main_db_header);
$destination_length = $main_db_header[7]['destination']['length']; //8
$price_length = $main_db_header[8]['price']['length']; //3
$row_length = $main_db_header[0]['rowlength']; //147
$main_file_length = strlen($main_db_file_contents);
$total_main_file_rows = ($main_file_length/$row_length);
//$starting_row = get_starting_row($main_db_file_contents, $parsed_ids[0], $id_length, $row_length, $total_main_file_rows, 0);
$destinations = [];
$prices = [];
$starting_row = 0;
//for ($i = 0; $i < count($parsed_ids); $i++){
for ($i = 0; $i < count($parsed_ids); $i++){
$starting_row = get_starting_row($main_db_file_contents, $parsed_ids[$i], $id_length, $row_length, $total_main_file_rows, $starting_row);
$destination = get_field_from_row($main_db_file_contents, $starting_row, $row_length, $destination_offset, $destination_length);
//var_dump($destination);
array_push($destinations, $destination);
$price = get_field_from_row($main_db_file_contents, $starting_row, $row_length, $price_offset, $price_length);
//var_dump($price);
array_push($prices, $price);
//$starting_row++;
//$starting_row = get_starting_row($main_db_file_contents, $parsed_ids[0], $id_length, $row_length, $total_main_file_rows, $starting_row);
//$starting_row = get_starting_row($main_db_file_contents, $parsed_ids[$i], $id_length, $row_length, $total_main_file_rows, $starting_row);
}
function get_field_from_row($db, $current_row, $row_length, $field_offset, $field_length) {
//$value = substr($db, ($current_row * $row_length) + $field_offset, $field_length);
$value = substr($db, ($current_row * $row_length) + $field_offset, $field_length);
return $value;
}
function get_next_row($db, $id, $id_length, $row_length,$total_main_file_rows, $current_row){
//returns index of row that that has the given id
}
function get_starting_row($db, $first_id, $id_length, $row_length, $total_main_file_rows, $offset){
$start_row = (int)floor($total_main_file_rows / 2) + $offset; //halfway point of file
$start_row_id = substr($db, ($start_row) * $row_length , $id_length); //gets current row id
while ($start_row > -1 && $start_row <= $total_main_file_rows) {
//var_dump($start_row);
print ("\nseeing if ".$first_id." is less than ".$start_row_id);
if ( $first_id < $start_row_id){
print("\nIT LOWER!");
//print ("seeing if ".$first_id." is less than ".$row_id);
//go back a bit less
$start_row = (int)floor($start_row / 2);
//print("\n" . $start_row);
$start_row_id = substr($db, ($start_row) * $row_length , $id_length);
//print("\nstarting row: " . $start_row . "\n");
//print("\n" . $first_id . ", " . $row_id);
} elseif($start_row_id < $first_id) {
//go forward a bit less
//$start_row++; //new
//$start_row = $start_row + (int)round($start_row / 2);
$start_row = (int)ceil($start_row / 2) + $start_row +2;
//$start_row++;
print("\nIT HIGHER!");
//print("\nstarting row: " . $start_row . "\n");
//print("\n" . $start_row);
//print("\n" . $first_id . ", " . $row_id);
} else {
print("\nIT IS THE SAME!");
//print("\nrow found: " . $start_row . "\n");
return $start_row;
}
}
//return $row_id;
}
function get_field_offset($field, $header) {
$total_offset = 0;
foreach ($header as $key => $value){
foreach ($value as $k => $val) {
if ($k != "rowlength"){
if ($field === (string)$k){
//var_dump($k);
return $total_offset;
}
$total_offset += $val['length'] +1; //+1 is needed for single-character delimeter (|)
}
}
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.6 0.012 0.006 16.75 8.3.5 0.007 0.014 16.76 8.3.4 0.010 0.010 21.91 8.3.3 0.009 0.009 18.77 8.3.2 0.006 0.012 24.18 8.3.1 0.005 0.005 24.66 8.3.0 0.005 0.003 26.16 8.2.18 0.009 0.013 25.92 8.2.17 0.012 0.009 19.01 8.2.16 0.007 0.013 22.96 8.2.15 0.015 0.003 25.66 8.2.14 0.009 0.000 24.66 8.2.13 0.005 0.002 26.16 8.2.12 0.005 0.003 26.35 8.2.11 0.003 0.006 20.60 8.2.10 0.006 0.006 17.72 8.2.9 0.000 0.008 17.75 8.2.8 0.000 0.009 17.97 8.2.7 0.000 0.008 17.75 8.2.6 0.004 0.004 17.63 8.2.5 0.006 0.003 18.05 8.2.4 0.003 0.006 19.94 8.2.3 0.005 0.003 18.32 8.2.2 0.000 0.008 18.27 8.2.1 0.000 0.008 19.45 8.2.0 0.004 0.004 19.44 8.1.28 0.012 0.006 25.92 8.1.27 0.000 0.009 23.99 8.1.26 0.012 0.003 28.09 8.1.25 0.000 0.008 28.09 8.1.24 0.006 0.003 23.96 8.1.23 0.007 0.004 22.33 8.1.22 0.004 0.004 17.79 8.1.21 0.005 0.003 18.77 8.1.20 0.010 0.000 17.60 8.1.19 0.009 0.000 17.48 8.1.18 0.007 0.007 18.10 8.1.17 0.004 0.004 18.75 8.1.16 0.004 0.004 18.99 8.1.15 0.000 0.008 18.99 8.1.14 0.010 0.000 19.16 8.1.13 0.003 0.003 20.41 8.1.12 0.004 0.004 17.46 8.1.11 0.006 0.003 17.59 8.1.10 0.000 0.011 17.56 8.1.9 0.004 0.004 17.63 8.1.8 0.000 0.008 17.49 8.1.7 0.000 0.008 17.63 8.1.6 0.003 0.005 17.62 8.1.5 0.004 0.004 17.58 8.1.4 0.000 0.008 17.63 8.1.3 0.006 0.003 17.74 8.1.2 0.006 0.003 17.80 8.1.1 0.005 0.005 17.69 8.1.0 0.003 0.005 17.53 8.0.30 0.004 0.004 19.85 8.0.29 0.000 0.007 16.88 8.0.28 0.000 0.007 18.60 8.0.27 0.004 0.004 18.07 8.0.26 0.003 0.003 18.49 8.0.25 0.007 0.000 17.12 8.0.24 0.000 0.007 17.12 8.0.23 0.004 0.004 17.16 8.0.22 0.003 0.003 17.01 8.0.21 0.000 0.008 17.04 8.0.20 0.003 0.003 17.06 8.0.19 0.000 0.008 17.12 8.0.18 0.000 0.007 17.09 8.0.17 0.000 0.009 17.10 8.0.16 0.008 0.000 17.15 8.0.15 0.005 0.002 16.83 8.0.14 0.004 0.004 16.82 8.0.13 0.003 0.003 13.46 8.0.12 0.005 0.003 17.05 8.0.11 0.005 0.002 16.99 8.0.10 0.004 0.004 16.89 8.0.9 0.000 0.008 17.16 8.0.8 0.008 0.007 17.05 8.0.7 0.000 0.008 17.11 8.0.6 0.000 0.008 16.91 8.0.5 0.000 0.007 17.07 8.0.3 0.011 0.006 17.48 8.0.2 0.010 0.012 17.20 8.0.1 0.004 0.004 17.20 8.0.0 0.017 0.003 16.86 7.4.33 0.005 0.000 15.55 7.4.32 0.000 0.006 16.79 7.4.30 0.000 0.006 16.66 7.4.29 0.000 0.008 16.65 7.4.28 0.008 0.000 16.66 7.4.27 0.004 0.002 16.58 7.4.26 0.003 0.003 13.49 7.4.25 0.000 0.007 16.71 7.4.24 0.000 0.007 16.59 7.4.23 0.007 0.000 16.72 7.4.22 0.003 0.003 16.75 7.4.21 0.006 0.010 16.69 7.4.20 0.004 0.004 16.44 7.4.13 0.008 0.012 16.63 7.4.12 0.009 0.010 16.80 7.4.11 0.007 0.011 16.46 7.4.10 0.010 0.006 16.46 7.4.9 0.009 0.009 16.71 7.4.8 0.008 0.008 16.61 7.4.7 0.012 0.005 16.54 7.4.6 0.012 0.006 16.60 7.4.5 0.004 0.014 16.45 7.4.4 0.010 0.010 16.63 7.4.3 0.010 0.010 16.53 7.4.2 0.010 0.007 16.33 7.4.1 0.006 0.012 16.35 7.4.0 0.012 0.006 16.54 7.3.33 0.007 0.000 16.34 7.3.32 0.003 0.003 13.50 7.3.31 0.005 0.002 16.51 7.3.30 0.007 0.000 16.55 7.3.29 0.006 0.010 16.49 7.3.26 0.011 0.008 16.73 7.3.23 0.010 0.006 16.55 7.3.22 0.003 0.013 16.61 7.3.21 0.013 0.003 16.50 7.3.20 0.015 0.003 16.48 7.3.19 0.010 0.010 16.40 7.3.18 0.013 0.003 16.64 7.3.17 0.007 0.011 16.77 7.3.16 0.004 0.014 16.49 7.3.15 0.013 0.003 16.64 7.3.14 0.010 0.007 16.60 7.3.13 0.013 0.003 16.44 7.3.12 0.014 0.003 16.69 7.3.11 0.014 0.004 16.34 7.3.10 0.011 0.005 16.60 7.3.9 0.013 0.006 16.65 7.3.8 0.010 0.007 16.55 7.3.7 0.011 0.005 16.41 7.3.6 0.012 0.003 16.55 7.3.5 0.009 0.009 16.40 7.3.4 0.010 0.007 16.43 7.3.3 0.013 0.003 16.29 7.3.2 0.029 0.013 16.65 7.3.1 0.004 0.013 16.58 7.3.0 0.012 0.009 16.58 7.2.34 0.004 0.013 16.56 7.2.33 0.009 0.009 16.77 7.2.32 0.003 0.013 16.68 7.2.31 0.015 0.003 16.89 7.2.30 0.012 0.006 16.66 7.2.29 0.013 0.007 16.79 7.2.28 0.010 0.007 16.80 7.2.27 0.010 0.007 16.85 7.2.26 0.004 0.013 16.87 7.2.25 0.007 0.010 16.77 7.2.24 0.009 0.009 16.67 7.2.23 0.007 0.011 16.98 7.2.22 0.007 0.011 16.87 7.2.21 0.007 0.010 16.96 7.2.20 0.013 0.004 16.84 7.2.19 0.003 0.013 16.59 7.2.18 0.006 0.012 16.71 7.2.17 0.007 0.011 16.73 7.2.16 0.008 0.008 16.78 7.2.15 0.009 0.009 16.91 7.2.14 0.009 0.009 16.80 7.2.13 0.008 0.011 16.74 7.2.12 0.010 0.010 16.85 7.2.11 0.007 0.014 16.69 7.2.10 0.009 0.009 16.73 7.2.9 0.009 0.009 16.80 7.2.8 0.006 0.012 16.81 7.2.7 0.005 0.014 16.88 7.2.6 0.011 0.008 16.93 7.2.5 0.013 0.006 16.81 7.2.4 0.012 0.006 16.95 7.2.3 0.011 0.007 16.86 7.2.2 0.012 0.006 16.69 7.2.1 0.032 0.010 16.61 7.2.0 0.011 0.007 16.76
preferences:dark mode live preview
38.46 ms | 401 KiB | 5 Q