Skip to content

Commit

Permalink
updated fn signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
abmmhasan committed May 5, 2024
1 parent 92a4eca commit b3d9256
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
13 changes: 6 additions & 7 deletions src/GetSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@ trait GetSequence
/**
* Generates a sequence number based on the current time.
*
* @param DateTimeInterface $dateTime The current time.
* @param int $dateTime The current time.
* @param string $machineId The machine ID.
* @return int The generated sequence number.
* @throws FileLockException
*/
private static function sequence(DateTimeInterface $dateTime, string $machineId, string $type): int
private static function sequence(int $dateTime, string $machineId, string $type): int
{
self::$fileLocation = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "uid-$type-$machineId.seq";
self::$fileLocation ??= sys_get_temp_dir() . DIRECTORY_SEPARATOR . "uid-$type-$machineId.seq";
if (!file_exists(self::$fileLocation)) {
touch(self::$fileLocation);
}
$handle = fopen(self::$fileLocation, "r+");
if (!flock($handle, LOCK_EX)) {
throw new FileLockException('Could not acquire lock on ' . self::$fileLocation);
}
$now = $dateTime->format('Uv');
$line = fgetcsv($handle);
$sequence = 0;
if ($line) {
if ($line && ($line[0] = (int)$line[0]) <= $dateTime) {
$sequence = match ($line[0]) {
$now => $line[1],
$dateTime => $line[1],
default => $sequence
};
}
ftruncate($handle, 0);
rewind($handle);
fputcsv($handle, [$now, ++$sequence]);
fputcsv($handle, [$dateTime, ++$sequence]);
flock($handle, LOCK_UN);
fclose($handle);
return $sequence;
Expand Down
5 changes: 2 additions & 3 deletions src/Snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public static function generate(int $datacenter = 0, int $workerId = 0): string
throw new SnowflakeException("Invalid worker ID, must be between 0 ~ $maxWorkId.");
}

$now = new DateTimeImmutable('now');
$currentTime = (int)$now->format('Uv');
$currentTime = (int)(new DateTimeImmutable('now'))->format('Uv');
while (($sequence = self::sequence(
$now,
$currentTime,
$datacenter . $workerId,
'snowflake'
)) > (-1 ^ (-1 << self::$maxSequenceLength))) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sonyflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function generate(int $machineId = 0): string
if ($machineId < 0 || $machineId > $maxMachineID) {
throw new SonyflakeException("Invalid machine ID, must be between 0 ~ $maxMachineID.");
}
$now = new DateTimeImmutable('now');
$now = (int)(new DateTimeImmutable('now'))->format('Uv');
$elapsedTime = self::elapsedTime();
while (($sequence = self::sequence($now, $machineId, 'sonyflake')) > (-1 ^ (-1 << self::$maxSequenceLength))) {
$nextMillisecond = self::elapsedTime();
Expand Down

0 comments on commit b3d9256

Please sign in to comment.