<commit_before>"""
"""

class Cache:

    def __init__(self):
        pass

    def lock(self, layer, coord, format):
        pass

    def unlock(self, layer, coord, format):
        pass

    def read(self, layer, coord, format):<commit_msg>Building up lock/unlock methods with SQLite calls<commit_after>"""
"""

from sqlite3 import connect, OperationalError, IntegrityError

class Cache:

    def __init__(self):
        self.dbpath = '/tmp/stache.db'

        db = connect(self.dbpath).cursor()

        try:
            db.execute("""
                CREATE TABLE locks (
                    row     INTEGER,
                    column  INTEGER,
                    zoom    INTEGER,
                    format  TEXT,
                    
                    PRIMARY KEY (row, column, zoom, format)
                )
                """)
        except OperationalError:
            pass

    def lock(self, layer, coord, format):
        """ Acquire a cache lock for this tile.
        
            Returns nothing, but (TODO) blocks until the lock has been acquired.
            Lock is implemented as a row in the "locks" table.
        """
        db = connect(self.dbpath, isolation_level='EXCLUSIVE').cursor()
        
        try:
            db.execute("""INSERT INTO locks
                          (row, column, zoom, format) VALUES (?, ?, ?, ?)""",
                       (coord.row, coord.column, coord.zoom, format))
        except IntegrityError:
            # yikes, locked. deal with this later.
            pass
        else:
            db.connection.commit()

        db.close()

    def unlock(self, layer, coord, format):
        """ Release a cache lock for this tile.

            Lock is implemented as a row in the "locks" table.
        """
        db = connect(self.dbpath, isolation_level='EXCLUSIVE').cursor()
        db.execute("""DELETE FROM locks
                      WHERE row=? AND column=? AND zoom=? AND format=?""",
                   (coord.row, coord.column, coord.zoom, format))
        db.connection.commit()
        db.close()

    def read(self, layer, coord, format):
{'fraction_non_alphanumeric': 0.10680529300567108, 'fraction_numerical': 0.0004725897920604915, 'mean_word_length': 1.5660606060606062, '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': '5311587', 'n_tokens_mistral': 582, 'n_tokens_neox': 543, 'n_words': 173}