mnesia


how to use mnesia:select/4 and mnesia:select/1 for paging query


a table named "md" with structure {id,name},I want read records from md use paging query,I tried mnesia:select/4 and mnesia:select/1 as below:
%% first use select/2: "ID < 10",returned [1,2,4,3,8,5,9,7,6]
(ejabberd#localhost)5> mnesia:activity(transaction,fun mnesia:select/2,md, [{{md,'$1','_'},[{'<','$1',10}],['$1']}]).
{atomic,[1,2,4,3,8,5,9,7,6]}
%%but when query with select/4,returned [6], why?
(ejabberd#localhost)7> {atomic,{R1,C1}}=mnesia:activity(transaction,fun mnesia:select/4,md,[{{md,'$1','_'},[{'<','$1',10}],['$1']}],5,read).
{atomic,{[6],
{mnesia_select,md,
{tid,10535470,<0.460.0>},
ejabberd#localhost,disc_only_copies,
{dets_cont,select,5,
<<0,0,0,29,18,52,86,120,0,0,0,21,131,104,3,...>>,
{141720,148792,<<>>},
md,<0.130.0>,<<>>},
[],undefined,undefined,
[{{md,'$1','_'},[{'<','$1',10}],['$1']}]}}}
%% and then use mnesia:select/1 with continuation "C1",got wrong_transaction
(ejabberd#localhost)8> mnesia:activity(transaction,fun mnesia:select/1,C1).
{aborted,wrong_transaction}
how to use mnesia:select/4 and mnesia:select/1 for paging query?
You will have to call select/1 inside the same transaction.
Otherwise the table can change between invocations to select/4 and select/1.
You must use a dirty context if you want to use is as written above.
here is my solution:
use async_dirty instead of transaction
{Record,Cont}=mnesia:activity(async_dirty, fun mnesia:select/4,[md,[{Match_head,[Guard],[Result]}],Limit,read])
then read next Limit number of records:
mnesia:activity(async_dirty, fun mnesia:select/1,[Cont])
full code:
-record(md,{id,name}).
batch_delete(Id,Limit) ->
Match_head = #md{id='$1',name='$2'},
Guard = {'<','$1',Id},
Result = '$_',
{Record,Cont} = mnesia:activity(async_dirty, fun mnesia:select/4,[md,[{Match_head,[Guard],[Result]}],Limit,read]),
delete_next({Record,Cont}).
delete_next('$end_of_table') ->
over;
delete_next({Record,Cont}) ->
delete(Record),
delete_next(mnesia:activity(async_dirty, fun mnesia:select/1,[Cont])).
delete(Records) ->
io:format("delete(~p)~n",[Records]),
F = fun() ->
[ mnesia:delete_object(O) || O <- Records]
end,
mnesia:transaction(F).

Related Links

Mesia activity/4 is reading only from the first fragment
Is there a built-in mechanism for us to launch Mnesia in Elixir?
how to use mnesia:select/4 and mnesia:select/1 for paging query
Querying mnesia Fragmentated Tables using QLC returns wrong results

Categories

HOME
python-2.7
rust
vsts-build
liferay-7
iptables
apache-kafka-connect
visualforce
firebase-dynamic-links
phpseclib
polymer-2.x
driver
watch-os-3
sonicwall
pygobject
playframework-2.0
cpu-usage
wsf
data-type-conversion
sqlplus
backtracking
conda
stackexchange.redis
generator
acl
strophe.js
android-service
tf-idf
history.js
react-bootstrap-table
winsock
progid
rust-cargo
seafile-server
winrm
adminer
apptentive
char-pointer
transform
pdfnet
jslint
love2d
jade4j
google-prediction
web-inspector
wysihtml5
sonicmq
simple-schema
google-news
patternlab.io
mobile-angular-ui
dex
openerp-6
scheduledexecutorservice
jsf-2.3
wolfram-language
highlighting
mathnet
utf-16
persistent-object-store
gravatar
flask-security
grinder
ibm-was
ambiguity
mobilefirst-server
clarion
subfolder
mxe
findersync
createfile
n-tier-architecture
rpg
network-printers
redpitaya
neo4jphp
commenting
jms-serializer
insertonsubmit
chartfx
donut-chart
oembed
lynx
first-class
kaazing
java.lang.class
flymake
dropshadow
background-repeat
qtextbrowser
motodev-studio
pyjamas
downgrade
hungarian-notation
outlook-2007-addin
odac
urchin
j2mepolish
jquery-effects
.net-client-profile
downcasting
remote-working

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App