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

python: to_be_file #365

Open
nedtwigg opened this issue Apr 29, 2024 · 1 comment
Open

python: to_be_file #365

nedtwigg opened this issue Apr 29, 2024 · 1 comment
Assignees
Labels

Comments

@nedtwigg
Copy link
Member

This is different than to_match_disk. "MatchDisk" has a special file format, garbage-collection, lots of fancy features. to_be_file is only for a single binary facet, it writes the binary to disk unchanged.

The demo that this unlocks is AI image generation, where you can open the snapshot without any extra tooling. Jus ttsay to_be_file("test.jpg") and you can just open the file.

The beginning of it is here:

  • class ToBeFileWriteTracker : WriteTracker<TypedPath, ToBeFileLazyBytes>() {
    fun writeToDisk(
    key: TypedPath,
    snapshot: ByteArray,
    call: CallStack,
    layout: SnapshotFileLayout
    ) {
    val lazyBytes = ToBeFileLazyBytes(key, layout, snapshot)
    recordInternal(key, lazyBytes, call, layout)
    // recordInternal will throw an exception on a duplicate write, so we can safely write to disk
    lazyBytes.writeToDisk()
    // and because we are doing duplicate checks, `ToBeFileLazyBytes` can allow its in-memory
    // data to be garbage collected, because it can safely read from disk in the future
    }
    }
    class ToBeFileLazyBytes(val location: TypedPath, val layout: SnapshotFileLayout, data: ByteArray) {
    /** When constructed, we always have the data. */
    var data: ByteArray? = data
    /**
    * Shortly after being construted, this data is written to disk, and we can stop holding it in
    * memory.
    */
    internal fun writeToDisk() {
    data?.let { layout.fs.fileWriteBinary(location, it) }
    ?: throw IllegalStateException("Data has already been written to disk!")
    data = null
    }
    /**
    * If we need to read our data, we do it from memory if it's still there, or from disk if it
    * isn't.
    */
    private fun readData(): ByteArray = data ?: layout.fs.fileReadBinary(location)
    /** We calculate equality based on this data. */
    override fun equals(other: Any?): Boolean =
    if (this === other) true
    else if (other is ToBeFileLazyBytes) readData().contentEquals(other.readData()) else false
    override fun hashCode(): Int = readData().contentHashCode()
    }
@nedtwigg nedtwigg added the py label Apr 29, 2024
@hssahota2 hssahota2 self-assigned this Apr 29, 2024
@nedtwigg
Copy link
Member Author

nedtwigg commented May 5, 2024

  • basic to_be_file implementation python to_be_file #366
  • needs expect_selfie(actual: bytes) -> BinarySelfie
    • means BinaryFacet
  • needs cache_selfie_binary
    • def cache_selfie_binary(to_cache: Cacheable[bytes]) -> CacheSelfieBinary[bytes]: ...
      def cache_selfie_binary(to_cache: Cacheable[T], roundtrip: CacheSelfie[T, bytes]) -> CacheSelfieBinary[T]: ...

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

2 participants