Skip to content

Commit

Permalink
fixed blockstore, generated blockstore migration file, cleaned up lin…
Browse files Browse the repository at this point in the history
…gering warnings
  • Loading branch information
mekaem committed Jul 4, 2024
1 parent 19283c8 commit d545572
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 110 deletions.
2 changes: 1 addition & 1 deletion lib/hexpds/auth/session/cleaner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Hexpds.Auth.Session.Cleaner do
Memento.transaction!(fn ->
:mnesia.foldl(
fn {_, r_jwt, _, _}, _ ->
unless Hexpds.Auth.JWT.verify(r_jwt) do
unless is_map(Hexpds.Auth.JWT.verify(r_jwt)) do
Hexpds.Auth.Session.delete(r_jwt)
end
end,
Expand Down
86 changes: 5 additions & 81 deletions lib/hexpds/blockstore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,16 @@ defmodule Hexpds.BlockStore do
@callback del_block(key :: binary()) :: :ok | {:error, term()}
end

defmodule Hexpds.BlockStoreServer do
use GenServer
@behaviour Hexpds.BlockStore

def init(init_arg) do
{:ok, init_arg}
end

def start_link(inital_state \\ %{}) do
GenServer.start_link(__MODULE__, inital_state, name: __MODULE__)
end

def put_block(key, value) do
GenServer.call(__MODULE__, {:put_block, key, value})
end

def get_block(key) do
GenServer.call(__MODULE__, {:get_block, key})
end

def del_block(key) do
GenServer.cast(__MODULE__, {:del_block, key})
end

def init() do
{:ok, :state}
end

def handle_call({:put_block, key, value}, _from, state) do
case Map.fetch(state, key) do
{:ok, ^value} -> {:reply, :ok, state}
{:ok, _} -> {:reply, {:error, :immutable_value}, state}
:error -> {:reply, :ok, Map.put(state, key, value)}
end
end

def handle_call({:get_block, key}, _from, state) do
case Map.fetch(state, key) do
{:ok, value} -> {:reply, {:ok, value}, state}
:error -> {:reply, {:error, :not_found}, state}
end
end

def handle_cast({:del_block, key}, state) do
{:noreply, Map.delete(state, key)}
end
end

defmodule BlockStore.Repo do
use Ecto.Repo,
otp_app: :blockstore_app,
adapter: Ecto.Adapters.SQLite3
end

defmodule BlocksTable do
use Ecto.Schema

schema "blocks" do
field(:key, :string)
field(:value, :string)
field(:value, :binary)
end
end

defmodule Hexpds.EctoBlockStore do
use Ecto.Repo, otp_app: :hexpds, adapter: Ecto.Adapters.SQLite3
import Ecto.Query
@behaviour Hexpds.BlockStore

Expand All @@ -77,9 +22,9 @@ defmodule Hexpds.EctoBlockStore do
end

def put_block(key, value) do
case get_by(BlocksTable, key: key) do
case Hexpds.Database.get_by(BlocksTable, key: key) do
nil ->
case insert!(%BlocksTable{key: key, value: value}) do
case Hexpds.Database.insert!(%BlocksTable{key: key, value: value}) do
{:ok, _} -> :ok
{:error, _} -> {:error, :insert_failed}
end
Expand All @@ -96,34 +41,13 @@ defmodule Hexpds.EctoBlockStore do
end

def get_block(key) do
case get_by(BlocksTable, key: key) do
case Hexpds.Database.get_by(BlocksTable, key: key) do
nil -> {:error, :not_found}
block -> {:ok, block.value}
end
end

def del_block(key) do
delete_all(from(b in BlocksTable, where: b.key == ^key))
end
end

defmodule Hexpds.OverlayBlockStore do
@behaviour Hexpds.BlockStore

defstruct upper: nil, lower: nil

def put_block(%{upper: upper}, key, value) do
Hexpds.BlockStore.put_block(upper, key, value)
end

def get_block(%{upper: upper, lower: lower}, key) do
case BlockStore.get_block(upper, key) do
{:error, :not_found} -> BlockStore.get_block(lower, key)
result -> result
end
end

def del_block(%{upper: upper}, key) do
BlockStore.del_block(upper, key)
Hexpds.Database.delete_all(from(b in BlocksTable, where: b.key == ^key))
end
end
3 changes: 0 additions & 3 deletions lib/hexpds/cid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,6 @@ defmodule Hexpds.CID do
with {:ok, encoded_multihash} <- Multicodec.encode(multihash, codec),
encoded_version = Varint.LEB128.encode(version) do
{:ok, <<encoded_version::binary, encoded_multihash::binary>>}
else
{:error, _reason} = err -> err
_ -> {:error, "unable to encode buffer."}
end
end

Expand Down
41 changes: 24 additions & 17 deletions lib/hexpds/didplc.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
defmodule Hexpds.DidPlc do
defmodule Services do
defmodule AtprotoPds do
@type t :: %__MODULE__{endpoint: String.t(), type: String.t()}
@derive Jason.Encoder
defstruct [:endpoint, type: "AtprotoPersonalDataServer"]
end

@derive Jason.Encoder
defstruct [:atproto_pds]

@type t :: %__MODULE__{atproto_pds: AtprotoPds.t()}

defmodule AtprotoPds do
@type t :: %__MODULE__{endpoint: String.t(), type: String.t()}
@derive Jason.Encoder
defstruct [:endpoint, type: "AtprotoPersonalDataServer"]
end
end

defmodule Operation do
defmodule VerificationMethods do
defstruct([:atproto])
@type t :: %__MODULE__{atproto: Hexpds.K256.PrivateKey.t()}
end

defstruct [
:rotationKeys,
:prev,
Expand All @@ -18,23 +42,6 @@ defmodule Hexpds.DidPlc do
type: String.t()
}

defmodule VerificationMethods do
defstruct([:atproto])
@type t :: %__MODULE__{atproto: Hexpds.K256.PrivateKey.t()}
end

defmodule Services do
@type t :: %__MODULE__{atproto_pds: AtprotoPds.t()}
@derive Jason.Encoder
defstruct [:atproto_pds]

defmodule AtprotoPds do
@type t :: %__MODULE__{endpoint: String.t(), type: String.t()}
@derive Jason.Encoder
defstruct [:endpoint, type: "AtprotoPersonalDataServer"]
end
end

def genesis(
%Hexpds.K256.PrivateKey{} = rotationkey,
%Hexpds.K256.PrivateKey{} = signingkey,
Expand Down
2 changes: 1 addition & 1 deletion lib/hexpds/mst.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Hexpds.MST do
alias Hexpds.MST
alias Hexpds.MST.MSTNode, as: MSTNode
@hash_fun :sha256

Expand Down Expand Up @@ -77,6 +76,7 @@ end
defmodule Hexpds.MSTServer do
use GenServer
alias Hexpds.MST.MSTNode, as: MSTNode
@hash_fun :sha256

# Starting the server with an empty root
@spec start_link(any()) :: :ignore | {:error, any()} | {:ok, pid()}
Expand Down
6 changes: 3 additions & 3 deletions lib/hexpds/service/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ defmodule Hexpds.Http do
%{cids: cids, cursor: next_cursor} ->
{200, %{cursor: next_cursor, cids: Enum.map(cids, &to_string/1)}}

other ->
{400, %{error: "InvalidRequest", message: inspect(other)}}
_ ->
{400, %{error: "InvalidRequest", message: "Unknown user."}}
end
end

Expand All @@ -250,7 +250,7 @@ defmodule Hexpds.Http do
end

@spec xrpc_procedure(Plug.Conn.t(), String.t(), map(), Hexpds.Auth.Context.t()) ::
{integer(), map()}
{integer(), map() | {:blob, Hexpds.Blob.t()}}

XRPC.procedure _,
"com.atproto.server.createSession",
Expand Down
12 changes: 8 additions & 4 deletions lib/hexpds/service/xrpc/listBlobs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ defmodule Hexpds.Xrpc.Query.ListBlobs do
end
|> Hexpds.Database.all()

%{
cids: cids,
cursor: to_string(List.last(cids))
}
unless Hexpds.User.get(did) do
%{}
else
%{
cids: cids,
cursor: to_string(List.last(cids))
}
end
end
end
10 changes: 10 additions & 0 deletions priv/database/migrations/20240704032607_blockstore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Hexpds.Database.Migrations.Blockstore do
use Ecto.Migration

def change do
create table(:blocks) do
add :key, :string
add :value, :binary
end
end
end

0 comments on commit d545572

Please sign in to comment.