def callee(session):
        session.create_table(
            os.path.join(path, 'series'),
            ydb.TableDescription()
            .with_primary_keys('series_id')
            .with_columns(
                ydb.Column('series_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)),
                ydb.Column('title', ydb.OptionalType(ydb.PrimitiveType.Utf8)),
                ydb.Column('info', ydb.OptionalType(ydb.PrimitiveType.Utf8)),
                ydb.Column('release_date', ydb.OptionalType(ydb.PrimitiveType.Datetime)),
                ydb.Column('views', ydb.OptionalType(ydb.PrimitiveType.Uint64)),
                ydb.Column('uploaded_user_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)),
            )
            .with_indexes(
                ydb.TableIndex('users_index').with_index_columns('uploaded_user_id')
            )
        )

        session.create_table(
            os.path.join(path, 'users'),
            ydb.TableDescription()
            .with_primary_keys('user_id')
            .with_columns(
                ydb.Column('user_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)),
                ydb.Column('name', ydb.OptionalType(ydb.PrimitiveType.Utf8)),
                ydb.Column('age', ydb.OptionalType(ydb.PrimitiveType.Uint32)),
            )
            .with_index(
                ydb.TableIndex('name_index').with_index_columns('name')
            )
        )
Пример #2
0
def bulk_upsert(table_client, path):
    print("\n> bulk upsert: episodes")
    column_types = ydb.BulkUpsertColumns() \
        .add_column('series_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)) \
        .add_column('season_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)) \
        .add_column('episode_id', ydb.OptionalType(ydb.PrimitiveType.Uint64)) \
        .add_column('title', ydb.OptionalType(ydb.PrimitiveType.Utf8)) \
        .add_column('air_date', ydb.OptionalType(ydb.PrimitiveType.Uint64))
    rows = basic_example_data.get_episodes_data_for_bulk_upsert()
    table_client.bulk_upsert(os.path.join(path, "episodes"), rows,
                             column_types)
Пример #3
0
def create_table(session, path):
    session.create_table(
        os.path.join(path, "schools"),
        ydb.TableDescription().with_column(
            ydb.Column("city",
                       ydb.OptionalType(ydb.PrimitiveType.Utf8))).with_column(
                           ydb.Column(
                               "number",
                               ydb.OptionalType(ydb.PrimitiveType.Uint32))).
        with_column(
            ydb.Column("address", ydb.OptionalType(
                ydb.PrimitiveType.Utf8))).with_primary_keys("city", "number"))
Пример #4
0
 def callee(session):
     # Documents table stores the contents of web pages.
     # The table is partitioned by hash(Url) in order to evenly distribute the load.
     print("Creating table %s" % os.path.join(path, "documents"))
     session.create_table(
         os.path.join(path, "documents"),
         ydb.TableDescription().with_primary_keys("doc_id").with_columns(
             ydb.Column("doc_id",
                        ydb.OptionalType(ydb.PrimitiveType.Uint64)),
             ydb.Column("url", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
             ydb.Column("html", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
             ydb.Column("timestamp",
                        ydb.OptionalType(ydb.PrimitiveType.Uint64)),
         ).with_profile(
             ydb.TableProfile()
             # Partition Documents table by DocId
             .with_partitioning_policy(
                 ydb.PartitioningPolicy().with_uniform_partitions(
                     DOC_TABLE_PARTITION_COUNT))))
Пример #5
0
def create_tables(table_client, path):
    session = table_client.session().create()
    # Documents table stores the contents of web pages.
    # The table is partitioned by hash(Url) in order to evenly distribute the load.
    session.create_table(
        os.path.join(path, "documents"),
        ydb.TableDescription().with_primary_keys("doc_id").with_columns(
            ydb.Column("doc_id", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
            ydb.Column("url", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
            ydb.Column("html", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
            ydb.Column("timestamp",
                       ydb.OptionalType(ydb.PrimitiveType.Uint64)),
        ).with_profile(ydb.TableProfile()
                       # Partition Documents table by DocId
                       .with_partitioning_policy(
                           ydb.PartitioningPolicy().with_uniform_partitions(
                               DOC_TABLE_PARTITION_COUNT))))

    # Multiple ExpirationQueue tables allow to scale the load.
    # Each ExpirationQueue table can be handled by a dedicated worker.
    for expiration_queue in range(EXPIRATION_QUEUE_COUNT):
        session.create_table(
            os.path.join(path, "expiration_queue_%d" % expiration_queue),
            ydb.TableDescription().with_primary_keys(
                "timestamp", "doc_id").with_columns(
                    ydb.Column("doc_id",
                               ydb.OptionalType(ydb.PrimitiveType.Uint64)),
                    ydb.Column("timestamp",
                               ydb.OptionalType(ydb.PrimitiveType.Uint64)),
                ))
def create_tables(session, path):
    # Creating Series table
    session.create_table(
        os.path.join(path, 'series'),
        ydb.TableDescription().with_column(
            ydb.Column('series_id', ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_column(
                    ydb.Column('title', ydb.OptionalType(
                        ydb.PrimitiveType.Utf8))).with_column(
                            ydb.Column(
                                'series_info',
                                ydb.OptionalType(ydb.PrimitiveType.Utf8))).
        with_column(
            ydb.Column(
                'release_date', ydb.OptionalType(
                    ydb.PrimitiveType.Uint64))).with_primary_key('series_id'))

    # Creating Seasons table
    session.create_table(
        os.path.join(path, 'seasons'),
        ydb.TableDescription().with_column(
            ydb.Column('series_id', ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_column(
                    ydb.Column('season_id',
                               ydb.OptionalType(
                                   ydb.PrimitiveType.Uint64))).with_column(
                                       ydb.Column(
                                           'title',
                                           ydb.OptionalType(
                                               ydb.PrimitiveType.Utf8))).
        with_column(
            ydb.Column('first_aired', ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_column(
                    ydb.Column(
                        'last_aired', ydb.OptionalType(
                            ydb.PrimitiveType.Uint64))).with_primary_keys(
                                'series_id', 'season_id'))

    # Creating Episodes table
    session.create_table(
        os.path.join(path, 'episodes'),
        ydb.TableDescription().with_column(
            ydb.Column('series_id', ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_column(
                    ydb.Column('season_id',
                               ydb.OptionalType(
                                   ydb.PrimitiveType.Uint64))).with_column(
                                       ydb.Column(
                                           'episode_id',
                                           ydb.OptionalType(
                                               ydb.PrimitiveType.Uint64))).
        with_column(
            ydb.Column('title', ydb.OptionalType(
                ydb.PrimitiveType.Utf8))).with_column(
                    ydb.Column(
                        'air_date', ydb.OptionalType(
                            ydb.PrimitiveType.Uint64))).with_primary_keys(
                                'series_id', 'season_id', 'episode_id'))
Пример #7
0
class SeriesRepository(object):
    _table_description = (
        ydb.TableDescription().with_column(
            ydb.Column("series_id", ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_column(
                    ydb.Column("title", ydb.OptionalType(
                        ydb.PrimitiveType.Utf8))).with_column(
                            ydb.Column(
                                "series_info",
                                ydb.OptionalType(
                                    ydb.PrimitiveType.Utf8))).with_column(
                                        ydb.Column(
                                            "release_date",
                                            ydb.OptionalType(
                                                ydb.PrimitiveType.Uint32))).
        with_column(
            ydb.Column("views", ydb.OptionalType(
                ydb.PrimitiveType.Uint64))).with_primary_keys("series_id"))
    _table_rev_index_description = (ydb.TableDescription().with_column(
        ydb.Column("rev_views", ydb.OptionalType(
            ydb.PrimitiveType.Uint64))).with_column(
                ydb.Column("series_id",
                           ydb.OptionalType(
                               ydb.PrimitiveType.Uint64))).with_primary_keys(
                                   "rev_views", "series_id"))
    _table_name = "series"
    _table_rev_views_name = "series_rev_views"

    def __init__(self, ydb_session):
        """
        :type ydb_session: ydb.YDBCachedSession
        """
        super(SeriesRepository, self).__init__()
        self._session = ydb_session

    def create_tables(self):
        self._session.session.create_table(
            os.path.join(self._session.table_prefix, self._table_name),
            self._table_description)
        self._session.session.create_table(
            os.path.join(self._session.table_prefix,
                         self._table_rev_views_name),
            self._table_rev_index_description)

    def drop_tables(self):
        self._session.session.drop_table(
            os.path.join(self._session.table_prefix, self._table_name))
        self._session.session.drop_table(
            os.path.join(self._session.table_prefix,
                         self._table_rev_views_name))

    def insert(self, series):
        """
        :type series: Series
        """
        self._session.session.transaction(ydb.SerializableReadWrite()).execute(
            self._session.queries[queries.INSERT_QUERY], {
                "$seriesId": series.series_id,
                "$title": series.title,
                "$seriesInfo": series.series_info,
                "$releaseDate": model.to_days(series.release_date),
                "$views": series.views,
            },
            commit_tx=True)

    def delete(self, series_id):
        """
        :type series_id: int
        """
        result_sets = self._session.session.transaction(
            ydb.SerializableReadWrite()).execute(
                self._session.queries[queries.DELETE_QUERY], {
                    "$seriesId": series_id,
                },
                commit_tx=True)
        if len(result_sets[0].rows) < 1:
            raise RuntimeError("Query count was not returned")
        return result_sets[0].rows[0].cnt

    def update_views(self, series_id, new_views):
        """
        :type series_id: int
        :type new_views: int
        """
        tx = self._session.session.transaction(
            ydb.SerializableReadWrite()).begin()
        result_sets = tx.execute(
            self._session.queries[queries.UPDATE_VIEWS_QUERY], {
                "$seriesId": series_id,
                "$newViews": new_views,
            },
            commit_tx=True)
        if len(result_sets[0].rows) < 1:
            raise RuntimeError("Query count was not returned")
        return result_sets[0].rows[0].cnt

    def find_by_id(self, series_id):
        """
        :type series_id: int
        :rtype: Series or None
        """
        tx = self._session.session.transaction(
            ydb.SerializableReadWrite()).begin()
        result_sets = tx.execute(
            self._session.queries[queries.FIND_BY_ID_QUERY], {
                "$seriesId": series_id,
            },
            commit_tx=True)
        if len(result_sets[0].rows) < 1:
            return None
        return self._extract_series(result_sets[0].rows[0])

    def find_all(self, limit, last_series_id=None):
        """
        :type limit: int
        :type last_series_id: int or None
        :rtype: list(Series)
        """
        tx = self._session.session.transaction(
            ydb.SerializableReadWrite()).begin()
        if last_series_id is None:
            result_sets = tx.execute(
                self._session.queries[queries.FIND_ALL_QUERY], {
                    "$limit": limit,
                },
                commit_tx=True)
        else:
            result_sets = tx.execute(
                self._session.queries[queries.FIND_ALL_NEXT_QUERY], {
                    "$limit": limit,
                    "$lastSeriesId": last_series_id
                },
                commit_tx=True)
        return [self._extract_series(row) for row in result_sets[0].rows]

    def find_most_viewed(self, limit, last_series_id=None, last_views=None):
        """
        :type limit: int
        :type last_series_id: int or None
        :type last_views: int or None
        :rtype: list(Series)
        """
        tx = self._session.session.transaction(
            ydb.SerializableReadWrite()).begin()
        if last_series_id is None or last_views is None:
            result_sets = tx.execute(
                self._session.queries[queries.FIND_MOST_VIEWED_QUERY], {
                    "$limit": limit,
                },
                commit_tx=True)
        else:
            result_sets = tx.execute(
                self._session.queries[queries.FIND_MOST_VIEWED_NEXT_QUERY], {
                    "$limit": limit,
                    "$lastSeriesId": last_series_id,
                    "$lastViews": last_views,
                },
                commit_tx=True)
        return [self._extract_series(row) for row in result_sets[0].rows]

    @staticmethod
    def _extract_series(row):
        """
        :rtype:  Series
        """
        series = model.Series()
        series.series_id = row.series_id
        series.title = row.title
        series.series_info = row.series_info
        series.release_date = model.from_days(row.release_date)
        series.views = row.views
        return series