Skip to content

Commit

Permalink
improve hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 17, 2024
1 parent 15dc074 commit 0b2b1ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
10 changes: 8 additions & 2 deletions src/Generator/Protobuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Generator;

use PSX\Schema\Exception\GeneratorException;
use PSX\Schema\Generator\Type\GeneratorInterface;
use PSX\Schema\Type\StructDefinitionType;

Expand Down Expand Up @@ -63,9 +64,10 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte

private function generateNumber(string $name): int
{
$hash = sha1($name);
$result = 0;
for ($i = 0; $i < strlen($name); $i++) {
$result+= ord($name[$i]);
for ($i = 0; $i < 7; $i++) {
$result+= (hexdec($hash[$i]) << ($i * 4));
}

/** @psalm-suppress TypeDoesNotContainType */
Expand All @@ -74,6 +76,10 @@ private function generateNumber(string $name): int
$result += (19_999 - $result) + 1;
}

if ($result > 536_870_911) {
throw new GeneratorException('Generated index is too large');
}

return $result;
}
}
54 changes: 27 additions & 27 deletions tests/Generator/resource/protobuf/protobuf.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
message Location {
optional float lat = 321 [json_name="lat"];
optional float long = 432 [json_name="long"];
optional float lat = 210819871 [json_name="lat"];
optional float long = 259130331 [json_name="long"];
}


message Author {
optional string title = 546 [json_name="title"];
optional string email = 520 [json_name="email"];
optional repeated string categories = 1062 [json_name="categories"];
optional repeated Location locations = 972 [json_name="locations"];
optional Location origin = 648 [json_name="origin"];
optional string title = 186570435 [json_name="title"];
optional string email = 215464074 [json_name="email"];
optional repeated string categories = 142514949 [json_name="categories"];
optional repeated Location locations = 170963641 [json_name="locations"];
optional Location origin = 77547563 [json_name="origin"];
}


message News {
optional map<string, string> config = 630 [json_name="config"];
optional map<string, string> inlineConfig = 1237 [json_name="inlineConfig"];
optional map<string, string> mapTags = 717 [json_name="mapTags"];
optional map<string, Author> mapReceiver = 1139 [json_name="mapReceiver"];
optional repeated string tags = 431 [json_name="tags"];
optional repeated Author receiver = 853 [json_name="receiver"];
optional bool read = 412 [json_name="read"];
optional Author author = 659 [json_name="author"];
optional map<string, string> meta = 423 [json_name="meta"];
optional Timestamp sendDate = 808 [json_name="sendDate"];
optional Timestamp readDate = 794 [json_name="readDate"];
optional float price = 531 [json_name="price"];
optional int64 rating = 645 [json_name="rating"];
optional string content = 763 [json_name="content"];
optional string question = 888 [json_name="question"];
optional string version = 774 [json_name="version"];
optional string coffeeTime = 1015 [json_name="coffeeTime"];
optional string captcha = 2011 [json_name="g-recaptcha-response"];
optional string mediaFields = 1189 [json_name="media.fields"];
optional Any payload = 746 [json_name="payload"];
optional map<string, string> config = 178760701 [json_name="config"];
optional map<string, string> inlineConfig = 215698315 [json_name="inlineConfig"];
optional map<string, string> mapTags = 14364631 [json_name="mapTags"];
optional map<string, Author> mapReceiver = 167282547 [json_name="mapReceiver"];
optional repeated string tags = 174057145 [json_name="tags"];
optional repeated Author receiver = 176067080 [json_name="receiver"];
optional bool read = 199096954 [json_name="read"];
optional Author author = 244171887 [json_name="author"];
optional map<string, string> meta = 155201724 [json_name="meta"];
optional Timestamp sendDate = 133269890 [json_name="sendDate"];
optional Timestamp readDate = 10547211 [json_name="readDate"];
optional float price = 54294786 [json_name="price"];
optional int64 rating = 126276291 [json_name="rating"];
optional string content = 258011200 [json_name="content"];
optional string question = 237941427 [json_name="question"];
optional string version = 57813356 [json_name="version"];
optional string coffeeTime = 145564454 [json_name="coffeeTime"];
optional string captcha = 21415284 [json_name="g-recaptcha-response"];
optional string mediaFields = 83269706 [json_name="media.fields"];
optional Any payload = 145090319 [json_name="payload"];
}

30 changes: 15 additions & 15 deletions tests/Generator/resource/protobuf/protobuf_oop.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
message HumanType {
optional string firstName = 937 [json_name="firstName"];
optional HumanType parent = 650 [json_name="parent"];
optional string firstName = 5347166 [json_name="firstName"];
optional HumanType parent = 227794829 [json_name="parent"];
}


message Student {
optional string firstName = 937 [json_name="firstName"];
optional HumanType parent = 650 [json_name="parent"];
optional string matricleNumber = 1466 [json_name="matricleNumber"];
optional string firstName = 5347166 [json_name="firstName"];
optional HumanType parent = 227794829 [json_name="parent"];
optional string matricleNumber = 110046485 [json_name="matricleNumber"];
}


message StudentMap {
optional int64 totalResults = 1302 [json_name="totalResults"];
optional HumanType parent = 650 [json_name="parent"];
optional repeated Student entries = 762 [json_name="entries"];
optional int64 totalResults = 229871694 [json_name="totalResults"];
optional HumanType parent = 227794829 [json_name="parent"];
optional repeated Student entries = 219233836 [json_name="entries"];
}


message HumanMap {
optional int64 totalResults = 1302 [json_name="totalResults"];
optional HumanType parent = 650 [json_name="parent"];
optional repeated HumanType entries = 762 [json_name="entries"];
optional int64 totalResults = 229871694 [json_name="totalResults"];
optional HumanType parent = 227794829 [json_name="parent"];
optional repeated HumanType entries = 219233836 [json_name="entries"];
}


message Map {
optional int64 totalResults = 1302 [json_name="totalResults"];
optional P parent = 650 [json_name="parent"];
optional repeated T entries = 762 [json_name="entries"];
optional int64 totalResults = 229871694 [json_name="totalResults"];
optional P parent = 227794829 [json_name="parent"];
optional repeated T entries = 219233836 [json_name="entries"];
}


message RootSchema {
optional StudentMap students = 890 [json_name="students"];
optional StudentMap students = 57976611 [json_name="students"];
}

0 comments on commit 0b2b1ae

Please sign in to comment.