spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davies <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-3074] [PySpark] support groupByKey() wi...
Date Thu, 09 Apr 2015 03:44:52 GMT
Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1977#discussion_r28033500
  
    --- Diff: python/pyspark/shuffle.py ---
    @@ -529,6 +522,322 @@ def sorted(self, iterator, key=None, reverse=False):
             return heapq.merge(chunks, key=key, reverse=reverse)
     
     
    +class ExternalList(object):
    +    """
    +    ExternalList can have many items which cannot be hold in memory in
    +    the same time.
    +
    +    >>> l = ExternalList(range(100))
    +    >>> len(l)
    +    100
    +    >>> l.append(10)
    +    >>> len(l)
    +    101
    +    >>> for i in range(10240):
    +    ...     l.append(i)
    +    >>> len(l)
    +    10341
    +    >>> import pickle
    +    >>> l2 = pickle.loads(pickle.dumps(l))
    +    >>> len(l2)
    +    10341
    +    >>> list(l2)[100]
    +    10
    +    """
    +    LIMIT = 10240
    +
    +    def __init__(self, values):
    +        self.values = values
    +        self.count = len(values)
    +        self._file = None
    +        self._ser = None
    +
    +    def __getstate__(self):
    +        if self._file is not None:
    +            self._file.flush()
    +            f = os.fdopen(os.dup(self._file.fileno()))
    +            f.seek(0)
    +            serialized = f.read()
    +        else:
    +            serialized = ''
    +        return self.values, self.count, serialized
    +
    +    def __setstate__(self, item):
    +        self.values, self.count, serialized = item
    +        if serialized:
    +            self._open_file()
    +            self._file.write(serialized)
    +        else:
    +            self._file = None
    +            self._ser = None
    +
    +    def __iter__(self):
    +        if self._file is not None:
    +            self._file.flush()
    +            # read all items from disks first
    +            with os.fdopen(os.dup(self._file.fileno()), 'r') as f:
    +                f.seek(0)
    +                for values in self._ser.load_stream(f):
    +                    for v in values:
    +                        yield v
    +
    +        for v in self.values:
    +            yield v
    +
    +    def __len__(self):
    +        return self.count
    +
    +    def append(self, value):
    +        self.values.append(value)
    +        self.count += 1
    +        # dump them into disk if the key is huge
    +        if len(self.values) >= self.LIMIT:
    +            self._spill()
    +
    +    def _open_file(self):
    +        dirs = _get_local_dirs("objects")
    +        d = dirs[id(self) % len(dirs)]
    +        if not os.path.exists(d):
    +            os.makedirs(d)
    +        p = os.path.join(d, str(id))
    +        self._file = open(p, "w+", 65536)
    +        self._ser = CompressedSerializer(PickleSerializer())
    +        os.unlink(p)
    +
    +    def _spill(self):
    +        """ dump the values into disk """
    +        global MemoryBytesSpilled, DiskBytesSpilled
    +        if self._file is None:
    +            self._open_file()
    +
    +        used_memory = get_used_memory()
    +        pos = self._file.tell()
    +        self._ser.dump_stream([self.values], self._file)
    +        self.values = []
    +        gc.collect()
    +        DiskBytesSpilled += self._file.tell() - pos
    +        MemoryBytesSpilled += (used_memory - get_used_memory()) << 20
    +
    +
    +class ExternalListOfList(ExternalList):
    +    """
    +    An external list for list.
    +    """
    +    def __init__(self, values):
    +        ExternalList.__init__(self, values)
    +        self.count = sum(len(i) for i in values)
    +
    +    def append(self, value):
    +        ExternalList.append(self, value)
    --- End diff --
    
    done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message