Database in memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    #1

    Database in memory

    I have an application that will maintain an in-memory database in the
    form of a list of lists. Does anyone know of a way to search for and
    retreive "records" from such a structure?

    Many thanks,
    bootkey

  • Gabriel Genellina

    #2
    Re: Database in memory

    En Mon, 09 Apr 2007 10:19:12 -0300, Jim <cook_jim@yahoo .comescribió:
    I have an application that will maintain an in-memory database in the
    form of a list of lists. Does anyone know of a way to search for and
    retreive "records" from such a structure?
    Why not a true database? SQLite can handle an in-memory database.

    --
    Gabriel Genellina

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Database in memory

      In <1176124752.172 926.129850@w1g2 000hsg.googlegr oups.com>, Jim wrote:
      I have an application that will maintain an in-memory database in the
      form of a list of lists. Does anyone know of a way to search for and
      retreive "records" from such a structure?
      Scan the list of lists with a ``for`` loop. Or build indexes with
      dictionaries.

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Jeremy Sanders

        #4
        Re: Database in memory

        Jim wrote:
        I have an application that will maintain an in-memory database in the
        form of a list of lists. Does anyone know of a way to search for and
        retreive "records" from such a structure?
        The dictionary is the obvious way to index things:

        # items consist of name and age
        data = [
        ['fred', 42],
        ['jim', 16], ...
        ]

        name_index = {}
        for item in data:
        name_index[item[0]] = item
        >>name_index['fred']
        ['fred', 42]

        Dictionaries are one of the most useful things in Python. Make sure you know
        how to take adavantage of them...

        Jeremy

        --
        Jeremy Sanders

        Comment

        • Travis Oliphant

          #5
          Re: Database in memory

          Jim wrote:
          I have an application that will maintain an in-memory database in the
          form of a list of lists. Does anyone know of a way to search for and
          retreive "records" from such a structure?
          >
          Actually, the new NumPy can work as a very-good fast and efficient
          simple in-memory database (or memory-mapped data-base for that matter).

          The elements of a NumPy array can be arbitrary records. You would
          search using logical combinations of comparision. I think the ability
          for NumPy (which now handles arbitrary records) to be used as a
          data-base is under-appreciated.

          Mind you, it is SQL-less. NumPy only provides the "tables" it does not
          provide the fancy logic on-top of the tables. So, perhaps it would be
          better to say that NumPy could serve as the foundation for a simple
          data-base application.

          -Travis

          Comment

          • Hendrik van Rooyen

            #6
            Re: Database in memory

            "Jeremy Sanders" ...emy+comp...p ython@jer...rs. net wrote:
            Dictionaries are one of the most useful things in Python. Make sure you know
            how to take adavantage of them...
            +1 for QOTW

            - Hendrik

            Comment

            • Nicko

              #7
              Re: Database in memory

              Jim wrote:
              I have an application that will maintain an in-memory database in the
              form of a list of lists. Does anyone know of a way to search for and
              retreive "records" from such a structure?
              The answer very much depends on the manner in which you want to do the
              look-up. If you only need to do exact-match look-up for items with
              unique keys (e.g. find the single record where SSN=1234567890) then
              using a dictionary is by far the best solution. It's fast and it's
              easy.

              If you expect to do exact-match look-up where the keys are not unique
              then build a dictionary containing 'set' objects which are the sets of
              records which have the given key. This lets you neatly find the
              intersection of selections on multiple criteria (e.g. matches =
              zipcode_index["94101"] & hometype_index["condo"] ).

              If you need to do range matching (e.g. 20000 <= salary < 50000) then
              your best bet is to keep a list of the records sorted in the ordering
              of the key, do a binary search to find where the lower and upper
              bounds lie within the sorted list and then take a slice. If you also
              have some index dictionaries containing sets then you can combine
              these two methods with something like 'matches =
              set(salary_inde x[lo_sal:hi_sal]) & zipcode_index["81435"] '

              Having said all that, if you think that there is any possibility that
              you might ever want to expand the functionality of your program to
              require either (a) more complex and flexible searching and/or (b)
              putting the database somewhere else, then I would strongly suggest
              that you use PySQLite. SQLite is an efficient in-memory database with
              an SQL engine and the Python interface conforms to the DB-API spec, so
              you won't need to change your code (much) if you want to move the
              database to some MySQL, Oracle, Sybase or DB2 server at a later date.
              Furthermore SQLite is included in Python 2.5 as standard.

              Comment

              • Nicko

                #8
                Re: Database in memory

                On Apr 10, 1:10 pm, "Nicko" <use...@nicko.o rgwrote:
                If you expect to do exact-match look-up where the keys are not unique
                then build a dictionary containing 'set' objects which are the sets of
                records which have the given key. This lets you neatly find the
                intersection of selections on multiple criteria (e.g. matches =
                zipcode_index["94101"] & hometype_index["condo"] ).
                Just FYI, if you're going to go this route then the items that you are
                indexing have to be hashable, which the built in 'list' type is not.
                Tuples are, or you can make some custom class (or your own subtype of
                list) which implements the __hash__ method based on some 'primary key'
                value from your data. Or you could just go for SQLite...

                Comment

                Working...