[erlang-questions] HOW TO IMPROVE THE PERFORMANCE OF DETS

Ulf Wiger ulf@REDACTED
Fri Dec 22 11:21:42 CET 2006


Den 2006-12-22 11:05:54 skrev chamila piyasena <tchamila@REDACTED>:

> Thanks Wiger,
>
> But the dets records wont update, It only stores at the files and  
> retrieved
> when needed in this application. and as I mentioned in a previous mail a  
> new
> dets file is created every  hour and named according to the date.
> so i don't think  I can apply this to it.
>
> thanx

So if they're not updated, that's one less problem. The main
idea was that when you create a dets file, you create a second
(or more) dets file at the same time that maps a secondary key
to the primary key.

Say you have a record, #book{isbn, title, authors}

You store the #book{} records in the primary dets file.
If you also want to retrieve all books written by a certain
author, you can make sure that there is a mapping table on
the side:

insert_author(#book{isbn = I, authors = As} = B) ->
    dets:insert(book_table, B),
    lists:foreach(
        fun(A) -> dets:insert(
                     author_index,
                     {A, I})
        end, As).

books_by_author(A) ->
    Res = dets:lookup(author_index, A),
    lists:map(
        fun({_, ISBN}) ->
            [Book] = dets:lookup(book_table, ISBN),
            Book
        end, Res).

(book_table is a set with {keypos, 2},
author_index is a bag with {keypos,1})

The obvious tradeoff is that it will cost more to write
the data, but if you only do it once, and then mainly
retrieve data, it's a good deal.

BR,
Ulf W

-- 
Ulf Wiger




More information about the erlang-questions mailing list