# Path: pyoko/db/adapter/base.py
# class BaseAdapter(object):
#     """
#     QuerySet is a lazy data access layer for Riak.
#     """
# 
#     def __init__(self, **conf):
#         self._current_context = None
#         self._pre_compiled_query = ''
#         # pass permission checks to genareted model instances
#         self._pass_perm_checks = False
#         self._cfg = {'row_size': 1000,
#                      'rtype': ReturnType.Model,
#                      'start':0}
#         self._cfg.update(conf)
#         self._model = None
#         self.is_clone = False
# 
# Path: pyoko/fields.py
# DATE_FORMAT = "%Y-%m-%dT00:00:00Z"
# 
# DATE_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
# 
# Path: pyoko/db/connection.py
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
#         """
#         The body of the multi-get worker. Loops until
#         :meth:`_should_quit` returns ``True``, taking tasks off the
#         input queue, fetching the object, and putting them on the
#         output queue.
#         """
#         while not self._should_quit():
#             try:
#                 task = self._inq.get(block=True, timeout=0.25)
#             except TypeError:
#                 if self._should_quit():
#                     break
#                 else:
#                     raise
#             except Empty:
#                 continue
# 
#             try:
#                 # If data is found in cache this data is used.
#                 # Else, data is taken from riak and set to cache.
#                 if settings.ENABLE_CACHING:
#                     obj_data = cache.get(task.key)
#                     if obj_data:
#                         task.outq.put((task.key, json.loads(obj_data)))
#                         raise FoundInCache()
# 
#                 btype = task.client.bucket_type(task.bucket_type)
#                 obj = btype.bucket(task.bucket).get(task.key, **task.options)
# 
#                 if not obj.exists:
#                     raise NotFound()
# 
#                 if settings.ENABLE_CACHING:
#                     cache.set(task.key, json.dumps(obj.data), settings.CACHE_EXPIRE_DURATION)
# 
#                 task.outq.put((task.key, obj.data))
# 
#             except KeyboardInterrupt:
#                 raise
#             except FoundInCache or NotFound:
#                 pass
#             except Exception as err:
#                 errdata = (task.bucket_type, task.bucket, task.key, err)
#                 task.outq.put(errdata)
#             finally:
#                 self._inq.task_done()
# 
# Path: pyoko/lib/utils.py
# def ub_to_str(string):
#     """
#     converts py2 unicode / py3 bytestring into str
#     Args:
#         string (unicode, byte_string): string to be converted
#         
#     Returns:
#         (str)
#     """
#     if not isinstance(string, str):
#         if six.PY2:
#             return str(string)
#         else:
#             return string.decode()
#     return string
# 
# Path: pyoko/conf.py
# class Settings(object):
#     def __init__(self):
# 
# Path: pyoko/db/connection.py
# class FoundInCache(Exception):
# class NotFound(Exception):
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
# 
# Path: pyoko/exceptions.py
# class MultipleObjectsReturned(PyokoError):
#     """The query returned multiple objects when only one was expected."""
#     pass
# 
# class ObjectDoesNotExist(PyokoError):
#     pass
# 
# class PyokoError(Exception):
#     pass


from collections import defaultdict
from datetime import date, timedelta
from datetime import datetime
from riak.util import bytes_to_str
from pyoko.db.adapter.base import BaseAdapter
from pyoko.fields import DATE_FORMAT, DATE_TIME_FORMAT
from math import ceil
from pyoko.db.connection import PyokoMG
from pyoko.lib.utils import ub_to_str
    from urllib.request import urlopen
    from urllib2 import urlopen
from enum import Enum
from pyoko.conf import settings
from pyoko.db.connection import client, cache, log_bucket, version_bucket
from pyoko.exceptions import MultipleObjectsReturned, ObjectDoesNotExist, PyokoError
from collections import OrderedDict
import copy
import json
import time
import concurrent.futures as con
import six
import riak
import sys
        return model

    @staticmethod
    def set_to_cache(vk):
        """
        Args:
            vk (tuple): obj data (dict), obj key(str)

        Return:
            tuple: value (dict), key (string)
        """
        v, k = vk

        try:
            cache.set(k, json.dumps(v), settings.CACHE_EXPIRE_DURATION)
        except Exception as e:
            pass
            # todo should add log.error()
        return v, k

    @staticmethod
    def get_from_cache(key):
        """
        Args:
            key (str):
        Return:
            (dict): from json string
        """

        try:

# Path: pyoko/db/adapter/base.py
# class BaseAdapter(object):
#     """
#     QuerySet is a lazy data access layer for Riak.
#     """
# 
#     def __init__(self, **conf):
#         self._current_context = None
#         self._pre_compiled_query = ''
#         # pass permission checks to genareted model instances
#         self._pass_perm_checks = False
#         self._cfg = {'row_size': 1000,
#                      'rtype': ReturnType.Model,
#                      'start':0}
#         self._cfg.update(conf)
#         self._model = None
#         self.is_clone = False
# 
# Path: pyoko/fields.py
# DATE_FORMAT = "%Y-%m-%dT00:00:00Z"
# 
# DATE_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
# 
# Path: pyoko/db/connection.py
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
#         """
#         The body of the multi-get worker. Loops until
#         :meth:`_should_quit` returns ``True``, taking tasks off the
#         input queue, fetching the object, and putting them on the
#         output queue.
#         """
#         while not self._should_quit():
#             try:
#                 task = self._inq.get(block=True, timeout=0.25)
#             except TypeError:
#                 if self._should_quit():
#                     break
#                 else:
#                     raise
#             except Empty:
#                 continue
# 
#             try:
#                 # If data is found in cache this data is used.
#                 # Else, data is taken from riak and set to cache.
#                 if settings.ENABLE_CACHING:
#                     obj_data = cache.get(task.key)
#                     if obj_data:
#                         task.outq.put((task.key, json.loads(obj_data)))
#                         raise FoundInCache()
# 
#                 btype = task.client.bucket_type(task.bucket_type)
#                 obj = btype.bucket(task.bucket).get(task.key, **task.options)
# 
#                 if not obj.exists:
#                     raise NotFound()
# 
#                 if settings.ENABLE_CACHING:
#                     cache.set(task.key, json.dumps(obj.data), settings.CACHE_EXPIRE_DURATION)
# 
#                 task.outq.put((task.key, obj.data))
# 
#             except KeyboardInterrupt:
#                 raise
#             except FoundInCache or NotFound:
#                 pass
#             except Exception as err:
#                 errdata = (task.bucket_type, task.bucket, task.key, err)
#                 task.outq.put(errdata)
#             finally:
#                 self._inq.task_done()
# 
# Path: pyoko/lib/utils.py
# def ub_to_str(string):
#     """
#     converts py2 unicode / py3 bytestring into str
#     Args:
#         string (unicode, byte_string): string to be converted
#         
#     Returns:
#         (str)
#     """
#     if not isinstance(string, str):
#         if six.PY2:
#             return str(string)
#         else:
#             return string.decode()
#     return string
# 
# Path: pyoko/conf.py
# class Settings(object):
#     def __init__(self):
# 
# Path: pyoko/db/connection.py
# class FoundInCache(Exception):
# class NotFound(Exception):
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
# 
# Path: pyoko/exceptions.py
# class MultipleObjectsReturned(PyokoError):
#     """The query returned multiple objects when only one was expected."""
#     pass
# 
# class ObjectDoesNotExist(PyokoError):
#     pass
# 
# class PyokoError(Exception):
#     pass


# Path: pyoko/db/adapter/db_riak.py
from collections import defaultdict
from datetime import date, timedelta
from datetime import datetime
from riak.util import bytes_to_str
from pyoko.db.adapter.base import BaseAdapter
from pyoko.fields import DATE_FORMAT, DATE_TIME_FORMAT
from math import ceil
from pyoko.db.connection import PyokoMG
from pyoko.lib.utils import ub_to_str
    from urllib.request import urlopen
    from urllib2 import urlopen
from enum import Enum
from pyoko.conf import settings
from pyoko.db.connection import client, cache, log_bucket, version_bucket
from pyoko.exceptions import MultipleObjectsReturned, ObjectDoesNotExist, PyokoError
from collections import OrderedDict
import copy
import json
import time
import concurrent.futures as con
import six
import riak
import sys

        return model

    @staticmethod
    def set_to_cache(vk):
        """
        Args:
            vk (tuple): obj data (dict), obj key(str)

        Return:
            tuple: value (dict), key (string)
        """
        v, k = vk

        try:
            cache.set(k, json.dumps(v), settings.CACHE_EXPIRE_DURATION)
        except Exception as e:
            pass
            # todo should add log.error()
        return v, k

    @staticmethod
    def get_from_cache(key):
        """
        Args:
            key (str):
        Return:
            (dict): from json string
        """

        try:

            value = cache.get(key)