@ 2020-10-18T02:33:30Z <?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 = (int)ceil($start_row / 2) + $start_row +1;
$start_row_id = substr($db, ($start_row) * $row_length , $id_length);
//$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.009 16.63 8.3.5 0.009 0.006 16.59 8.3.4 0.014 0.007 22.03 8.3.3 0.013 0.006 19.07 8.3.2 0.008 0.000 24.18 8.3.1 0.005 0.003 24.66 8.3.0 0.016 0.003 26.16 8.2.18 0.016 0.006 25.92 8.2.17 0.011 0.011 19.21 8.2.16 0.011 0.004 22.96 8.2.15 0.012 0.006 25.66 8.2.14 0.004 0.004 24.66 8.2.13 0.004 0.004 26.16 8.2.12 0.005 0.003 26.35 8.2.11 0.003 0.007 21.07 8.2.10 0.004 0.008 18.03 8.2.9 0.000 0.008 18.03 8.2.8 0.003 0.007 19.95 8.2.7 0.006 0.003 17.75 8.2.6 0.003 0.005 17.75 8.2.5 0.008 0.000 17.88 8.2.4 0.008 0.003 18.34 8.2.3 0.000 0.007 18.33 8.2.2 0.000 0.008 18.16 8.2.1 0.000 0.009 19.43 8.2.0 0.004 0.004 19.48 8.1.28 0.006 0.009 25.92 8.1.27 0.012 0.003 23.99 8.1.26 0.005 0.003 28.09 8.1.25 0.004 0.004 28.09 8.1.24 0.006 0.003 22.29 8.1.23 0.011 0.000 22.38 8.1.22 0.005 0.003 17.78 8.1.21 0.005 0.003 18.79 8.1.20 0.003 0.006 17.60 8.1.19 0.000 0.008 17.35 8.1.18 0.000 0.008 18.10 8.1.17 0.006 0.003 18.78 8.1.16 0.004 0.004 18.93 8.1.15 0.004 0.004 19.04 8.1.14 0.006 0.005 19.02 8.1.13 0.003 0.003 20.28 8.1.12 0.004 0.004 17.62 8.1.11 0.009 0.000 17.60 8.1.10 0.000 0.010 17.48 8.1.9 0.004 0.004 17.45 8.1.8 0.003 0.005 17.52 8.1.7 0.004 0.004 17.54 8.1.6 0.004 0.004 17.70 8.1.5 0.004 0.004 17.68 8.1.4 0.000 0.008 17.55 8.1.3 0.004 0.004 17.76 8.1.2 0.009 0.000 17.82 8.1.1 0.004 0.004 17.70 8.1.0 0.006 0.003 17.54 8.0.30 0.000 0.008 20.23 8.0.29 0.000 0.008 16.88 8.0.28 0.000 0.007 18.50 8.0.27 0.000 0.007 18.15 8.0.26 0.007 0.000 20.22 8.0.25 0.000 0.007 17.08 8.0.24 0.000 0.006 17.02 8.0.23 0.004 0.004 17.00 8.0.22 0.000 0.007 16.93 8.0.21 0.003 0.003 17.08 8.0.20 0.000 0.006 17.06 8.0.19 0.005 0.003 17.04 8.0.18 0.000 0.007 17.05 8.0.17 0.000 0.009 17.05 8.0.16 0.003 0.005 17.06 8.0.15 0.006 0.003 16.95 8.0.14 0.008 0.000 17.07 8.0.13 0.006 0.000 13.45 8.0.12 0.004 0.004 17.02 8.0.11 0.000 0.008 17.00 8.0.10 0.003 0.005 16.82 8.0.9 0.004 0.004 16.99 8.0.8 0.005 0.010 16.98 8.0.7 0.003 0.005 17.08 8.0.6 0.000 0.008 16.99 8.0.5 0.003 0.005 16.86 8.0.3 0.010 0.008 17.02 8.0.2 0.011 0.008 17.32 8.0.1 0.000 0.007 17.27 8.0.0 0.014 0.005 16.78 7.4.33 0.005 0.000 15.55 7.4.32 0.003 0.003 16.72 7.4.30 0.003 0.003 16.75 7.4.29 0.000 0.007 16.64 7.4.28 0.005 0.003 16.75 7.4.27 0.003 0.003 16.62 7.4.26 0.000 0.005 13.42 7.4.25 0.008 0.000 16.56 7.4.24 0.003 0.003 16.73 7.4.23 0.000 0.007 16.63 7.4.22 0.002 0.005 16.61 7.4.21 0.007 0.009 16.70 7.4.20 0.002 0.005 16.54 7.4.13 0.013 0.009 16.66 7.4.12 0.005 0.013 16.74 7.4.11 0.016 0.003 16.43 7.4.10 0.015 0.009 16.65 7.4.9 0.015 0.007 16.64 7.4.8 0.009 0.009 16.63 7.4.7 0.005 0.014 16.29 7.4.6 0.004 0.016 16.71 7.4.5 0.007 0.010 16.54 7.4.4 0.007 0.011 16.45 7.4.3 0.011 0.009 16.73 7.4.2 0.011 0.007 16.48 7.4.1 0.007 0.014 16.58 7.4.0 0.012 0.012 16.58 7.3.33 0.005 0.003 16.55 7.3.32 0.006 0.000 13.52 7.3.31 0.003 0.003 16.61 7.3.30 0.003 0.003 16.34 7.3.29 0.009 0.007 16.47 7.3.26 0.007 0.011 16.60 7.3.23 0.014 0.004 16.43 7.3.22 0.010 0.014 16.47 7.3.21 0.006 0.013 16.79 7.3.20 0.009 0.015 16.44 7.3.19 0.011 0.007 16.66 7.3.18 0.009 0.009 16.70 7.3.17 0.011 0.007 16.72 7.3.16 0.000 0.018 16.62 7.3.15 0.004 0.015 16.46 7.3.14 0.007 0.010 16.69 7.3.13 0.012 0.006 16.73 7.3.12 0.019 0.003 16.46 7.3.11 0.010 0.007 16.50 7.3.10 0.009 0.009 16.38 7.3.9 0.009 0.009 16.61 7.3.8 0.014 0.003 16.38 7.3.7 0.007 0.017 16.45 7.3.6 0.003 0.014 16.63 7.3.5 0.014 0.004 16.53 7.3.4 0.012 0.009 16.49 7.3.3 0.010 0.013 16.59 7.3.2 0.048 0.010 16.51 7.3.1 0.018 0.007 16.44 7.3.0 0.011 0.006 16.46 7.2.34 0.010 0.016 16.53 7.2.33 0.006 0.013 16.92 7.2.32 0.010 0.009 16.80 7.2.31 0.003 0.015 16.95 7.2.30 0.011 0.007 16.74 7.2.29 0.010 0.010 16.93 7.2.28 0.016 0.003 16.87 7.2.27 0.006 0.012 16.57 7.2.26 0.015 0.006 16.64 7.2.25 0.011 0.012 16.87 7.2.24 0.009 0.009 16.70 7.2.23 0.007 0.011 16.79 7.2.22 0.009 0.010 16.94 7.2.21 0.010 0.010 16.66 7.2.20 0.022 0.003 16.82 7.2.19 0.008 0.011 16.66 7.2.18 0.012 0.006 16.60 7.2.17 0.021 0.003 16.74 7.2.16 0.009 0.012 16.65 7.2.15 0.009 0.017 16.71 7.2.14 0.015 0.009 16.76 7.2.13 0.020 0.000 16.86 7.2.12 0.009 0.012 16.76 7.2.11 0.013 0.006 16.87 7.2.10 0.006 0.012 16.70 7.2.9 0.015 0.011 16.72 7.2.8 0.011 0.008 16.71 7.2.7 0.009 0.011 16.98 7.2.6 0.008 0.011 16.78 7.2.5 0.006 0.019 16.65 7.2.4 0.015 0.003 16.82 7.2.3 0.003 0.017 16.86 7.2.2 0.011 0.007 16.77 7.2.1 0.006 0.012 16.91 7.2.0 0.009 0.009 16.68
preferences:dark mode live preview
60.94 ms | 401 KiB | 5 Q