Skip to content

Commit 53a812f

Browse files
committed
Ensure that within a batch of statements the same ID is not generated twice
1 parent dd60d4f commit 53a812f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

app/locker/repository/Statement/EloquentStorer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function store(array $statements, array $attachments, StoreOptions $opts)
5252
* @return [String => \stdClass] Array of statements mapped to their UUIDs.
5353
*/
5454
private function constructValidStatements(array $statements, StoreOptions $opts) {
55+
$generated_ids = [];
5556
$constructed = [];
5657
$this->hashes = [];
5758

@@ -64,7 +65,8 @@ private function constructValidStatements(array $statements, StoreOptions $opts)
6465
}
6566

6667
if (!isset($statement->id)) {
67-
$statement->id = $this->getUUID();
68+
$statement->id = $this->getUUID($generated_ids);
69+
$generated_ids[] = $statement->id;
6870
}
6971

7072
// Validates statement.
@@ -102,18 +104,25 @@ private function activateStatements(array $ids, StoreOptions $opts) {
102104

103105
/**
104106
* Generates a UUID.
107+
* @param $excludes An array of ids to check that the new id is unique against
105108
* @return String
106109
*/
107-
private function getUUID() {
110+
private function getUUID($exclude=[]) {
108111
$remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'LL';
109112
mt_srand(crc32(serialize([microtime(true), $remote_addr, 'ETC'])));
110113

111-
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
114+
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
112115
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
113116
mt_rand(0, 0xffff),
114117
mt_rand(0, 0x0fff) | 0x4000,
115118
mt_rand(0, 0x3fff) | 0x8000,
116119
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
117120
);
121+
122+
if( in_array($uuid, $exclude)){
123+
return $this->getUUID($exclude);
124+
} else {
125+
return $uuid;
126+
}
118127
}
119128
}

0 commit comments

Comments
 (0)