# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2008, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
#
# $
# $Id: selectable_object_pipe.py 2607 2008-04-21 23:17:11Z kurt $
"""
Contains the SelectableObjectPipe object.
Provides support for a SelectablePipe that handles opaque objects.
"""
from snaplogic.common.snapstream.selectable_pipe import SelectablePipe
from snaplogic.common.snapstream import memory
class SelectableObjectPipe(SelectablePipe):
MEMORY_ESTIMATE_COUNT = 2048
def __init__(self):
super(SelectableObjectPipe, self).__init__()
self._buffer = []
self._estimate_count = 0
self._estimated_size = 0
self._buffer_max_size = 1
def _available_write(self):
return len(self._buffer) < self._buffer_max_size
def _available_read(self):
return len(self._buffer) > 0
def _put(self, item):
self._buffer.append(item)
if self._estimate_count < self.MEMORY_ESTIMATE_COUNT:
self._estimate_count += 1
self._estimated_size += memory.estimate_size(item)
if self._estimated_size < memory.MAX_BUFFER_SIZE:
self._buffer_max_size += 1
def _get(self):
item = self._buffer.pop(0)
return item
|