Python Memory NoSQL Database

Source: Internet
Author: User

Python Memory NoSQL Database


From the network, after modification, adhering to the spirit of open source, feedback network!

#!/usr/bin/python #-*-coding:utf-8-*-# # memdb.py # python Memory db # # 2015-12 ################################### ##################################### # The MIT License (MIT) # Http://opensource.org/licenses/MIT # # Copyright (c) 2 015 Copyright Cheungmine # # Permission is hereby granted, free of charge, to all person obtaining # A copy of this SOFTW Is and associated documentation files (the # "software"), to deal in the software without restriction, including # with Out limitation the rights to use, copy, modify, merge, publish, # Distribute, sublicense, and/or sell copies of the SOFTW Was, and to # Permit persons to whom the software was furnished to doing so, subject # to the following conditions: # # The Above copyright notice and this permission notice shall is # included in all copies or substantial portions of the Softwa
Re.  # # The software is provided ' as is ', without WARRANTY of any KIND, # EXPRESS OR implied, including and not LIMITED to the Warranties of # MERChantability, FITNESS for A particular PURPOSE and noninfringement. # in NO EVENT shall the AUTHORS or COPYRIGHT holders is liable for any # CLAIM, damages OR other liability, WHETHER in an ACTION of contract, # TORT or OTHERWISE, arising from, out of or in CONNECTION with the # Software or the use or other DEA
Lings in the software. ######################################################################## from multiprocessing import RLock # Sync for
        Threading Class Objectsync:def __init__ (self, name): Self.refcount = 0 Self.synclock = Rlock ()


    Self.keyname = name def Lock (self): Self.synclock.acquire () Self.refcount = Self.refcount + 1
    def Unlock (self): Self.refcount = Self.refcount-1 self.synclock.release () class Objectsyncfactory: def __init__ (self): Self.globallock = Objectsync ("") Self.rowlocks = {} def __removelock (self, S EM, KeyName): Self.globalLock.Lock () Self. rowlocks[keyname] = None self.globalLock.Unlock () def getlock (self, tablename, key): KeyName = tabl Ename + "," + str (key) Self.globalLock.Lock () L = None try:l = self.rowlocks[keyname ] if L = = None:self.rowlocks[keyname] = Objectsync (keyname) L = self.rowlocks

        [KeyName] except:self.rowlocks[keyname] = Objectsync (keyname) L = self.rowlocks[keyname] Self.globalLock.Unlock () return L class Pairguard:def __init__ (self, Factory, SEM): self. 
        Syncfactory = Factory Self.host = Sem self.host.Lock () def __del__ (self): Self.host.Unlock () if Self.host.refcount = = 0:self.syncfactory._objectsyncfactory__removelock (Self.host, Self.host.key Name) ######################################## # Database Table class Memtable:def __init__ (self): Self.row s = {} self.tablElock = Objectsync ("") def GetRowCount (self): return Len (self.rows) def DeleteAll (self): self. Tablelock.lock () self.rows = {} self.tableLock.Unlock () def __deleteall (self): Self.rows = {
    } def getallvalue (self): return self.rows # Throw Keyerror If key is not found. def GetValue (self, key): Return Self.rows[key] # no Exist:add # exist:update def addvalue (self, k EY, value): Self.tableLock.Lock () Self.rows[key] = value Self.tableLock.Unlock () def __addva Lue (self, Key, value): Self.rows[key] = value def delvalue (self, key): self. AddValue (Key,none) def __delvalue (self, Key): Self._memtable__addvalue (Key, None) ######################## ################ # Memdb class Memdb:def __init__ (self): Self.tables = {} self.syncfactory = Objectsy Ncfactory () # is not a thread Safed def createtable (Self, tabLename): Self.tables[tablename] = memtable () # is no thread Safed def droptable (self, tablename): Self.tables[tablename] = None def GetValue (self, tablename, key): MT = Self.tables[tablename] Pai Rguard (Self.syncfactory, Self.syncFactory.GetLock (tablename, key)) return Mt. GetValue (Key) def addvalue (self, tablename, key, value): MT = Self.tables[tablename] Pairguard (self. Syncfactory, Self.syncFactory.GetLock (tablename, key)) Mt. AddValue (key, value) def delvalue (self, tablename, key): MT = Self.tables[tablename] Pairguard (self. Syncfactory, Self.syncFactory.GetLock (tablename, key)) Mt. Delvalue (Key) def __getvalue (self, tablename, key): MT = Self.tables[tablename] return Mt. GetValue (Key) def __addvalue (self, tablename, key, value): MT = Self.tables[tablename] Mt._memtable_ _addvalue (key, value) def __delvalue (self, tablename, KEY): MT = Self.tables[tablename] Mt._memtable__delvalue (Key) class Transaction:def __init__ (self, C

        Onn): Self.dbconn = conn Self.logs = [] def Commit (self): syncs = [] tables = {} For P in self.logs:tables[p[0] "= True for name in tables:synctable = Self.dbconn 

        . Memdb.syncFactory.GetLock (Name, ' table ') Syncs.append ((Synctable.keyname, synctable)) Syncs.sort () #lock guards = [] for the sync in Syncs:guards.append (Pairguard (Self.dbconn.memdb.syncF
                Actory, sync[1])) #commit Self.logs.reverse () while True:if len (self.logs) = = 0:

        Break P = Self.logs.pop () self.dbconn.memdb._memdb__addvalue (p[0], p[1], p[2])
            #unlock Guards.reverse () while True:if len (guards) = = 0:break Guards.pop () sElf.dbconn._memdbconnect__endtransaction () def Rollback (self): Self.dbconn._memdbconnect__endtransaction ()
    def logpoint (self, tablename, key, value): Self.logs.append ((tablename, key, value)) class Memdbconnect: def __init__ (self, db): Self.memdb = db Self.btransaction = False Self.trans = None def BES


    Gintransaction (self): self.btransaction = True Self.trans = Transaction (self) return Self.trans def __endtransaction (self): self.btransaction = False Self.trans = None def committransaction (SE LF): If self.bTransaction:self.bTransaction = False ts = Self.trans Self.tran s = None if ts:ts.
            Commit () def rollbacktransaction (self): if self.bTransaction:self.bTransaction = False ts = Self.trans Self.trans = None if ts:ts.
Rollback ()

    # not thread safe def createtable (self, tablename): Self.memdb.CreateTable (tablename) # not thread Safe def droptable (self, tablename): Self.memdb.DropTable (tablename) # not thread safe def hastable ( Self, tablename): If Self.memdb.tables.get (tablename): Return True Else:return Fa LSE DEF GetValue (self, tablename, key): if Self.bTransaction:return self.memdb._memdb__getvalue (tablename, key) Else:return Self.memdb.GetValue (tablename, key) def addvalue (self, tablename,
            Key, value): If Self.bTransaction:self.trans.LogPoint (tablename, key, value) Else:
            Self.memdb.AddValue (tablename, key, value) def delvalue (self, tablename, key): if Self.btransaction: Self.trans.LogPoint (tablename, key, None) Else:self.memdb.DelValue (tablename, key) def Q Uerytablesnothrow (self):
        tables = [] try:self.

            BeginTransaction () for tablename,_ in Self.memdb.tables.items (): Tables.append (tablename) Self. CommitTransaction () Except:tables = [] self.
        RollbackTransaction () Finally:return tables def querytablekeysnothrow (self, tablename): Keys = [] try:self. BeginTransaction () if self. HasTable (tablename): Rows_dict = Self.memdb.tables[tablename].rows for key, _ in Rows_dict . Items (): Keys.append (key) self. CommitTransaction () Except:keys = [] self. 
            RollbackTransaction () Finally:return keys def createtablenothrow (self, tablename): try: Self. BeginTransaction () if not self.
               HasTable (tablename): Self.memdb.CreateTable (tablename) Self. CommitTransaction () except:self.
            RollbackTransaction () Finally:pass def droptablenothrow (self, tablename): try: Self. BeginTransaction () if self. HasTable (tablename): Self.memdb.DropTable (tablename) self. CommitTransaction () except:self.
        RollbackTransaction () Finally:pass def getvaluenothrow (self, tablename, key, DefaultValue): result = DefaultValue try:self. BeginTransaction () result = self. GetValue (tablename, key) self. CommitTransaction () except:self.
        RollbackTransaction () Finally:return result def addvaluenothrow (self, tablename, key, value): result = False try:self. BeginTransaction () self. AddValue (tablename, key, value) self. CommitTransaction () result = TruE except:self.
        RollbackTransaction () Finally:return result def delvaluenothrow (self, tablename, key): result = False try:self. BeginTransaction () self. Delvalue (tablename, key) self. CommitTransaction () result = True except:self. RollbackTransaction () Finally:return result def appendvaluelistnothrow (self, tablename, key, VA Lue, Non_repeated_value): Try:self. BeginTransaction () if self. HasTable (tablename): Try:values = self.
                            GetValue (tablename, key) if non_repeated_value:if value not in values: Values.append (value) self.
                        AddValue (tablename, key, values) Else:values.append (value) Self. AddValue (tabLename, key, values) except Keyerror:self. AddValue (tablename, key, [value]) finally:self. CommitTransaction () except:self. RollbackTransaction () Finally:pass def appendvaluelistmultinothrow (self, tablenames, keys, Valu ES, non_repeated_values): Try:self.  BeginTransaction () for I in range (0, Len (tablenames)): T, K, V, NRV = Tablenames[i], keys[i], Values[i], non_repeated_values[i] if self. HasTable (t): Try:vals = self.
                                GetValue (t, K) if Nrv:if v not in Vals: Vals.append (v) self.
                            AddValue (t, K, Vals) Else:vals.append (v) Self. AddValue (t, K, Vals) excePT Keyerror:self. AddValue (t, K, [v]) self.committransaction () except:self.
 RollbackTransaction () Finally:pass

Usage 1:

Can be embedded directly into Python:

def test ():
    db = Memdb ()
    tname = "table1"
    db. CreateTable (tname)

    #for i in range (100000):
    #    DB. AddValue (Tname,i, "SDFSD")
    db. AddValue (tname,11, "SDFSD")
    print db. GetValue (Tname, one)

    db. AddValue (tname,11, "dddddd")
    print db. GetValue (tname,11)

    db. AddValue (tname,12, "DSFDSFD")
    print db. GetValue (tname,12)

    conn = memdbconnect (db)
    t = conn. BeginTransaction () for
    I in range (100000):
        Conn. AddValue (Tname,i, "SDFSD")
    Conn. AddValue (tname,12, "Sfdas")
    Conn. AddValue (tname,12, "ddddd")
    t.commit ()
    print db. GetValue (tname,12)


Python Memory NoSQL Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.