def provide_batch(self):
        if self.done:
            return 0, None

        if not self.f:
            try:
                self.f = open(self.spec.replace(BSON_SCHEME, ""))
            except __HOLE__, e:
                return "error: could not open bson: %s; exception: %s" % \
                    (self.spec, e), None

        batch = pump.Batch(self)

        batch_max_size = self.opts.extra['batch_max_size']
        batch_max_bytes = self.opts.extra['batch_max_bytes']

        cmd = memcacheConstants.CMD_TAP_MUTATION
        vbucket_id = 0x0000ffff
        cas, exp, flg = 0, 0, 0

        while (self.f and
               batch.size() < batch_max_size and
               batch.bytes < batch_max_bytes):
            doc_size_buf = self.f.read(4)
            if not doc_size_buf:
                self.done = True
                self.f.close()
                self.f = None
                break
            doc_size, = struct.unpack("<i", doc_size_buf)
            doc_buf = self.f.read(doc_size - 4)
            if not doc_buf:
                self.done = True
                self.f.close()
                self.f = None
                break
            doc = bson._elements_to_dict(doc_buf, dict, True)
            key = doc['_id']
            doc_json = json.dumps(doc)
            msg = (cmd, vbucket_id, key, flg, exp, cas, '', doc_json)
            batch.append(msg, len(doc))

        if batch.size() <= 0:
            return 0, None
        return 0, batch
dataset/ETHPy150Open membase/membase-cli/pump_bson.py/BSONSource.provide_batch