Skip to content

Commit 3ef3dc9

Browse files
authored
Merge pull request #143 from opcodesio/bug/incorrect-indices-when-searching
fix how entry indices are generated, so that it also works properly when searching
2 parents 6f5031f + a703b9e commit 3ef3dc9

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/LogIndex.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class LogIndex
1818

1919
protected int $lastScannedFilePosition;
2020

21+
protected int $lastScannedIndex;
22+
2123
public function __construct(
2224
protected LogFile $file,
2325
protected ?string $query = null
@@ -67,9 +69,9 @@ public function clearCache(): void
6769
$this->loadMetadata();
6870
}
6971

70-
public function addToIndex(int $filePosition, int|Carbon $timestamp, string $severity): int
72+
public function addToIndex(int $filePosition, int|Carbon $timestamp, string $severity, int $index = null): int
7173
{
72-
$logIndex = $this->nextLogIndexToCreate ?? 0;
74+
$logIndex = $index ?? $this->nextLogIndexToCreate ?? 0;
7375

7476
if ($timestamp instanceof Carbon) {
7577
$timestamp = $timestamp->timestamp;
@@ -291,6 +293,20 @@ public function getLastScannedFilePosition(): int
291293
return $this->lastScannedFilePosition;
292294
}
293295

296+
public function setLastScannedIndex(int $index): void
297+
{
298+
$this->lastScannedIndex = $index;
299+
}
300+
301+
public function getLastScannedIndex(): int
302+
{
303+
if (! isset($this->lastScannedIndex)) {
304+
$this->loadMetadata();
305+
}
306+
307+
return $this->lastScannedIndex;
308+
}
309+
294310
public function incomplete(): bool
295311
{
296312
return $this->file->size() !== $this->getLastScannedFilePosition();
@@ -436,6 +452,7 @@ public function getMetadata(): array
436452
'query' => $this->getQuery(),
437453
'identifier' => $this->identifier(),
438454
'last_scanned_file_position' => $this->lastScannedFilePosition,
455+
'last_scanned_index' => $this->lastScannedIndex,
439456
'next_log_index_to_create' => $this->nextLogIndexToCreate,
440457
'max_chunk_size' => $this->maxChunkSize,
441458
'current_chunk_index' => $this->getCurrentChunk()->index,
@@ -454,6 +471,7 @@ protected function loadMetadata(): void
454471
$data = Cache::get($this->metaCacheKey(), []);
455472

456473
$this->lastScannedFilePosition = $data['last_scanned_file_position'] ?? 0;
474+
$this->lastScannedIndex = $data['last_scanned_index'] ?? 0;
457475
$this->nextLogIndexToCreate = $data['next_log_index_to_create'] ?? 0;
458476
$this->maxChunkSize = $data['max_chunk_size'] ?? self::DEFAULT_CHUNK_SIZE;
459477
$this->chunkDefinitions = $data['chunk_definitions'] ?? [];

src/LogReader.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self
308308
$currentLog = '';
309309
$currentLogLevel = '';
310310
$currentTimestamp = null;
311+
$currentIndex = $this->index()->getLastScannedIndex();
311312
fseek($this->fileHandle, $this->index()->getLastScannedFilePosition());
312313
$currentLogPosition = ftell($this->fileHandle);
313314
$lastPositionToScan = isset($maxBytesToScan) ? ($currentLogPosition + $maxBytesToScan) : null;
@@ -327,10 +328,11 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self
327328
if (preg_match($logMatchPattern, $line, $matches) === 1) {
328329
if ($currentLog !== '') {
329330
if (is_null($this->query) || preg_match($this->query, $currentLog)) {
330-
$logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel);
331+
$logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel, $currentIndex);
331332
}
332333

333334
$currentLog = '';
335+
$currentIndex++;
334336
}
335337

336338
$currentTimestamp = strtotime($matches[1] ?? '');
@@ -361,12 +363,11 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self
361363

362364
if ($currentLog !== '' && preg_match($logMatchPattern, $currentLog) === 1) {
363365
if ((is_null($this->query) || preg_match($this->query, $currentLog))) {
364-
$logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel);
366+
$logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel, $currentIndex);
365367
}
366-
367-
$currentLog = '';
368368
}
369369

370+
$logIndex->setLastScannedIndex($currentIndex);
370371
$logIndex->setLastScannedFilePosition(ftell($this->fileHandle));
371372
$logIndex->save();
372373

tests/Unit/LogIndex/LogIndexTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@
4949
]);
5050
});
5151

52+
it('can optionally provide a specific index', function () {
53+
$logIndex = createLogIndex();
54+
55+
$indexGenerated = $logIndex->addToIndex(
56+
100,
57+
now()->subMinute(),
58+
'info',
59+
$indexProvided = 10
60+
);
61+
62+
expect($indexGenerated)->toBe($indexProvided);
63+
});
64+
5265
it('can get a flat index/position array', function () {
5366
$logIndex = createLogIndex();
5467
$firstIndexGenerated = $logIndex->addToIndex(

0 commit comments

Comments
 (0)