Skip to main content

A multivalued BTree.

Project description

Multivalued BTree

A multivalued BTree that allows to store several values in the same node. This is based on Python BTrees 4.10 module.

How to use

It is very easy to use. You only have a MultivaluedBTree() class that has two optional parameters reverse and queue_type to define how the elements are extracted with pop(), popitem() or get() methods. This class is thought to implement an ordered queue in a BTree structure that can have several values for each key. Therefore, the assignment operator does not replace the previous value but add a new one when the tree has already that key. Therefore, if you do:

from multivaluedbtree import MultivaluedBTree

tree = MultivaluedBTree()
tree['a'] = 1  # Set 1 in the key 'a'
tree['a'] = 2 # Added 2 to the key 'a'

The tree will have the list [1, 2] for the key 'a' and its length will be 2. The keys are ordered in increase order, but this order can be changed with reverse parameter. By default, the values in the same key are stored with LIFO (Last In, First Out), but this order can be changed with the queue_type parameter.

Example of use without parameters (keys in incremental order and values in LIFO queues):

from multivaluedbtree import MultivaluedBTree

tree = MultivaluedBTree()
tree['a'] = 1  # Set 1 in the key 'a'
r1 = tree['a']  # Gets [1]
tree['a'] = 2  # Add 2 to the key 'a'
r2 = tree['a']  # Gets [1, 2]
len(tree)  # Returns 2 although it only has 1 key
tree['b'] = 3  # Set 3 to the key 'b'
len(tree)  # Returns 3 although it only has 2 key
tree['c'] = 4  # Sets 3 to the key 'c'
# Adds 5 and 6 to the key 'd'
tree['d'] = 5
tree['d'] = 6
tree.values()  # Returns [1, 2, 3]
tree.values('b', 'c')  # Returns [3, 4]
tree.values('c', 'd')  # Returns [4, 5, 6]
r3 = tree.popitem()  # Returns ('a', 2) and remove the item 2 from 'a'
r4 = tree['a']  # Returns [1]
r5 = tree.pop('a')  # Returns 1
r6 = tree.pop('a')  # Raises an KeyError because the key 'a' has no values.
r7 = tree.pop('a', 10)  # Returns 10 because is the default value if the key 'a' does not exist.

Example with FIFO queues as values:

from multivaluedbtree import MultivaluedBTree, QueueType

tree = MultivaluedBTree(queue_type=QueueType.FIFO)
tree['a'] = 1
tree['a'] = 2
tree['b'] = 3
r1 = tree['a']  # Returns [2, 1]
len(tree)  # Returns 3
r2 = tree.values()  # Returns [2, 1, 3]
tree['c'] = 4
tree['d'] = 5
tree['d'] = 6
r3 = tree.values('b', 'c')  # Returns [3, 4]
r4 = tree.values('c', 'd'), # Returns [4, 6, 5]
r5 = tree.popitem()  # Returns ('a', 1))
r6 = tree['a']  # Returns [2]
r7 = tree.pop('a')  # Returns 2
tree.pop('a')  # Raises an KeyError because the key 'a' has no values.
tree.pop('a', 10)  # Returns 10 because is the default value if the key 'a' does not exist.

Example in decremental key order and LIFO queues:

from multivaluedbtree import MultivaluedBTree

tree = MultivaluedBTree(reverse=True)
tree['a'] = 1
tree['a'] = 2
tree['b'] = 3
tree['c'] = 4
tree['d'] = 5
tree['d'] = 6
r1 = tree['a']  # Returns [1, 2]
r2 = tree['b']  # Returns [3]
len(tree)  # Returns 6
r3 = tree.values()  # Returns [6, 5, 4, 3, 2, 1]
r4 = tree.values('b', 'c')  # Returns [4, 3]
r5 = tree.values('c', 'd')  # Returns [6, 5, 4]

Example in decremental key order and LIFO queues:

from multivaluedbtree import MultivaluedBTree, QueueType

tree = MultivaluedBTree(reverse=True, queue_type=QueueType.FIFO)
tree['a'] = 1
tree['a'] = 2
tree['b'] = 3
tree['c'] = 4
tree['d'] = 5
tree['d'] = 6
r1 = tree['a']  # Returns [2, 1]
r2 = tree['b']  # Returns [3]
len(tree)  # Returns 6
r3 = tree.values()  # Returns [5, 6, 4, 3, 1, 2]
r4 = tree.values('b', 'c')  # Returns [4, 3]
r5 = tree.values('c', 'd')  # Returns [5, 6, 4]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multivaluedbtree-0.0.1.tar.gz (17.1 kB view details)

Uploaded Source

File details

Details for the file multivaluedbtree-0.0.1.tar.gz.

File metadata

  • Download URL: multivaluedbtree-0.0.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

File hashes

Hashes for multivaluedbtree-0.0.1.tar.gz
Algorithm Hash digest
SHA256 35537f85fe8bb2b24f2622ac899b36fabcb3cbe61b24174b76e15affd7deb3ad
MD5 45ec4618c0d62bd5f24a6bb6c39cee11
BLAKE2b-256 9d5ede64290e993b5676c27d73378e6fc7e440930823999b4f3aead7c9527416

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page