@ 2020-10-18T02:31:40Z <?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 +1;
//$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.7 0.007 0.007 18.30 8.3.6 0.015 0.003 16.88 8.3.5 0.011 0.003 16.42 8.3.4 0.010 0.003 21.98 8.3.3 0.008 0.008 18.83 8.3.2 0.009 0.000 24.18 8.3.1 0.009 0.000 24.66 8.3.0 0.011 0.007 26.16 8.2.18 0.014 0.007 25.92 8.2.17 0.009 0.006 18.96 8.2.16 0.007 0.010 22.96 8.2.15 0.006 0.012 25.66 8.2.14 0.006 0.003 24.66 8.2.13 0.004 0.004 26.16 8.2.12 0.000 0.008 26.35 8.2.11 0.003 0.006 21.12 8.2.10 0.007 0.004 18.16 8.2.9 0.005 0.003 18.47 8.2.8 0.005 0.003 18.46 8.2.7 0.005 0.003 17.63 8.2.6 0.009 0.000 17.63 8.2.5 0.009 0.000 18.05 8.2.4 0.000 0.008 18.34 8.2.3 0.000 0.007 18.19 8.2.2 0.000 0.008 18.14 8.2.1 0.004 0.004 19.36 8.2.0 0.007 0.000 19.39 8.1.28 0.011 0.007 25.92 8.1.27 0.006 0.009 23.99 8.1.26 0.003 0.006 28.09 8.1.25 0.004 0.004 28.09 8.1.24 0.003 0.006 22.58 8.1.23 0.004 0.008 21.22 8.1.22 0.008 0.000 18.03 8.1.21 0.006 0.003 18.77 8.1.20 0.005 0.003 17.48 8.1.19 0.000 0.008 17.48 8.1.18 0.009 0.000 18.10 8.1.17 0.003 0.006 18.56 8.1.16 0.003 0.006 18.95 8.1.15 0.004 0.004 18.91 8.1.14 0.003 0.007 18.94 8.1.13 0.000 0.007 20.36 8.1.12 0.007 0.000 17.64 8.1.11 0.004 0.004 17.48 8.1.10 0.004 0.004 17.58 8.1.9 0.000 0.007 17.45 8.1.8 0.005 0.003 17.45 8.1.7 0.000 0.008 17.62 8.1.6 0.004 0.004 17.74 8.1.5 0.004 0.004 17.66 8.1.4 0.000 0.008 17.62 8.1.3 0.000 0.008 17.80 8.1.2 0.006 0.003 17.68 8.1.1 0.005 0.003 17.62 8.1.0 0.004 0.004 17.63 8.0.30 0.000 0.007 20.16 8.0.29 0.005 0.005 16.88 8.0.28 0.004 0.004 18.58 8.0.27 0.008 0.000 18.09 8.0.26 0.000 0.007 20.25 8.0.25 0.000 0.007 17.02 8.0.24 0.007 0.000 17.03 8.0.23 0.004 0.004 16.98 8.0.22 0.000 0.007 17.01 8.0.21 0.004 0.004 17.05 8.0.20 0.006 0.000 17.00 8.0.19 0.003 0.006 17.07 8.0.18 0.004 0.004 16.93 8.0.17 0.000 0.009 16.90 8.0.16 0.008 0.000 17.10 8.0.15 0.004 0.004 16.85 8.0.14 0.000 0.007 16.91 8.0.13 0.000 0.006 13.49 8.0.12 0.004 0.004 16.89 8.0.11 0.008 0.000 17.07 8.0.10 0.004 0.004 17.02 8.0.9 0.004 0.004 16.95 8.0.8 0.009 0.007 17.01 8.0.7 0.003 0.006 16.88 8.0.6 0.005 0.002 17.11 8.0.5 0.004 0.004 16.90 8.0.3 0.006 0.014 17.24 8.0.2 0.007 0.012 17.28 8.0.1 0.004 0.004 17.05 8.0.0 0.007 0.011 16.79 7.4.33 0.000 0.005 15.55 7.4.32 0.003 0.003 16.64 7.4.30 0.006 0.000 16.59 7.4.29 0.000 0.008 16.71 7.4.28 0.003 0.005 16.71 7.4.27 0.003 0.003 16.71 7.4.26 0.005 0.000 13.48 7.4.25 0.004 0.004 16.60 7.4.24 0.004 0.004 16.64 7.4.23 0.000 0.007 16.86 7.4.22 0.002 0.005 16.70 7.4.21 0.007 0.008 16.71 7.4.20 0.003 0.006 16.85 7.4.13 0.014 0.009 16.58 7.4.12 0.003 0.015 16.73 7.4.11 0.012 0.009 16.67 7.4.10 0.012 0.008 16.35 7.4.9 0.010 0.007 16.66 7.4.8 0.009 0.015 16.75 7.4.7 0.012 0.006 16.46 7.4.6 0.012 0.004 16.41 7.4.5 0.014 0.004 16.64 7.4.4 0.013 0.010 16.72 7.4.3 0.006 0.012 16.73 7.4.2 0.006 0.011 16.86 7.4.1 0.010 0.013 16.51 7.4.0 0.009 0.009 16.64 7.3.33 0.000 0.008 16.52 7.3.32 0.005 0.002 13.51 7.3.31 0.005 0.003 16.51 7.3.30 0.000 0.007 16.50 7.3.29 0.007 0.009 16.49 7.3.26 0.011 0.008 16.74 7.3.23 0.010 0.010 16.55 7.3.22 0.007 0.010 16.77 7.3.21 0.004 0.014 16.36 7.3.20 0.003 0.015 16.49 7.3.19 0.006 0.011 16.49 7.3.18 0.008 0.008 16.49 7.3.17 0.017 0.006 16.62 7.3.16 0.008 0.014 16.51 7.3.15 0.005 0.014 16.50 7.3.14 0.004 0.018 16.48 7.3.13 0.016 0.006 16.74 7.3.12 0.009 0.009 16.71 7.3.11 0.010 0.011 16.50 7.3.10 0.008 0.009 16.43 7.3.9 0.007 0.011 16.68 7.3.8 0.013 0.010 16.46 7.3.7 0.017 0.006 16.51 7.3.6 0.016 0.006 16.59 7.3.5 0.007 0.015 16.68 7.3.4 0.013 0.009 16.41 7.3.3 0.006 0.012 16.34 7.3.2 0.012 0.006 16.53 7.3.1 0.011 0.007 16.54 7.3.0 0.011 0.008 16.62 7.2.34 0.014 0.004 16.73 7.2.33 0.011 0.007 16.91 7.2.32 0.004 0.013 16.86 7.2.31 0.015 0.004 16.76 7.2.30 0.004 0.018 16.84 7.2.29 0.020 0.003 16.73 7.2.28 0.007 0.016 16.78 7.2.27 0.013 0.010 16.77 7.2.26 0.010 0.013 16.75 7.2.25 0.010 0.007 16.92 7.2.24 0.012 0.009 16.61 7.2.23 0.019 0.006 16.87 7.2.22 0.006 0.012 16.65 7.2.21 0.010 0.013 16.82 7.2.20 0.010 0.014 16.66 7.2.19 0.012 0.012 16.60 7.2.18 0.014 0.003 16.73 7.2.17 0.016 0.008 16.71 7.2.16 0.006 0.012 16.72 7.2.15 0.016 0.003 16.72 7.2.14 0.009 0.009 16.66 7.2.13 0.011 0.007 16.75 7.2.12 0.000 0.018 16.93 7.2.11 0.019 0.006 16.71 7.2.10 0.010 0.013 16.75 7.2.9 0.009 0.009 16.95 7.2.8 0.014 0.005 16.84 7.2.7 0.017 0.009 16.88 7.2.6 0.009 0.009 16.94 7.2.5 0.012 0.007 16.64 7.2.4 0.011 0.014 16.84 7.2.3 0.022 0.003 16.89 7.2.2 0.011 0.014 16.64 7.2.1 0.020 0.006 16.89 7.2.0 0.011 0.008 16.94
preferences:dark mode live preview
66.39 ms | 401 KiB | 5 Q