Skip to content
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

Random access to files #231

Open
LewsTherinTelescope opened this issue Oct 20, 2023 · 4 comments
Open

Random access to files #231

LewsTherinTelescope opened this issue Oct 20, 2023 · 4 comments
Labels

Comments

@LewsTherinTelescope
Copy link

Multiplatform Sources and Sinks are already great features, but it would also be nice to have random-access capabilities like Okio's FileHandle class. The BufferView option discussed in #225 would help in some cases, but in other cases it's impractical to load the entire file into memory to get at a small part of it—reading a few individual files from an archive, for example. As far as I know the only way to do this right now is to create a new Source and skip to the specified offset, which isn't particularly convenient. (In fact, if I'm reading the code correctly, the skip function does read everything up until that point into a buffer temporarily? Which would make it a no-go for those usecases as well.)

(I've tried to search both the repo and Slack to make sure there wasn't existing discussion about this, but if I've missed something please point me to it!)

@fzhinkin fzhinkin added the fs label Oct 20, 2023
@ptitjes
Copy link

ptitjes commented Nov 2, 2023

@fzhinkin I also am in need of this feature, as I am also using Okio's FileHandles in my projects. I think Okio's current design on this is rather genuine. Would you accept a PR that replicates their design?

@fzhinkin
Copy link
Collaborator

fzhinkin commented Nov 6, 2023

@ptitjes there is a plan to add proper file support to kotlinx-io (replacing or extending existing API that was added mainly to plug a few specific use cases), but the corresponding API should be designed first. The latter means that Okio's FS API will be considered but will be not necessarily taken as is.
At this point, I would abstain from taking FileHandle from Okio until it is clear what future kotlinx-io's file support will look like.

@bjhham
Copy link

bjhham commented Dec 9, 2024

I was also looking for random access files, so I ended up writing a pretty simple implementation to get me by for the JVM. Maybe you could consider it in the design.

Example usage:

fs.randomAccessStorage(file).use { storage ->
    Buffer().use { buffer ->
        while (storage.readAtMostTo(buffer, blockSize) == blockSize) {
            storageAdapter.read(buffer)?.let { row ->
                if (query(scope + row)) {
                    // roll back to start
                    storage.position -= blockSize
                    // write the new item
                    val newRow = fromRow(row.update(mod.changes))
                    storageAdapter.write(newRow, buffer)
                    storage.write(buffer, blockSize)
                    index[newRow] = storage.position
                }
            }
        }
    }
}

Code:
See https://pl.kotl.in/TI-mCJ5mc

@amal
Copy link

amal commented Dec 9, 2024

@bjhham You can take a look at fluxo-io. It provides different RandomAccessData implementations (nine options for JVM, and one cross-platform). I plan to add kotlinx-io based cross-platform implementation soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants