# Path: biggraphite/accessor.py
# class Error(Exception):
# class RetryableError(Error):
# class InvalidArgumentError(Error):
# class Accessor(object):
# class PointGrouper(object):
# SHARD_REPLICA_MASK = 0xC000
# SHARD_WRITER_MASK = SHARD_REPLICA_MASK ^ 0xFFFF
# SHARD_REPLICA_BITS = bin(SHARD_REPLICA_MASK).count("1")
# SHARD_WRITER_BITS = 16 - SHARD_REPLICA_BITS
# SHARD_REPLICA_SHIFT = SHARD_WRITER_BITS
# SHARD_MAX_REPLICAS = 2 ** SHARD_REPLICA_BITS
# def pack_shard(replica, writer):
# def unpack_shard(shard):
# def _wait_async_call(async_function, *args, **kwargs):
#     def on_done(exception):
#     def __init__(self, backend_name):
#     def __enter__(self):
#     def __exit__(self, _type, _value, _traceback):
#     def set_cache(self, cache, data_ttl=60, metadata_ttl=60):
#     def connect(self):
#     def syncdb(self, retentions=None, dry_run=False):
#     def create_metric(self, metric):
#     def update_metric(self, name, updated_metadata):
#     def delete_metric(self, name):
#     def delete_directory(self, name):
#     def _check_connected(self):
#     def drop_all_metrics(self):
#     def fetch_points(self, metric, time_start, time_end, stage, aggregated=True):
#     def has_metric(self, metric_name):
#     def get_metric(self, metric_name, strict_checks=False):
#     def glob_metric_names(self, glob, start_time=None, end_time=None):
#     def glob_metrics(self, glob, start_time=None, end_time=None):
#     def glob_directory_names(self, glob, start_time=None, end_time=None):
#     def background(self):
#     def flush(self):
#     def insert_points(self, metric, datapoints):
#     def insert_points_async(self, metric, datapoints, on_done=None):
#     def insert_downsampled_points(self, metric, datapoints):
#     def insert_downsampled_points_async(self, metric, datapoints, on_done=None):
#     def map(
#         self,
#         callback,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         errback=None,
#         callback_on_progress=None,
#     ):
#     def repair(
#         self,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         callback_on_progress=None,
#     ):
#     def clean(
#         self,
#         max_age=None,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         callback_on_progress=None,
#         disable_clean_directories=False,
#         disable_clean_metrics=False,
#     ):
#     def shutdown(self):
#     def touch_metric(self, metric):
#     def __init__(
#         self,
#         metric,
#         time_start_ms,
#         time_end_ms,
#         stage,
#         query_results,
#         source_stage=None,
#         aggregated=True,
#     ):
#     def __iter__(self):
#     def run_aggregator(self):
#     def generate_values_aggregated(self):
#     def generate_values_stage0(self):
# 
# Path: biggraphite/drivers/_downsampling.py
# class Downsampler(object):
# class MetricAggregates(object):
#     CAPACITY = 20
#     PURGE_EVERY_S = 3600
#     DEFAULT_STAGE0_CAPACITY = 10
#     def __init__(self, capacity=CAPACITY):
#     def feed(self, metric, points):
#     def clear(self):
#     def purge(self, now=time.time()):
#     def __init__(self, metric_metadata, stage0_capacity=DEFAULT_STAGE0_CAPACITY):
#     def _get_expired_stage0_points(self, points):
#     def _get_non_expired_stage0_points(self):
#     def _update_stage0(self, points):
#     def _merge(self, stage, result, points):
#     def _update_stage(self, stage_index, expired_points, non_expired_points):
#     def update(self, points):
#     def _stage0(self):
#     def _retention(self):
#     def obsolete(self, now):
#     def _stage0_timestamp(self):
#     def _stage0_timestamp(self, value):
#     def _get_stage_timestamp(self, stage_index):
#     def _set_stage_timestamp(self, stage_index, timestamp):
#     def _get_stage_value(self, stage_index):
#     def _set_stage_value(self, stage_index, value):
#     def _get_stage_count(self, stage_index):
#     def _set_stage_count(self, stage_index, count):


import collections
import logging
import sortedcontainers
from biggraphite import accessor as bg_accessor
from biggraphite import glob_utils as bg_glob
from biggraphite.drivers import _delayed_writer
from biggraphite.drivers import _downsampling
log = logging.getLogger(__name__)

OPTIONS = {}


class Error(bg_accessor.Error):
    """Base class for all exceptions from this module."""


class InvalidArgumentError(Error, bg_accessor.InvalidArgumentError):
    """Callee did not follow requirements on the arguments."""


class _MemoryAccessor(bg_accessor.Accessor):
    """A memory acessor that doubles as a memory MetadataCache."""

    TYPE = "memory"

    Row = collections.namedtuple(
        "Row", ["time_start_ms", "offset", "shard", "value", "count"]
    )

    Row0 = collections.namedtuple("Row", ["time_start_ms", "offset", "value"])

    def __init__(self):
        """Create a new MemoryAccessor."""
        super(_MemoryAccessor, self).__init__("memory")
        self._metric_to_points = collections.defaultdict(sortedcontainers.SortedDict)
        self._name_to_metric = {}
        self._directory_names = sortedcontainers.SortedSet()

# Path: biggraphite/accessor.py
# class Error(Exception):
# class RetryableError(Error):
# class InvalidArgumentError(Error):
# class Accessor(object):
# class PointGrouper(object):
# SHARD_REPLICA_MASK = 0xC000
# SHARD_WRITER_MASK = SHARD_REPLICA_MASK ^ 0xFFFF
# SHARD_REPLICA_BITS = bin(SHARD_REPLICA_MASK).count("1")
# SHARD_WRITER_BITS = 16 - SHARD_REPLICA_BITS
# SHARD_REPLICA_SHIFT = SHARD_WRITER_BITS
# SHARD_MAX_REPLICAS = 2 ** SHARD_REPLICA_BITS
# def pack_shard(replica, writer):
# def unpack_shard(shard):
# def _wait_async_call(async_function, *args, **kwargs):
#     def on_done(exception):
#     def __init__(self, backend_name):
#     def __enter__(self):
#     def __exit__(self, _type, _value, _traceback):
#     def set_cache(self, cache, data_ttl=60, metadata_ttl=60):
#     def connect(self):
#     def syncdb(self, retentions=None, dry_run=False):
#     def create_metric(self, metric):
#     def update_metric(self, name, updated_metadata):
#     def delete_metric(self, name):
#     def delete_directory(self, name):
#     def _check_connected(self):
#     def drop_all_metrics(self):
#     def fetch_points(self, metric, time_start, time_end, stage, aggregated=True):
#     def has_metric(self, metric_name):
#     def get_metric(self, metric_name, strict_checks=False):
#     def glob_metric_names(self, glob, start_time=None, end_time=None):
#     def glob_metrics(self, glob, start_time=None, end_time=None):
#     def glob_directory_names(self, glob, start_time=None, end_time=None):
#     def background(self):
#     def flush(self):
#     def insert_points(self, metric, datapoints):
#     def insert_points_async(self, metric, datapoints, on_done=None):
#     def insert_downsampled_points(self, metric, datapoints):
#     def insert_downsampled_points_async(self, metric, datapoints, on_done=None):
#     def map(
#         self,
#         callback,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         errback=None,
#         callback_on_progress=None,
#     ):
#     def repair(
#         self,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         callback_on_progress=None,
#     ):
#     def clean(
#         self,
#         max_age=None,
#         start_key=None,
#         end_key=None,
#         shard=0,
#         nshards=1,
#         callback_on_progress=None,
#         disable_clean_directories=False,
#         disable_clean_metrics=False,
#     ):
#     def shutdown(self):
#     def touch_metric(self, metric):
#     def __init__(
#         self,
#         metric,
#         time_start_ms,
#         time_end_ms,
#         stage,
#         query_results,
#         source_stage=None,
#         aggregated=True,
#     ):
#     def __iter__(self):
#     def run_aggregator(self):
#     def generate_values_aggregated(self):
#     def generate_values_stage0(self):
# 
# Path: biggraphite/drivers/_downsampling.py
# class Downsampler(object):
# class MetricAggregates(object):
#     CAPACITY = 20
#     PURGE_EVERY_S = 3600
#     DEFAULT_STAGE0_CAPACITY = 10
#     def __init__(self, capacity=CAPACITY):
#     def feed(self, metric, points):
#     def clear(self):
#     def purge(self, now=time.time()):
#     def __init__(self, metric_metadata, stage0_capacity=DEFAULT_STAGE0_CAPACITY):
#     def _get_expired_stage0_points(self, points):
#     def _get_non_expired_stage0_points(self):
#     def _update_stage0(self, points):
#     def _merge(self, stage, result, points):
#     def _update_stage(self, stage_index, expired_points, non_expired_points):
#     def update(self, points):
#     def _stage0(self):
#     def _retention(self):
#     def obsolete(self, now):
#     def _stage0_timestamp(self):
#     def _stage0_timestamp(self, value):
#     def _get_stage_timestamp(self, stage_index):
#     def _set_stage_timestamp(self, stage_index, timestamp):
#     def _get_stage_value(self, stage_index):
#     def _set_stage_value(self, stage_index, value):
#     def _get_stage_count(self, stage_index):
#     def _set_stage_count(self, stage_index, count):


# Path: biggraphite/drivers/memory.py
import collections
import logging
import sortedcontainers
from biggraphite import accessor as bg_accessor
from biggraphite import glob_utils as bg_glob
from biggraphite.drivers import _delayed_writer
from biggraphite.drivers import _downsampling

log = logging.getLogger(__name__)

OPTIONS = {}


class Error(bg_accessor.Error):
    """Base class for all exceptions from this module."""


class InvalidArgumentError(Error, bg_accessor.InvalidArgumentError):
    """Callee did not follow requirements on the arguments."""


class _MemoryAccessor(bg_accessor.Accessor):
    """A memory acessor that doubles as a memory MetadataCache."""

    TYPE = "memory"

    Row = collections.namedtuple(
        "Row", ["time_start_ms", "offset", "shard", "value", "count"]
    )

    Row0 = collections.namedtuple("Row", ["time_start_ms", "offset", "value"])

    def __init__(self):
        """Create a new MemoryAccessor."""
        super(_MemoryAccessor, self).__init__("memory")
        self._metric_to_points = collections.defaultdict(sortedcontainers.SortedDict)
        self._name_to_metric = {}
        self._directory_names = sortedcontainers.SortedSet()

        self.__downsampler = _downsampling.Downsampler()