ConnorRigby

ConnorRigby

Nerves Core Team

So i’ve landed some features

In the lower level libs that make up the Sqlite.Ecto2 adapter. Now my problem is one of the actual implementation when working with Ecto. I know Ecto itself doesn’t support any notification system, but
the Postgrex adapter does support notifications. From skimming the source, i found that Postgrex just
opens another connection to the database which is something that sqlite can not support just because how it was built.

so one idea i had was to use Elixir.Registry, so every connection in the pool sends notifications out via Registry.

My issue is that sqlite sends a message that looks like
{:insert | :update | :delete, 'tablename', rowid}
(here are the docs for that)

Basically i get a rowid not a primary key or anything that is actually exposed to the Ecto Repo,
so even if an end user subscribed and got this message, they wouldn’t be able to easily lookup the data
that was affected because rowid is kind of an internal thing.

What i was thinking of doing is:

%{columns: c, rows: [row]} = Ecto.Adapters.SQL.query!(repo, "SELECT * FROM '#{table}' where rowid = #{rowid};", [])

And then sending that message to subscribers of the Registry, so a consumer would have to do:

receive do
   {action, 'users', %{columns: c, rows: [row]}} -> MyRepo.load(User, {result.columns, c, row})
end

The only problem i have with this is that every insert will also have a query to dispatch the notification,
and having user call load seems a little strange also.

Showing Posts 1 to 9

idi527

idi527

:waving_hand:

Thank you for implementing this.

But could the table name in the message be a binary instead of charlist?

If there isn’t a way to lookup the ecto schema module for a table name at runtime, I’d probably just leave the notification as is (that is, I’d just publish the rowid, without any preloads).

Also, can the nif “overload” the subscriber pid with messages? If the changes are published faster than the pid can handle them.

ConnorRigby

ConnorRigby OP

Nerves Core Team

unfortunately the implementation of the message dispatching is handled by a NIF, and the convention for that NIF is to use charlists, because the lib is an Erlang library, not Elixir. Charlists are also faster for NIFs if i remember correctly, since it doesn’t have to allocate, convert to term, deallocate etc.

I don’t think Ecto encourages looking up a schema module at runtime, since multiple schemas could use the same table.

I guess dispatching the rowid isn’t that bad. I could make a wrapper module that one could use by supplying a schema and translate it for the user.

idi527

idi527

That would be perfect, at least for me, I think.

ConnorRigby

ConnorRigby OP

Nerves Core Team

The nif handles messages in a thread, so it’s possible that messages can come in faster than they can be handled, but they should be received in order making this not that big of a deal i don’t think

ConnorRigby

ConnorRigby OP

Nerves Core Team

I’ve started implementing this here

But have ran into a couple issues so far.

  1. inserts are slow while notifications are turned on. Not really sure why.
  2. deletes can’t lookup the thing that was deleted since it doesn’t exist anymore
dimitarvp

dimitarvp

Completely uninformed and probably stupid question:

Does sqlite keep a journal of mappings between a row ID and the row itself, even if short-lived? That could help with the deleted row issue.

Failing that, and if you really want notifications, I’m afraid you’ll have to maintain that journal yourself in an ETS or a DETS table.

ConnorRigby

ConnorRigby OP

Nerves Core Team

Sqlite keeps it opaquely, and garbage collects it before dispatching the deletion notification as far as i can tell.

I’m not incredibly fond of this idea for a couple reasons.

  1. (specifically dets) My primary usage of sqlite is to not rely on DETS/ETS at all.
  2. there is a possibility of missing an insert/update, meaning the deletion would still not have a missing record.
  3. I’d basically be cloning all Repo data in memory.
dimitarvp

dimitarvp

Fair enough, I figured. At this point you’d basically be making Sqlite++ with half the code being the original Sqlite (C++) and the other half Elixir. Far from ideal.

So it’s kind of a private API? Maybe you can ask the maintainer if you can make use of that journalling metadata before it gets GC-ed?

Sorry I am not very helpful, mostly just thinking out loud.

ConnorRigby

ConnorRigby OP

Nerves Core Team

I don’t think its a private api, just part of how the Sqlite3 virtual machine works. You can read about rowid here. Basically it exists, and you can use it to index things, but you can’t look at how it works. (as far as my limited knowledge of the sqlite public api goes anyway)

In most cases the rowid is the primary key, (although this isn’t always true), the only problem is that sqlite internally doesn’t actually dispatch the callback (what sends data back to Beam) until after the row has been destroyed.

There actually is commit and rollback hooks (described here) but that would add more complexity to the NIF than i would ideally give it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews