Code Monkey home page Code Monkey logo

ihashmap's Introduction

iHashmap

Lint and Test

Smart Hashmap logo

Wrapper for key-value based storage systems. Provides convenient way to organize data for quick searching.

Installation

1. Using pip: pip install ihashmap

2. Building from source: make install

How to use

Firstly you need to register methods:

from ihashmap.cache import Cache

Cache.register_get_method(YOUR_GET_METHOD)
Cache.register_set_method(YOUR_SET_METHOD)
Cache.register_update_method(YOUR_UPDATE_METHOD)
Cache.register_delete_method(YOUR_DELETE_METHOD)

NOTE: Methods signature MUST match their placeholders signature

GET_METHOD = lambda cache, name, key, default=None: None  # noqa: E731
SET_METHOD = lambda cache, name, key, value: None  # noqa: E731
UPDATE_METHOD = lambda cache, name, key, value: None  # noqa: E731
DELETE_METHOD = lambda cache, name, key: None  # noqa: E731
"""METHODS placeholders. You should register yours."""

Now you are all set up to use Cache.search

How it works

In default setup Cache creates and maintains indexes based on Cache.primary_key.

So every object save in cache MUST have such key. (By default its _id)

On every called action for example Cache.update Cache looks in pipeline Cache.PIPELINE.update for middlewares to run before and after main function execution. For example in current situation after .update function execution indexing middleware will check if documents fields matching its keys were changed. If so it will get index data, look for old values in value.__shadow_copy__ remove such index data and create new record with updated values.

Adding middlewares

Adding new action is easy:

from ihashmap.cache import Cache, PipelineContext

@Cache.PIPELINE.set.before()
def add_my_field(ctx: PipelineContext):

    key, value = ctx.args
    value["my_field"] = 1

Now every cache value saved with Cache.set will be added 'my_field' before main function execution.

Custom Indexes

To create custom index you need to simply create new subclass of Index.

from ihashmap.index import Index

class IndexByModel(Index):
    keys = ["_id", "model"]

NOTE: After that all values MUST have fields _id AND model

NOTE: Primary key MUST ALWAYS be in keys

Searching

After all required indexes created - searching will be as quick as possible.

from ihashmap.cache import Cache
from ihashmap.index import Index

class IndexByModel(Index):
    keys = ["_id", "model"]

cache = Cache()
cache.search("my_cache", {"model": "1.0"})

When .search is called it will firstly check for indexes containing search fields. After finding best index, it will get index data and find matching primary keys. Now searching is as easy as getting values by their key.

ihashmap's People

Contributors

yurzs avatar sharpeye90 avatar akulaev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.