question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Proposal: Multiple simultaneous dictionaries with one db file

See original GitHub issue

Hello,

I use sqlitedict quite extensively in my research code, and overall find it a wonderful piece of software. Recently however, I discovered an issue that I had to create an interesting workaround for, and was thinking that this might be useful to others as well, if made into an official feature.

Use case: I often use sqlitedict to store results from experiments (time series or just simple attributes). My structure might look like this:

A single dbfile with:

  • Attribute table for each experiment, with attribute keys underneath
  • Timeseries table for each experiment/timeseries attribute, with timestamp keys inside.
  • Metadata table to list experiments, and some mappings to attributes.

This works great with a small number of experiments, but when this grows to thousands, it is hard to work with the data because it is necessary to instantiate a sqlitedict for each dbfile/table combo.

Now, for my proposal:

I realized that it is possible to just switch out the .tablename attribute for an already open sqlitedict, and have it work just fine (the connection is already open, etc just need to change the table for the queries). This cut a recent processing time from ~5000s to 250s.

It would be cool to have an alternate interface that works like this:

db = sqlitedict.SqliteDictDb(dbfilename)
dictionary_foo = db.get_dict("foo")
dictionary_bar = db.get_dict("bar")

The dictionary interfaces themselves could be a lightweight wrapper that maintains a reference to the db object. These could share the same thread, connection, etc. and allow for much faster access to a variety of tables.

If this seems reasonable, I am more than happy to implement it and submit a pull request. If not, thats ok too, I’ll just keep it in my personal library. I just figured I would ask in case there is interest, and if there is any preference for how it is done.

Thank you! -Bill Katsak

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Digeniscommented, Jul 13, 2016
class defaultdict(collections.defaultdict):
    'like defaultdict but default_factory receives the key'

    def __missing__(self, key):
        self[key] = value = self.default_factory(key)
        return value


dicts = defaultdict(lambda table: SqliteDict('dicts.sqlite3', table, 'c', True))
dicts['dict1']['x'] = 1
dicts['dict2']['y'] = 2
0reactions
jonchuncommented, Mar 10, 2020

@mpenkov one problem I see with that approach is that you would need to pickle the entire dictionary every time you change any Key/Value inside the nested dictionary. With @wkatsak’s approach, you should be able to change a key/value pair in dictionary_foo and only pickle the value you’re changing rather than pickling the entirety of dictionary_foo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to use multiple table in python sqlitedict in the same file?
In sqlitedict README on github, it says that one of the features is: Support for multiple tables (=dicts) living in the same database...
Read more >
The Design and Implementation of Modern Column-Oriented ...
In this article, we survey recent research on column-oriented database systems, or column-stores, where each attribute of a table is stored in.
Read more >
Fundamentals of Database Systems - Index of
The book is meant to be used as a textbook for a one- or two-semester course in database systems at the junior, senior,...
Read more >
Dictionaries in Python - Towards Data Science
A dictionary has two characteristic features: keys and values. Each key has a corresponding value. Both, the key and the value, can be...
Read more >
Python 3.11: Cool New Features for You to Try - Real Python
Several new typing features that improve Python's static typing support; Native TOML support for working with configuration files. If you want ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found