コード例 #1
0
ファイル: parser.py プロジェクト: accelazh/magnetodb
    def parse_batch_get_request_items(cls, request_items_json):
        request_list = []
        for table_name, request_body in request_items_json.iteritems():
            validation.validate_table_name(table_name)
            validation.validate_object(request_body, table_name)

            consistent = request_body.pop(Props.CONSISTENT_READ, False)

            validation.validate_boolean(consistent, Props.CONSISTENT_READ)

            attributes_to_get = request_body.pop(Props.ATTRIBUTES_TO_GET, None)

            if attributes_to_get is not None:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            keys = request_body.pop(Props.KEYS, None)

            validation.validate_list(keys, Props.KEYS)

            validation.validate_unexpected_props(request_body, table_name)

            for key in keys:
                key_attribute_map = cls.parse_item_attributes(key)
                request_list.append(
                    models.GetItemRequest(table_name,
                                          key_attribute_map,
                                          attributes_to_get,
                                          consistent=consistent))
        return request_list
コード例 #2
0
ファイル: get_item.py プロジェクト: purpen/magnetodb
    def process_request(self, req, body, project_id, table_name):
        utils.check_project_id(req.context, project_id)
        req.context.tenant = project_id

        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, parser.Props.ATTRIBUTES_TO_GET
                )
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)
                select_type = models.SelectType.specific_attributes(
                    attributes_to_get
                )
            else:
                select_type = models.SelectType.all()

            key = body.pop(parser.Props.KEY, None)
            validation.validate_object(key, parser.Props.KEY)

            # parse consistent_read
            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)

            validation.validate_unexpected_props(body, "body")

            # parse key_attributes
            key_attributes = parser.Parser.parse_item_attributes(key)

            # format conditions to get item
            indexed_condition_map = {
                name: [models.IndexedCondition.eq(value)]
                for name, value in key_attributes.iteritems()
            }

        # get item
        result = storage.select_item(
            req.context, table_name, indexed_condition_map,
            select_type=select_type, limit=2, consistent=consistent_read)

        # format response
        if result.count == 0:
            return {}

        response = {
            parser.Props.ITEM: parser.Parser.format_item_attributes(
                result.items[0])
        }
        return response
コード例 #3
0
ファイル: get_item.py プロジェクト: zaletniy/magnetodb
def get_item(req, project_id, table_name):
    """The Getitem operation returns an item with the given primary key. """

    with probe.Probe(__name__ + '.validate'):
        body = req.json_body
        validation.validate_object(body, "body")

        # get attributes_to_get
        attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
        if attributes_to_get:
            attributes_to_get = validation.validate_set(
                attributes_to_get, parser.Props.ATTRIBUTES_TO_GET
            )
            for attr_name in attributes_to_get:
                validation.validate_attr_name(attr_name)
            select_type = models.SelectType.specific_attributes(
                attributes_to_get
            )
        else:
            select_type = models.SelectType.all()

        key = body.pop(parser.Props.KEY, None)
        validation.validate_object(key, parser.Props.KEY)

        # parse consistent_read
        consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
        validation.validate_boolean(consistent_read,
                                    parser.Props.CONSISTENT_READ)

        validation.validate_unexpected_props(body, "body")

        # parse key_attributes
        key_attributes = parser.Parser.parse_item_attributes(key)

    # get item
    result = storage.get_item(
        project_id, table_name, key_attributes,
        select_type=select_type, consistent=consistent_read)

    # format response
    if result.count == 0:
        return {}

    response = {
        parser.Props.ITEM: parser.Parser.format_item_attributes(
            result.items[0])
    }
    return response
コード例 #4
0
ファイル: get_item.py プロジェクト: accelazh/magnetodb
    def process_request(self, req, body, project_id, table_name):
        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            # get attributes_to_get
            attributes_to_get = body.pop(parser.Props.ATTRIBUTES_TO_GET, None)
            if attributes_to_get:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, parser.Props.ATTRIBUTES_TO_GET)
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)
                select_type = models.SelectType.specific_attributes(
                    attributes_to_get)
            else:
                select_type = models.SelectType.all()

            key = body.pop(parser.Props.KEY, None)
            validation.validate_object(key, parser.Props.KEY)

            # parse consistent_read
            consistent_read = body.pop(parser.Props.CONSISTENT_READ, False)
            validation.validate_boolean(consistent_read,
                                        parser.Props.CONSISTENT_READ)

            validation.validate_unexpected_props(body, "body")

            # parse key_attributes
            key_attributes = parser.Parser.parse_item_attributes(key)

        # get item
        result = storage.get_item(req.context,
                                  table_name,
                                  key_attributes,
                                  select_type=select_type,
                                  consistent=consistent_read)

        # format response
        if result.count == 0:
            return {}

        response = {
            parser.Props.ITEM:
            parser.Parser.format_item_attributes(result.items[0])
        }
        return response
コード例 #5
0
ファイル: parser.py プロジェクト: accelazh/magnetodb
    def parse_batch_get_request_items(cls, request_items_json):
        request_list = []
        for table_name, request_body in request_items_json.iteritems():
            validation.validate_table_name(table_name)
            validation.validate_object(request_body, table_name)

            consistent = request_body.pop(Props.CONSISTENT_READ, False)

            validation.validate_boolean(consistent, Props.CONSISTENT_READ)

            attributes_to_get = request_body.pop(
                Props.ATTRIBUTES_TO_GET, None
            )

            if attributes_to_get is not None:
                attributes_to_get = validation.validate_set(
                    attributes_to_get, Props.ATTRIBUTES_TO_GET
                )
                for attr_name in attributes_to_get:
                    validation.validate_attr_name(attr_name)

            keys = request_body.pop(Props.KEYS, None)

            validation.validate_list(keys, Props.KEYS)

            validation.validate_unexpected_props(request_body, table_name)

            for key in keys:
                key_attribute_map = cls.parse_item_attributes(key)
                request_list.append(
                    models.GetItemRequest(
                        table_name, key_attribute_map, attributes_to_get,
                        consistent=consistent
                    )
                )
        return request_list
コード例 #6
0
ファイル: create_table.py プロジェクト: accelazh/magnetodb
    def create_table(self, req, body, project_id):
        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            table_name = body.pop(parser.Props.TABLE_NAME, None)
            validation.validate_table_name(table_name)

            # parse table attributes
            attribute_definitions_json = body.pop(
                parser.Props.ATTRIBUTE_DEFINITIONS, None
            )
            validation.validate_list_of_objects(
                attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS
            )

            attribute_definitions = parser.Parser.parse_attribute_definitions(
                attribute_definitions_json
            )

            # parse table key schema
            key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None)
            validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA)

            key_attrs = parser.Parser.parse_key_schema(key_attrs_json)

            # parse table indexed field list
            lsi_defs_json = body.pop(
                parser.Props.LOCAL_SECONDARY_INDEXES, None
            )

            if lsi_defs_json:
                validation.validate_list_of_objects(
                    lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES
                )

                index_def_map = parser.Parser.parse_local_secondary_indexes(
                    lsi_defs_json
                )
            else:
                index_def_map = {}

            # validate the uniqueness of table and its indices' key schema
            range_keys = []
            if len(key_attrs) > 1:
                range_keys.append(key_attrs[1])
            else:
                # table has hash type primary key
                if len(index_def_map) > 0:
                    raise exception.ValidationError(
                        _("Table without range key in primary key schema "
                          "can not have indices"))
            for index in index_def_map.values():
                range_keys.append(index.alt_range_key_attr)
            try:
                validation.validate_set(range_keys, "key_schema")
            except exception.ValidationError:
                raise exception.ValidationError(
                    _("Table and its indices must have unique key schema"))

            validation.validate_unexpected_props(body, "body")
        # prepare table_schema structure
        table_schema = models.TableSchema(
            attribute_definitions, key_attrs, index_def_map)

        table_meta = storage.create_table(
            req.context, table_name, table_schema)

        url = req.path_url + "/" + table_name
        bookmark = req.path_url + "/" + table_name

        result = {
            parser.Props.TABLE_DESCRIPTION: {
                parser.Props.ATTRIBUTE_DEFINITIONS: (
                    parser.Parser.format_attribute_definitions(
                        table_meta.schema.attribute_type_map
                    )
                ),
                parser.Props.CREATION_DATE_TIME: table_meta.creation_date_time,
                parser.Props.ITEM_COUNT: 0,
                parser.Props.KEY_SCHEMA: (
                    parser.Parser.format_key_schema(
                        table_meta.schema.key_attributes
                    )
                ),
                parser.Props.TABLE_ID: str(table_meta.id),
                parser.Props.TABLE_NAME: table_name,
                parser.Props.TABLE_STATUS: (
                    parser.Parser.format_table_status(table_meta.status)
                ),
                parser.Props.TABLE_SIZE_BYTES: 0,
                parser.Props.LINKS: [
                    {
                        parser.Props.HREF: url,
                        parser.Props.REL: parser.Values.SELF
                    },
                    {
                        parser.Props.HREF: bookmark,
                        parser.Props.REL: parser.Values.BOOKMARK
                    }
                ]
            }
        }

        if table_meta.schema.index_def_map:
            table_def = result[parser.Props.TABLE_DESCRIPTION]
            table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = (
                parser.Parser.format_local_secondary_indexes(
                    table_meta.schema.key_attributes[0],
                    table_meta.schema.index_def_map
                )
            )

        return result
コード例 #7
0
ファイル: create_table.py プロジェクト: accelazh/magnetodb
    def create_table(self, req, body, project_id):
        with probe.Probe(__name__ + '.validate'):
            validation.validate_object(body, "body")

            table_name = body.pop(parser.Props.TABLE_NAME, None)
            validation.validate_table_name(table_name)

            # parse table attributes
            attribute_definitions_json = body.pop(
                parser.Props.ATTRIBUTE_DEFINITIONS, None)
            validation.validate_list_of_objects(
                attribute_definitions_json, parser.Props.ATTRIBUTE_DEFINITIONS)

            attribute_definitions = parser.Parser.parse_attribute_definitions(
                attribute_definitions_json)

            # parse table key schema
            key_attrs_json = body.pop(parser.Props.KEY_SCHEMA, None)
            validation.validate_list(key_attrs_json, parser.Props.KEY_SCHEMA)

            key_attrs = parser.Parser.parse_key_schema(key_attrs_json)

            # parse table indexed field list
            lsi_defs_json = body.pop(parser.Props.LOCAL_SECONDARY_INDEXES,
                                     None)

            if lsi_defs_json:
                validation.validate_list_of_objects(
                    lsi_defs_json, parser.Props.LOCAL_SECONDARY_INDEXES)

                index_def_map = parser.Parser.parse_local_secondary_indexes(
                    lsi_defs_json)
            else:
                index_def_map = {}

            # validate the uniqueness of table and its indices' key schema
            range_keys = []
            if len(key_attrs) > 1:
                range_keys.append(key_attrs[1])
            else:
                # table has hash type primary key
                if len(index_def_map) > 0:
                    raise exception.ValidationError(
                        _("Table without range key in primary key schema "
                          "can not have indices"))
            for index in index_def_map.values():
                range_keys.append(index.alt_range_key_attr)
            try:
                validation.validate_set(range_keys, "key_schema")
            except exception.ValidationError:
                raise exception.ValidationError(
                    _("Table and its indices must have unique key schema"))

            validation.validate_unexpected_props(body, "body")
        # prepare table_schema structure
        table_schema = models.TableSchema(attribute_definitions, key_attrs,
                                          index_def_map)

        table_meta = storage.create_table(req.context, table_name,
                                          table_schema)

        url = req.path_url + "/" + table_name
        bookmark = req.path_url + "/" + table_name

        result = {
            parser.Props.TABLE_DESCRIPTION: {
                parser.Props.ATTRIBUTE_DEFINITIONS:
                (parser.Parser.format_attribute_definitions(
                    table_meta.schema.attribute_type_map)),
                parser.Props.CREATION_DATE_TIME:
                table_meta.creation_date_time,
                parser.Props.ITEM_COUNT:
                0,
                parser.Props.KEY_SCHEMA: (parser.Parser.format_key_schema(
                    table_meta.schema.key_attributes)),
                parser.Props.TABLE_ID:
                str(table_meta.id),
                parser.Props.TABLE_NAME:
                table_name,
                parser.Props.TABLE_STATUS:
                (parser.Parser.format_table_status(table_meta.status)),
                parser.Props.TABLE_SIZE_BYTES:
                0,
                parser.Props.LINKS: [{
                    parser.Props.HREF: url,
                    parser.Props.REL: parser.Values.SELF
                }, {
                    parser.Props.HREF: bookmark,
                    parser.Props.REL: parser.Values.BOOKMARK
                }]
            }
        }

        if table_meta.schema.index_def_map:
            table_def = result[parser.Props.TABLE_DESCRIPTION]
            table_def[parser.Props.LOCAL_SECONDARY_INDEXES] = (
                parser.Parser.format_local_secondary_indexes(
                    table_meta.schema.key_attributes[0],
                    table_meta.schema.index_def_map))

        return result