niamtokik / cozo

An interface to cozodb
https://github.com/niamtokik/cozo
BSD 2-Clause "Simplified" License
7 stars 1 forks source link

Couple of issues starting in Elixir #29

Closed Madd0g closed 5 months ago

Madd0g commented 11 months ago
  1. I don't have wget so the installation didn't work on my mac.
  2. I worked my way around that problem and tried to use the library in my elixir app.

Note I am a beginner in elixir, so I might be doing something wrong.

When I try to match the output of cozo.open:

{:ok, db} = :cozo.open(:rocksdb, ~c"./dbrocks")

I get the warning:

The pattern can never match the type.

Pattern:
{:ok, _db}

Type:
{:error, _}

So I stumbled through this, printed out the output of cozo.open:

{:ok, {:cozo, 0, :rocksdb, ~c"./dbrocks", %{}, #PID<0.249.0>}}

then I tried to actually run a query, the only thing that worked for me is:

result = :cozo.run(0, String.to_charlist(query))

I don't know what that zero is, but it's the only thing that worked for me. What I'm trying to do is create a GenServer that starts cozo in init and saves the result in the state. I tried passing the entire object to cozo.run, but only passing the zero worked.

Am I doing it wrong?

Thanks

niamtokik commented 6 months ago

Hi @Madd0g,

Sorry for the (very very) late answer. I'm not using MacOS but @aramallo is using it, and it was working on his side.

  1. Regarding wget, you can do a quick fix in the Makefile, do you have an alternative on MacOS for that? Perhaps curl?
  2. 0 is the reference of the opened database. A new release was planned to deal with that and hide totally this reference. In fact, the rust version (not ready) will use a reference instead of an integer. Then, what you are currently is okay.

I switched to another project after I got stuck in front of rust months ago. I currently don't have lot of time to give for cozodb, if I find a moment, I will fix that for you :)

Madd0g commented 6 months ago

thanks for replying anyway, even if late :)

I wasn't sure I was doing it correctly, what I was really worried about is all the warnings I get is vscode when trying to match the open/2

here's my entire code that has this problem:

    result = :cozo.open(:rocksdb, ~c"./dbrocks")

    case result do
      {:ok, Db} ->
        # new_state = Map.put(state, :db_id, id) # 2nd part of the tuple "Db"
        new_state = Map.put(state, :db_id, 0)
        create_initial_relations(new_state)
        {:ok, new_state}

      {:error, err} ->
        raise(err)
    end

VSCode is warning about the {:ok, Db} -> part, saying:

The pattern can never match the type.

Pattern:
{:ok, Db}

Type:
{:error, _}
ElixirLS Dialyzer

It's been a while since I fired up that project, I tried to compile with iex just now and I don't see the warning when compiling, only in vscode, so maybe it's just a vscode problem?

niamtokik commented 6 months ago

Oh! I see the issue! Db in Elixir is not a variable, it starts with an uppercase letter, so, it will be an atom. Here the proof:

true = :erlang.is_atom(Db)

So, by doing what you are doing, the left side will never match the right side. To fix this issue, you will need to do something like that:


    case result do
      {:ok, db} ->
        # new_state = Map.put(state, :db_id, id) # 2nd part of the tuple "Db"
        new_state = Map.put(state, :db_id, 0)
        create_initial_relations(new_state)
        {:ok, new_state}

      {:error, err} ->
        raise(err)
    end

Then, you will have the correct data-structure from :cozo.open/2. So, that's not a VSCode issue, that's a typo on your side I think. :)

records are not supported on elixir side though, so you will need to match the whole data-structure from the function (a tuple) or you will need to decompose the tuple by hand but in this case... If the structure is changing, you will be impacted. I think creating a friendly interface to Elixir developers could help there.