<commit_before>import os
import leveldb
import threading
import compress
from pyethereum.slogging import get_logger
log = get_logger('db')

databases = {}


class DB(object):

    def __init__(self, dbfile):
        self.dbfile = os.path.abspath(dbfile)
        if dbfile in databases:
            self.db = databases[dbfile]
        else:
            self.db = leveldb.LevelDB(dbfile)
            databases[dbfile] = self.db
        self.uncommitted = dict()
        self.lock = threading.Lock()

    def get(self, key):
        if key in self.uncommitted:
            if self.uncommitted[key] is None:
                raise Exception("key not in db")
            return self.uncommitted[key]
        o = compress.decompress(self.db.Get(key))
        self.uncommitted[key] = o
        return o

    def put(self, key, value):
        with self.lock:
            self.uncommitted[key] = value

    def commit(self):
        log.debug('commit', db=self)
        with self.lock:
            batch = leveldb.WriteBatch()
            for k, v in self.uncommitted.iteritems():
                if v is None:
                    batch.Delete(k)
                else:
                    batch.Put(k, compress.compress(v))
            self.db.Write(batch, sync=False)
            self.uncommitted.clear()
<commit_msg>Fixed minor DB bug<commit_after>import os
import leveldb
import threading
import compress
from pyethereum.slogging import get_logger
log = get_logger('db')

databases = {}


class DB(object):

    def __init__(self, dbfile):
        self.dbfile = os.path.abspath(dbfile)
        if dbfile in databases:
            self.db = databases[dbfile]
        else:
            self.db = leveldb.LevelDB(dbfile)
            databases[dbfile] = self.db
        self.uncommitted = dict()
        self.lock = threading.Lock()

    def get(self, key):
        if key in self.uncommitted:
            if self.uncommitted[key] is None:
                raise KeyError("key not in db")
            return self.uncommitted[key]
        o = compress.decompress(self.db.Get(key))
        self.uncommitted[key] = o
        return o

    def put(self, key, value):
        with self.lock:
            self.uncommitted[key] = value

    def commit(self):
        log.debug('commit', db=self)
        with self.lock:
            batch = leveldb.WriteBatch()
            for k, v in self.uncommitted.iteritems():
                if v is None:
                    batch.Delete(k)
                else:
                    batch.Put(k, compress.compress(v))
            self.db.Write(batch, sync=False)
            self.uncommitted.clear()

{'fraction_non_alphanumeric': 0.10144927536231885, 'fraction_numerical': 0.0, 'mean_word_length': 1.8083511777301928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4691962', 'n_tokens_mistral': 797, 'n_tokens_neox': 772, 'n_words': 189}