-
Notifications
You must be signed in to change notification settings - Fork 64
Fixed prettyIndex for one-line files, use binarySearch #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephenamar-db
merged 2 commits into
databricks:master
from
hauserx:faster-pretty-index
Nov 20, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package sjsonnet | ||
|
|
||
| import utest._ | ||
|
|
||
| import java.io.{File, FileOutputStream} | ||
| import java.nio.file.Files | ||
| import scala.collection.mutable | ||
|
|
||
| object FileParserInputTests extends TestSuite { | ||
| private val tempFiles = mutable.ArrayBuffer[File]() | ||
|
|
||
| def createTestFile(content: String): FileParserInput = { | ||
| val tempFile = Files.createTempFile("fileparser-", ".txt").toFile | ||
| tempFiles += tempFile | ||
| val fos = new FileOutputStream(tempFile) | ||
| try { | ||
| fos.write(content.getBytes("UTF-8")) | ||
| } finally { | ||
| fos.close() | ||
| } | ||
| FileParserInput(tempFile) | ||
| } | ||
|
|
||
| val tests: Tests = Tests { | ||
| test("prettyIndex - empty file") { | ||
| val input = createTestFile("") | ||
| val result = input.prettyIndex(0) | ||
| assert(result == "1:1") | ||
| } | ||
|
|
||
| test("prettyIndex - single line without newline") { | ||
| val input = createTestFile("hello world") | ||
|
|
||
| assert(input.prettyIndex(0) == "1:1") | ||
| assert(input.prettyIndex(1) == "1:2") | ||
| assert(input.prettyIndex(6) == "1:7") | ||
| assert(input.prettyIndex(10) == "1:11") | ||
| } | ||
|
|
||
| test("prettyIndex - single line with newline") { | ||
| val input = createTestFile("hello world\n") | ||
|
|
||
| assert(input.prettyIndex(0) == "1:1") | ||
| assert(input.prettyIndex(5) == "1:6") | ||
| assert(input.prettyIndex(11) == "1:12") | ||
| } | ||
|
|
||
| test("prettyIndex - multiple lines with different lengths") { | ||
| val input = createTestFile("a\nbb\nccc\n") | ||
|
|
||
| assert(input.prettyIndex(0) == "1:1") | ||
| assert(input.prettyIndex(1) == "1:2") | ||
|
|
||
| assert(input.prettyIndex(2) == "2:1") | ||
| assert(input.prettyIndex(3) == "2:2") | ||
| assert(input.prettyIndex(4) == "2:3") | ||
|
|
||
| assert(input.prettyIndex(5) == "3:1") | ||
| assert(input.prettyIndex(6) == "3:2") | ||
| assert(input.prettyIndex(7) == "3:3") | ||
| assert(input.prettyIndex(8) == "3:4") | ||
| } | ||
|
|
||
| test("prettyIndex - no trailing newline") { | ||
| val input = createTestFile("line 1\nline 2\nline 3") | ||
|
|
||
| assert(input.prettyIndex(0) == "1:1") | ||
| assert(input.prettyIndex(7) == "2:1") | ||
| assert(input.prettyIndex(14) == "3:1") | ||
| assert(input.prettyIndex(19) == "3:6") | ||
| } | ||
|
|
||
| test("prettyIndex - large file with many lines") { | ||
| val lines = (1 to 1000).map(i => s"Line $i").mkString("\n") | ||
| val input = createTestFile(lines) | ||
|
|
||
| val line1Bytes = 9 * 7 | ||
| val line10Bytes = 90 * 8 | ||
| val line100Bytes = 400 * 9 | ||
| val offset500 = line1Bytes + line10Bytes + line100Bytes | ||
|
|
||
| val result = input.prettyIndex(offset500) | ||
| assert(result == "500:1") | ||
| } | ||
| } | ||
|
|
||
| override def utestAfterAll(): Unit = { | ||
| tempFiles.foreach(_.delete()) | ||
| tempFiles.clear() | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change it to
0, it will always later be resiezd then