# Path: debian/elevator/usr/share/pyshared/elevator/db.py
# class DatabaseOptions(dict):
#     def __init__(self, *args, **kwargs):
#         self['create_if_missing'] = True
#         self['error_if_exists'] = False
#         self['paranoid_checks'] = False
#         self['block_cache_size'] = 8 * (2 << 20)
#         self['write_buffer_size'] = 2 * (2 << 20)
#         self['block_size'] = 4096
#         self['max_open_files'] = 1000
# 
#         for key, value in kwargs.iteritems():
#             if key in self:
#                 self[key] = value
# 
# Path: debian/elevator/usr/share/pyshared/elevator/constants.py
# KEY_ERROR = 1
# 
# TYPE_ERROR = 0
# 
# DATABASE_ERROR = 6
# 
# VALUE_ERROR = 2
# 
# RUNTIME_ERROR = 4
# 
# SIGNAL_ERROR = 7
# 
# SUCCESS_STATUS = 1
# 
# FAILURE_STATUS = -1
# 
# WARNING_STATUS = -2
# 
# SIGNAL_BATCH_PUT = 1
# 
# SIGNAL_BATCH_DELETE = 0


import leveldb
import logging
from .db import DatabaseOptions
from .message import ResponseContent, ResponseHeader
from .constants import KEY_ERROR, TYPE_ERROR, DATABASE_ERROR,\
                       VALUE_ERROR, RUNTIME_ERROR, SIGNAL_ERROR,\
                       SUCCESS_STATUS, FAILURE_STATUS, WARNING_STATUS,\
                       SIGNAL_BATCH_PUT, SIGNAL_BATCH_DELETE
from .utils.patterns import destructurate
from .helpers.internals import failure, success
                    [VALUE_ERROR, "Batch only accepts sequences (list, tuples,...)"])
        except TypeError:
            return (FAILURE_STATUS,
                    [TYPE_ERROR, "Invalid type supplied"])
        db.Write(batch)

        return success()

    def DBConnect(self, *args, **kwargs):
        db_name = args[0]

        if (not db_name or
            not self.databases.exists(db_name)):
            error_msg = "Database %s doesn't exist" % db_name
            errors_logger.error(error_msg)
            return failure(DATABASE_ERROR, error_msg)

        db_uid = self.databases.index['name_to_uid'][db_name]
        if self.databases[db_uid]['status'] == self.databases.STATUSES.UNMOUNTED:
            self.databases.mount(db_name)

        return success(db_uid)

    def DBMount(self, db_name, *args, **kwargs):
        return self.databases.mount(db_name)

    def DBUmount(self, db_name, *args, **kwargs):
        return self.databases.umount(db_name)

    def DBCreate(self, db, db_name, db_options=None, *args, **kwargs):

# Path: debian/elevator/usr/share/pyshared/elevator/db.py
# class DatabaseOptions(dict):
#     def __init__(self, *args, **kwargs):
#         self['create_if_missing'] = True
#         self['error_if_exists'] = False
#         self['paranoid_checks'] = False
#         self['block_cache_size'] = 8 * (2 << 20)
#         self['write_buffer_size'] = 2 * (2 << 20)
#         self['block_size'] = 4096
#         self['max_open_files'] = 1000
# 
#         for key, value in kwargs.iteritems():
#             if key in self:
#                 self[key] = value
# 
# Path: debian/elevator/usr/share/pyshared/elevator/constants.py
# KEY_ERROR = 1
# 
# TYPE_ERROR = 0
# 
# DATABASE_ERROR = 6
# 
# VALUE_ERROR = 2
# 
# RUNTIME_ERROR = 4
# 
# SIGNAL_ERROR = 7
# 
# SUCCESS_STATUS = 1
# 
# FAILURE_STATUS = -1
# 
# WARNING_STATUS = -2
# 
# SIGNAL_BATCH_PUT = 1
# 
# SIGNAL_BATCH_DELETE = 0


# Path: debian/elevator/usr/share/pyshared/elevator/api.py
import leveldb
import logging
from .db import DatabaseOptions
from .message import ResponseContent, ResponseHeader
from .constants import KEY_ERROR, TYPE_ERROR, DATABASE_ERROR,\
                       VALUE_ERROR, RUNTIME_ERROR, SIGNAL_ERROR,\
                       SUCCESS_STATUS, FAILURE_STATUS, WARNING_STATUS,\
                       SIGNAL_BATCH_PUT, SIGNAL_BATCH_DELETE
from .utils.patterns import destructurate
from .helpers.internals import failure, success

                    [VALUE_ERROR, "Batch only accepts sequences (list, tuples,...)"])
        except TypeError:
            return (FAILURE_STATUS,
                    [TYPE_ERROR, "Invalid type supplied"])
        db.Write(batch)

        return success()

    def DBConnect(self, *args, **kwargs):
        db_name = args[0]

        if (not db_name or
            not self.databases.exists(db_name)):
            error_msg = "Database %s doesn't exist" % db_name
            errors_logger.error(error_msg)
            return failure(DATABASE_ERROR, error_msg)

        db_uid = self.databases.index['name_to_uid'][db_name]
        if self.databases[db_uid]['status'] == self.databases.STATUSES.UNMOUNTED:
            self.databases.mount(db_name)

        return success(db_uid)

    def DBMount(self, db_name, *args, **kwargs):
        return self.databases.mount(db_name)

    def DBUmount(self, db_name, *args, **kwargs):
        return self.databases.umount(db_name)

    def DBCreate(self, db, db_name, db_options=None, *args, **kwargs):

        db_options = DatabaseOptions(**db_options) if db_options else DatabaseOptions()