Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dunixir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
teaching
dunixir
Merge requests
!36
Resolve "Vérification de l’existence dans la piscine/sandbox/mempool"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Resolve "Vérification de l’existence dans la piscine/sandbox/mempool"
183-verification-de-l-existence-dans-la-piscine-sandbox-mempool
into
dev
Overview
0
Commits
2
Pipelines
2
Changes
4
Merged
Resolve "Vérification de l’existence dans la piscine/sandbox/mempool"
ABDELGHANI Nassim
requested to merge
183-verification-de-l-existence-dans-la-piscine-sandbox-mempool
into
dev
Feb 14, 2022
Overview
0
Commits
2
Pipelines
2
Changes
4
Closes
#183 (closed)
Edited
Feb 14, 2022
by
ABDELGHANI Nassim
0
0
Merge request reports
Compare
dev
dev (base)
and
latest version
latest version
1834feb3
2 commits,
Feb 14, 2022
4 files
+
89
−
12
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
lib/doc/mempool.ex
0 → 100644
+
42
−
0
View file @ 1834feb3
Edit in single-file editor
Open in Web IDE
defmodule
Doc
.
Mempool
do
@moduledoc
"""
Module for the mempool, i.e. documents that are yet to be added to a block
Used this [pattern](https://elixirforum.com/t/ets-or-genserver-for-caching-messages/42065) :
- GenServer owning an ETS table
- GenServer handling write access
- direct ETS calls for read access and concurency
"""
use
GenServer
def
init
(
_init_arg
)
do
:ets
.
new
(
:mempool
,
[
:set
,
:protected
,
:named_table
])
{
:ok
,
0
}
end
def
start_link
(
_initial_value
)
do
GenServer
.
start_link
(
__MODULE__
,
nil
,
name:
__MODULE__
)
end
@doc
"""
Check whether a document is already in the mempool or not
"""
def
can_add?
(
document
)
do
case
:ets
.
match
(
:mempool
,
{
:"$1"
,
document
})
do
[]
->
true
_
->
false
end
end
@doc
"""
Add a document in the mempool
"""
def
add
(
document
)
do
GenServer
.
call
(
__MODULE__
,
{
:add
,
document
})
end
def
handle_call
({
:add
,
document
},
_from
,
state
)
do
:ets
.
insert
(
:mempool
,
{
state
+
1
,
document
})
{
:reply
,
:ok
,
state
+
1
}
end
end
Loading