2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : pubsub_index.erl
|
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Provide uniq integer index for pubsub node
|
|
|
|
%%% Created : 30 Apr 2009 by Christophe Romain <christophe.romain@process-one.net>
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2009-04-30 07:18:06 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
2009-04-30 07:18:06 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-04-30 07:18:06 +02:00
|
|
|
|
|
|
|
%% important note:
|
|
|
|
%% new/1 and free/2 MUST be called inside a transaction bloc
|
|
|
|
|
|
|
|
-module(pubsub_index).
|
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
|
|
|
-include("pubsub.hrl").
|
|
|
|
|
|
|
|
-export([init/3, new/1, free/2]).
|
|
|
|
|
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, pubsub_index,
|
2015-04-08 17:12:05 +02:00
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_index)}]).
|
2009-04-30 07:18:06 +02:00
|
|
|
|
|
|
|
new(Index) ->
|
|
|
|
case mnesia:read({pubsub_index, Index}) of
|
2015-04-08 17:12:05 +02:00
|
|
|
[I] ->
|
|
|
|
case I#pubsub_index.free of
|
|
|
|
[] ->
|
|
|
|
Id = I#pubsub_index.last + 1,
|
|
|
|
mnesia:write(I#pubsub_index{last = Id}),
|
|
|
|
Id;
|
|
|
|
[Id | Free] ->
|
|
|
|
mnesia:write(I#pubsub_index{free = Free}), Id
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
mnesia:write(#pubsub_index{index = Index, last = 1, free = []}),
|
|
|
|
1
|
2009-04-30 07:18:06 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
free(Index, Id) ->
|
|
|
|
case mnesia:read({pubsub_index, Index}) of
|
2015-04-08 17:12:05 +02:00
|
|
|
[I] ->
|
|
|
|
Free = I#pubsub_index.free,
|
|
|
|
mnesia:write(I#pubsub_index{free = [Id | Free]});
|
|
|
|
_ ->
|
|
|
|
ok
|
2009-04-30 07:18:06 +02:00
|
|
|
end.
|