Example #1
0
def test_versioned_item_metadata_dict():
    item = VersionedItem(symbol="test",
                         library="test_lib",
                         data=None,
                         version=1.2,
                         metadata=None)
    assert(item.metadata_dict() == {'symbol': 'test', 'library': 'test_lib', 'version': 1.2})
Example #2
0
def test_versioned_item_metadata_dict():
    item = VersionedItem(symbol="test",
                         library="test_lib",
                         data=None,
                         version=1.2,
                         metadata=None,
                         host=None)
    assert(item.metadata_dict() == {'symbol': 'test', 'library': 'test_lib', 'version': 1.2})
Example #3
0
def test_versioned_item_default_host():
    item = VersionedItem(symbol="sym",
                         library="ONEMINUTE",
                         data=[1, 2, 3],
                         version=1.0,
                         metadata={'metadata': 'foo'})

    expected_item = VersionedItem(symbol="sym",
                                  library="ONEMINUTE",
                                  data=[1, 2, 3],
                                  version=1.0,
                                  host=None,
                                  metadata={'metadata': 'foo'})

    assert item == expected_item
Example #4
0
    def read_metadata(self, symbol, as_of=None, allow_secondary=None):
        """
        Return the metadata saved for a symbol.  This method is fast as it doesn't
        actually load the data.

        Parameters
        ----------
        symbol : `str`
            symbol name for the item
        as_of : `str` or int or `datetime.datetime`
            Return the data as it was as_of the point in time.
            `int` : specific version number
            `str` : snapshot name which contains the version
            `datetime.datetime` : the version of the data that existed as_of the requested point in time
        allow_secondary : `bool` or `None`
            Override the default behavior for allowing reads from secondary members of a cluster:
            `None` : use the settings from the top-level `Arctic` object used to query this version store.
            `True` : allow reads from secondary members
            `False` : only allow reads from primary members
        """
        _version = self._backing_store.read_version(self.library_name, symbol,
                                                    as_of)
        return VersionedItem(symbol=symbol,
                             library=self.library_name,
                             version=_version['version'],
                             metadata=_version.pop('metadata', None),
                             data=None)
Example #5
0
    def write(self,
              symbol,
              data,
              metadata=None,
              prune_previous_version=True,
              **kwargs):
        """
        Write 'data' under the specified 'symbol' name to this library.

        Parameters
        ----------
        symbol : `str`
            symbol name for the item
        data :
            to be persisted
        metadata : `dict`
            an optional dictionary of metadata to persist along with the symbol.
            Default: None
        prune_previous_version : `bool`
            Removes previous (non-snapshotted) versions from the database.
            Default: True
        kwargs :
            passed through to the write handler

        Returns
        -------
        VersionedItem named tuple containing the metadata and version number
        of the written symbol in the store.
        """
        _id = bson.ObjectId()
        version = {
            '_id': _id,
            'symbol': symbol,
            'metadata': metadata,
            'version': _id
        }

        previous_version = self._backing_store.read_version(
            self.library_name, symbol)

        handler = self._write_handler(version, symbol, data, **kwargs)
        handler.write(self._backing_store, self.library_name, version, symbol,
                      data, previous_version, **kwargs)

        #if prune_previous_version and previous_version:
        #     self._prune_previous_versions(symbol)

        # self._publish_change(symbol, version)

        # Insert the new version into the version DB
        self._insert_version(version)

        logger.debug('Finished writing versions for %s', symbol)

        return VersionedItem(symbol=symbol,
                             library=self.library_name,
                             version=version['version'],
                             metadata=version.pop('metadata', None),
                             data=None)
Example #6
0
def test_versioned_item_str_handles_none():
    item = VersionedItem(symbol=None,
                         library=None,
                         data=None,
                         version=None,
                         metadata=None)

    assert str(item)
Example #7
0
def test_versioned_item_str():
    item = VersionedItem(symbol="sym",
                         library="ONEMINUTE",
                         data=pd.DataFrame(),
                         version=1.0,
                         metadata={'metadata': 'foo'})

    expected = "VersionedItem(symbol=sym,library=ONEMINUTE," + \
               "data=<class 'pandas.core.frame.DataFrame'>,version=1.0,metadata={'metadata': 'foo'}"
    assert str(item) == expected
    assert repr(item) == expected
Example #8
0
 def _do_read(self, symbol, version, from_version=None, **kwargs):
     if version.get('deleted'):
         raise NoDataFoundException("No data found for %s in library %s" %
                                    (symbol, self._arctic_lib.get_name()))
     handler = self._read_handler(version, symbol)
     data = handler.read(self._backing_store,
                         self.library_name,
                         version,
                         symbol,
                         from_version=from_version,
                         **kwargs)
     return VersionedItem(symbol=symbol,
                          library=self.library_name,
                          version=version['version'],
                          metadata=version.pop('metadata', None),
                          data=data)