Skip to content

Add basic File table for (external) files stored separate from database - #2

Open
deruyter92 wants to merge 17 commits into
devfrom
jaap/add-file-schema
Open

deruyter92 wants to merge 17 commits into
devfrom
jaap/add-file-schema

Conversation

@deruyter92

@deruyter92 deruyter92 commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a shared File table for files from outside the pipeline (recordings, videos, configs). The bytes live in a
DataJoint store outside the database; the database keeps their SHA-256 and a FileContent entry references to the store.

Design

  • Immutable File: identity only (file_sha256, size_bytes), can be registered by hash only.
  • FileContent with content: <filepath@scene-files>: the bytes in this deployment's store. Never synced (new SyncAuthority.NOSYNC).
  • One store, scene-files. Each machine maps it in dj.config["stores"] (a lab disk, an S3 bucket).
    Files sit at the content-addressed path sha256/ab/<sha256> in every copy, so copies replicate with
    rclone copy --checksum and a row reads from whichever copy the machine is configured for.
  • Computed data does not go here: it uses DataJoint's own storage types (<npy@>, <object@>, <blob@>)
    in its own tables.
  • No Store, Location and their row-meta tables (as was the case in an earlier proposed version).

Helpers (scene_pipelines.ingestion.files)

  • register_file: store and register a local file (the two steps below); store=False registers the hash only.
  • register_file_hash: write the File row only (bytes kept elsewhere).
  • register_stored_file: write File and FileContent for bytes already in the store
  • store_file: copy into the store with DataJoint's put_file unless present; no database write.
  • verify_file: re-read the stored bytes and check the SHA-256
  • admin.init_file_store: prepare a store copy once, by hand. It writes scene-files.json as top-level marker in the store. Without it the other helpers refuse to store files.

Also

@deruyter92
deruyter92 requested review from Ayskin and C-Achard and removed request for Ayskin and C-Achard October 28, 2026 15:59
@deruyter92
deruyter92 requested review from Ayskin and removed request for C-Achard November 2, 2026 10:22
@deruyter92
deruyter92 requested a review from C-Achard November 3, 2026 09:26

@C-Achard C-Achard left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work overall, but we might need to be careful around certain design choices, especially the current coupling of recording vs copying, see #2 (comment)

Also let's make sure to use DJ's built-in object storage for derived/recomputable data. I think the current design makes sense for raw data acquired only once, shared, and reused across labs; for the rest I think it might be simpler to lean on their existing code

Comment thread scene_pipelines/schemas/files/files.py Resolved
Comment thread scene_pipelines/schemas/files/files.py Outdated
class Location(dj.Manual):
"""A copy of a file in a store, at ``object_key(file_sha256)``; it exists while the copy does.

Every ``File`` has at least one ``Location``. Each store has one writer (the lab's

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that enforced? Should it be?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to think about this. May be difficult to enforce this per site since File is SHARED.

Comment thread scene_pipelines/schemas/files/files.py Resolved
return {"store_name": name}


def _copy_into(path: Path, root: Path) -> tuple[str, int]:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a cleanup for temp files?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Onn a higher level I'm not very convinced by this copy design; an ingestion is always a hand-written object store and I think the amount of issues just in this part of the code point us towards a different design.

  • "Put bytes in a store, verify, record" is a single operation; having two code paths (register_file/record_stored) means two sets of failure modes.
  • _copy_into hashes the source and never checks back what is actually in the store

I think first of all recording and copying should be separate. Over network you may need retries or face bandwith limits as well.

Options seem to be:

  • rclone is super nice here I think, it handles some of the network annoyances, atomic writes, partial writes, and checks. Downside is it will just redo hashing (on its own) due to backend-dependent hashing, but that's a time loss mainly for a safety trade-off that may be acceptable.
  • fsspec is python native but brings again the atomicity and checks burden
  • Make every store S3 (run MiniO or actually Ceph on lab servers) - heavy and inconvenient imo

For semantics/prior art maybe check how git-annex did things. Not an expert, happy to help discuss the design together overall as I may have missed other options. But this is where things could get complicated if we redo everything by hand in Python.

Comment thread tests/test_row_meta.py Resolved
@C-Achard

C-Achard commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

(This is a rather long-term review with a lot of design requests, @deruyter92 the core design is really fine imo aside from the more minor comments I made so let me know if I should approve and move this to an issue preferably, or better indicate what can be solved easily now)

…ck before recording

- use datajoint.storage.StorageBackend
- check the object exists in the store with the given size
- refuse unmounted disk stores and store_kind / dj.config protocol mismatches
- drop SCENE_STORE, ACQUISITION write role
- File: raw data only; derived data uses DataJoint's own object storage
@deruyter92
deruyter92 marked this pull request as draft November 4, 2026 17:24
@deruyter92

Copy link
Copy Markdown
Collaborator Author

@C-Achard Thanks for your elaborate initial review. I will triage what we can put in a first working version while keeping the remaining issues open.

I think it is important to make better use of what is available from DataJoint as well. And let's test it properly when we have access to the Simons s3 infrastructure.

I am keeping the PR as draft for now while working on a better implementation.

2.3.3 makes file-protocol stores safe on Windows (POSIX separators; the
garbage collector could delete live files before). 2.3.6 is a security
release, including path checks for every store path.
keep SHA-256 and add `content: <filepath@scene-files>`, which is DJ's canonical codec for this use. Each deployment can have it's own definition in dj.config["stores"]["scene-files"] to decide which copy it reads. `Store`, `Location`, their row-meta tables and `ensure_store` are removed.

- register_file: hash, copy with DataJoint's storage backend unless present, record
- record_file: record bytes a transfer job (e.g. rclone) already put in place
- an `s3` extra (s3fs) and a DJ_STORES example in .env.example
DataJoint checks only that a <filepath@> file exists (and its size on
fetch). verify_file re-reads the bytes, in chunks, and compares SHA-256
and size with the File row: an audit to run now and then.
Stricter and separate init_file_store; avoids wrongly initializing a new file store for File ingestion by accident (e.g. when address contains a typo).

register_file and record_file now refuse a store without scene-files.json
at its root. Only admin.init_file_store() writes it, once per copy of the
store. It refuses a copy that is already prepared, so it cannot become a
check at the start of a script, and a place that holds none of a few
files the database records (unmounted, wrong location) unless
new_copy=True.

Also shortens the docstrings of the files modules.
Whoever registered the first file under a prefix created sha256/ab/ with
their own umask (often 0755), so other users could not add files to it.
init_file_store now creates sha256/00 to sha256/ff up front, on POSIX
group-writable with the setgid bit, so they take the root folder's group.
Deleting a wrong-size copy first is only needed on disk, where put_file
cannot overwrite on Windows (datajoint-python#1744). On S3 a PUT
overwrites, so writers can do without delete permission. Document a
lifecycle rule for incomplete multipart uploads.
store_file puts the bytes in the store; register_stored_file writes the
File row. (can be used downstream by lab-tables for specific modalities)

A strict xfail on Windows tracks datajoint-python#1744; once it passes,
the remove() before put_file can go.
@deruyter92
deruyter92 marked this pull request as ready for review November 23, 2026 14:14
@deruyter92

Copy link
Copy Markdown
Collaborator Author

@C-Achard thanks again for the review. I reworked the PR. Let me know what you think.

New design
Now using DataJoint's storage method instead of our own copy code. Copying (store_file) and registering (register_stored_file) are now separate steps. register_file calls both. Store and Location are gone: each machine's dj.config["stores"] decides which copy it reads.

Still open:

  • A file that changes while it is stored goes unnoticed (documented now, and opened an issue with DJ).
  • Bytes stay in the store when registration fails or a file is abandoned; nothing deletes automatically,
    since the store is shared. I will open issues for this to follow up.

@deruyter92 deruyter92 changed the title Add basic files schema: File, Store, Location Add basic File table for (external) files stored separate from database Nov 23, 2026
@C-Achard
C-Achard self-requested a review November 23, 2026 14:46

@C-Achard C-Achard left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

The only points I would ask about are:

  • The File/FileContent split
  • Should we figure out how to setup test S3 in CI, also to get hands-on experience (and it would likely be useful later)

Otherwise I'd rather keep this moving and test it live over further fine-tuning,so I'm approving now

Comment on lines +17 to +32
@mark_sync_authority(SyncAuthority.SHARED)
@schema
class File(dj.Manual):
"""One file from outside the pipeline (recording, video, config), identified by SHA-256.

Immutable and the same everywhere, so not stamped as RowMeta. What the file is belongs to
the table referencing it; computed data uses DataJoint's own storage types instead.
``content`` fetches as an ``ObjectRef`` (``read()``, ``open()``, ``download()``).
"""

definition = f"""
file_sha256: char(64) # lowercase hex SHA-256 of the bytes
---
size_bytes: int64
content: <filepath@{FILE_STORE}> # the bytes in the store, at object_key(file_sha256)
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have a small request for local vs stored granularity, could we do a shape more like:

@schema
class File(dj.Manual):          # no store needed
    definition = """
    file_sha256: char(64)
    ---
    size_bytes: int64
    """

@schema
class FileContent(dj.Manual):   # added later, optionally
    definition = f"""
    -> File
    ---
    content: <filepath@{FILE_STORE}>
    """

Note that this is more of a feature request, but it matters for e.g. human data and local deployment I think.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the granularity topic:

  • For live files: a <filepath@> store over a lab's live folder references files in place, but DataJoint records path, size and timestamp only, and reads whatever is there. Might need a separate FileLocation(-> File, store, path, seen_at), re-hashed before use, over a FileContent row.
  • For local backup first, S3 later: A synced FileContent row would point to bytes the central deployment cannot read before syncing, so "bytes check then sync rows" would need enforcing in sync.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this also unlocks size/SHA checks without needing the store, which is also quite useful imo

@deruyter92 deruyter92 Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree with the separation, good suggestion. I was thinking making a separate syncing category NEVER / NOSYNC or similar - meaning, the FileContent reference always stays on the machine, and never syncs instead of syncing the FileContent, we can re-register the reference by create the FileContent entry on the central DB separately. This means that the central database can never lie about having FileContent that is not actually in the store.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding CI - I have some notebooks for testing it locally. I will have a look for configuring something in CI. Thanks!

…thority.NOSYNC

A file can now be registered by hash while its bytes stay elsewhere (register_file(path, store=False),
register_file_hash).

FileContent (-> File, <filepath@scene-files>) holds THIS database's store copy of the bytes.

A new SyncAuthority.NOSYNC marks the FileContent to be never synchronized.
@deruyter92
deruyter92 added this pull request to stack #17 November 24, 2026 17:04
@deruyter92
deruyter92 removed this pull request from stack #17 November 24, 2026 17:04
@deruyter92
deruyter92 added this pull request to stack #19 November 24, 2026 17:07

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants