Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> As my app matured, I found that I often wanted hierarchical folder-like functionality. Rather than recreating this mess in db relationships, it was easier to store the path and other folder-level metadata in sqlite so that I could work with it in Python. E.g. `os.listdir(my_folder)`.

This is a silly argument, there's no reason to recreate the full hierarchy. If you have something like this:

    CREATE TABLE files (path TEXT UNIQUE COLLATE NOCASE);
Then you can do this:

    SELECT path FROM files WHERE path LIKE "./some/path/%";
This gets you everything in that path and everything in the subpaths (if you just want from the single folder, you can always just add a `directory` column). I benchmarked it using hyperfine on the Linux kernel source tree and a random deep folder: `/bin/ls` took ~1.5 milliseconds, the SQLite query took ~3.0 milliseconds (this is on a M1 MacBook Pro).

The reason it's fast is because the table has a UNIQUE index, and LIKE uses it if you turn off case-sensitivity. No need to faff about with hierarchies.

EDIT: btw, I am using SQLite for this purpose in a production application, couldn't be happier with it.



The postgres ltree extension does this beautifully. I'm not sure if similar things exist for sqlite or other rdbmses.

https://www.postgresql.org/docs/current/ltree.html


MSSQL has something similar but no, most other solutions involve recursive queries.


cool. i don't want to recreate a filesystem in my app logic


If that SELECT query is too much for you, I agree, SQLite is maybe not meant for you. Not a very solid argument against SQLite, though.


stop making personal attacks.

i'm sure your suggestion exhibits creative step-one thinking, but i've described several reasons why it doesn't make sense to recreate a filesystem in sqlite, the combination of which should make it clear why doing so is naive


calling someone's idea naive is also kind of a personal attack fwiw


Just pasting a % on your directories to list files is naive. At the very least you need to block/escape % characters in the path.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: