nhpip

nhpip

Hi,

So I’m looking at a project that involves setting special activity flags for users. I won’t go into the reasons why, bit suffice to say that efficiency is pretty important.

Now I have done something similar before where I created a bit-vector. For example:

add_activity = fn(id, activity) -> Bitwise.bor(activity, Bitwise.bsl(1, id)) end
id_active? = fn(id, activity) -> Bitwise.bor(Bitwise.bsl(1, id), activity) == activity end

The numbers do get quite large, but it’s still more efficient than lists or strings. The ability to do bitwise operations really helps performance when searching and updating.

However, I’d like to be able to save the data in PostgreSQL with ecto.

There will be a row per organization per day, but I only need to update the fact a user was active on a given day (so idempotent). It would be nice if we can keep the same efficiency here too (for inserts, updates and search).

Search: It would be nice to be able to say ‘get rows and position numbers that have bit set to 1’.
Update: It would be nice to be ably to apply a bitwise or to update an existing row.

I looked at the following Postgres types:

  • number: efficient, but no real way to do queries
  • bit / var bit: In theory awesome. I can probably write a fragment to do nice queries using bitwise operations. But, Postgres can’t seem to handle large numbers here. I need to do ugly chunking operations to, for example, cast from a numeric type to bit or to do boolean operations (it seems like 64 bits are some kind of internal limit).
  • bytea: possible, although searching could be a pain.
  • array: possibly the best in terms of updates and search, although casting could be awkward. Also, may not be that memory efficient ({:array, :boolean} maybe).

I’m leaning towards an array type (an array of booleans). I would, however, like casting to be efficient as well as memory efficient too.

Anyone have any recommendations?

Showing Posts 1 to 8

dimitarvp

dimitarvp

Nothing immediately jumps to mind except maybe support your {:array, boolean} idea, combined with a library a la ExBin — ExBin v0.4.0.

Also that library supports chunking so maybe it won’t be so awkward.

D4no0

D4no0

Have you thought about using a custom ecto type? seems the perfect use-case to me.

nhpip

nhpip OP

Yes to both of you. Although it would be nice to be able to do:

update Metrics
             set user_activity[2] = 1
             where org_name='Bob's fish';
LostKobrakai

LostKobrakai

Wrap a few of those in fragments / custom macros:

nhpip

nhpip OP

LostKobrakai

LostKobrakai

Not sure I see the exact issue. Casting numbers to bit is documented on the linked page:

In addition, it is possible to cast integral values to and from type bit. Some examples:

44::bit(10) 0000101100
44::bit(3) 100
cast(-44 as bit(12)) 111111010100
‘1110’::bit(4)::integer 14

nhpip

nhpip OP

The problem is it doesn’t work with NUMERICS

=# select cast(2628571181105336077494795701369512280333343682331285668112708328669651138588157877092084196316261329556113852143036127852982354048364156779817408440315960 as bit(1000));
ERROR:  cannot cast type numeric to bit
LINE 1: select cast(262857118110533607749479570136951228033334368233...
               ^
nhpip

nhpip OP

An array of booleans seems to be fine.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews