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

Revisiting knowledgebase persistence #74

Open
guregu opened this issue Nov 4, 2022 · 5 comments
Open

Revisiting knowledgebase persistence #74

guregu opened this issue Nov 4, 2022 · 5 comments

Comments

@guregu
Copy link
Contributor

guregu commented Nov 4, 2022

It'd be cool if we could automatically persist modules somehow. I don't think this needs to be built-in necessarily, instead it might be better to provide a hook for assert/retract and allow users to implement the persistence themselves.

db_log in predicates.c and the experimental persistence directive Trealla used to have is pretty close to what I'm looking for.

Some prior art would be prolog_listen/2,3 from SWI. I would be totally fine with less functionality and a different API though. For example, I don't need the ability to prevent modifications from happening, or be notified of retractall separately from retract, etc. Just hooks that already exist in db_log would be fine, as long as there is a way to grab clauses by the ID that db_log uses, so I know what data to persist.

My use case is a magical persistent interpreter that lives in the cloud™. I'd like to experiment with using Prolog knowledgebases to bang out CRUD APIs, like how auth.pl works. With WASM, we could call into the host in the assert/retract hooks and have the host persist the changes in whatever cloud database thing is convenient (or even browser local storage and such in the case of trealla-js).

@infradig
Copy link
Contributor

infradig commented Nov 4, 2022 via email

@infradig
Copy link
Contributor

infradig commented Nov 4, 2022

The persist directive was really a hangover from a previous Prolog project. I used it to create a demo micro-blogging platform, basically a clone of HN. The thought of managing a web-server & database instance etc was too off-putting even for a toy. But maybe a cloud-based one with persistence to AWS or similar would be doable.

@guregu
Copy link
Contributor Author

guregu commented Nov 5, 2022

Hooks or goal_expansion/2 might indeed work.

Whoops, totally forgot about goal_expansion/2. That might do it.

The persist directive was really a hangover from a previous Prolog project. I used it to create a demo micro-blogging platform, basically a clone of HN.

Interesting! Sounds very cool. I agree that maintaining servers is a pain, especially for fun side projects.

One of Trealla's strengths is that it's very compact. This puts it in a unique position to fit inside the restrictions of serverless/FaaS/PaaS/buzzword runtimes, which have tough size restrictions (Cloudflare Workers is 1MB, for example). My eventual goal for the WASM stuff is to get Trealla running in one of them with persistence. Hopefully this will result in a zero-effort hosting situation for Prolog on the web.

@infradig
Copy link
Contributor

infradig commented Nov 6, 2022 via email

@guregu
Copy link
Contributor Author

guregu commented Nov 6, 2022

No need to apologize! This stuff is very new and unstable and there's like 10 different ways to do everything.

The Trealla WASM port targets a specification called WASI (WebAssembly System Interface). This is basically "POSIX for WASM" and gives us most of the standard C APIs we know and love. The tricky part is that each WASI runtime/library implements these APIs differently. WASI isn't (yet?) built into browsers but it is built into some serverless platforms.

Currently, trealla-js uses wasmer-js which has a virtual in-memory POSIX-ish filesystem. This is ephemeral and disappears on refresh, but it's easy to add persistence. The simplest way would be with localStorage, but there's also the Cache API to store arbitrary cache blobs, IndexedDB which is kind of like an embedded NoSQL DB, the FileSystem API that can access your actual OS filesystem but only works in Chrome, etc.
However, platforms like Cloudflare Workers use their own WASI library that I need to add support for (shouldn't be too hard). They have ephemeral filesystems, but cloud services to store blobs (S3 clones) and key-value DBs and whatnot.
Some runtimes, like wasmer-go which the Go port currently uses, don't have virtual filesystems but chroot-style ones instead.
To complicate things even further, there are runtimes that have their own nonstandard APIs for message passing and stuff but I'll ignore them for now.
And of course, anything that can run container images can run regular Trealla without needing WASM at all.

TL;DR: we have access to a POSIX filesystem but it's usually ephemeral, but it's generally straightforward to add a bit of glue to persist it

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

No branches or pull requests

2 participants