# Path: carbono/buffer_manager/reorder_buffer.py
# class ReoderBuffer(object):
#     def __init__(self, out_buffer, size=10):
#         self.out_buffer = out_buffer
#         self.size = size
#         self._array = Manager().list(range(self.size))
#         self._lock = Lock()
#         self._min = 0
#         self._max = self.size
#         self._count = 0
#         self.update_control()
# 
#     def update_control(self):
#         self._control = dict()
#         for e, i in enumerate(xrange(self._min, self._max)):
#             self._control.update({i: e})
# 
#     def put(self, block_num, data):
#         self._lock.acquire()
#         if block_num not in self._control.keys():
#             self._lock.release()
#             raise IndexError
#         self._array[self._control[block_num]] = data
#         self._count += 1
#         if self._count == self.size: self.sync()
#         self._lock.release()
# 
#     def sync(self):
#         for i in xrange(self._count):
#             data = self._array[i]
#             self.out_buffer.put(data)
#         self._min += self._count
#         self._max += self._count
#         self._count = 0
#         self.update_control()


import time
import Queue
from multiprocessing import Process, Manager, Event
from multiprocessing.managers import BaseManager
from threading import Thread
from copy import deepcopy
from carbono.buffer_manager.reorder_buffer import ReoderBuffer
from carbono.utils import *
from carbono.config import *
from carbono.exception import *
        try:
            self.terminate()
        except AttributeError:
            pass


class WorkManager(Thread):
    def __init__(self, read_callback, job_callback):
        Thread.__init__(self)
        self.read_block = read_callback
        self.job = job_callback

        self.manager = Manager()
        self.num_cores = available_processors()
        self._block_number = 0
        self._worker_list = list()
        self.active = False

        self._setup()
        self._start_workers()

    def _setup(self):
        free_phy_mem = available_memory(percent=35)
        maxsize = int(free_phy_mem / BLOCK_SIZE)
        self._input_buffer = self.manager.Queue(maxsize)
        self.output_buffer = self.manager.Queue(maxsize)

        BaseManager.register("ReoderBuffer", ReoderBuffer)
        bm = BaseManager()
        bm.start()

# Path: carbono/buffer_manager/reorder_buffer.py
# class ReoderBuffer(object):
#     def __init__(self, out_buffer, size=10):
#         self.out_buffer = out_buffer
#         self.size = size
#         self._array = Manager().list(range(self.size))
#         self._lock = Lock()
#         self._min = 0
#         self._max = self.size
#         self._count = 0
#         self.update_control()
# 
#     def update_control(self):
#         self._control = dict()
#         for e, i in enumerate(xrange(self._min, self._max)):
#             self._control.update({i: e})
# 
#     def put(self, block_num, data):
#         self._lock.acquire()
#         if block_num not in self._control.keys():
#             self._lock.release()
#             raise IndexError
#         self._array[self._control[block_num]] = data
#         self._count += 1
#         if self._count == self.size: self.sync()
#         self._lock.release()
# 
#     def sync(self):
#         for i in xrange(self._count):
#             data = self._array[i]
#             self.out_buffer.put(data)
#         self._min += self._count
#         self._max += self._count
#         self._count = 0
#         self.update_control()


# Path: carbono/buffer_manager/work_manager.py
import time
import Queue
from multiprocessing import Process, Manager, Event
from multiprocessing.managers import BaseManager
from threading import Thread
from copy import deepcopy
from carbono.buffer_manager.reorder_buffer import ReoderBuffer
from carbono.utils import *
from carbono.config import *
from carbono.exception import *

        try:
            self.terminate()
        except AttributeError:
            pass


class WorkManager(Thread):
    def __init__(self, read_callback, job_callback):
        Thread.__init__(self)
        self.read_block = read_callback
        self.job = job_callback

        self.manager = Manager()
        self.num_cores = available_processors()
        self._block_number = 0
        self._worker_list = list()
        self.active = False

        self._setup()
        self._start_workers()

    def _setup(self):
        free_phy_mem = available_memory(percent=35)
        maxsize = int(free_phy_mem / BLOCK_SIZE)
        self._input_buffer = self.manager.Queue(maxsize)
        self.output_buffer = self.manager.Queue(maxsize)

        BaseManager.register("ReoderBuffer", ReoderBuffer)
        bm = BaseManager()
        bm.start()

        self.reorder_buffer = bm.ReoderBuffer(self.output_buffer, 50)