索引操作Handler的定义和说明

本文介绍了如何将数据库中的索引信息导入文件并构造全量索引。通过在mysql.constant包内定义枚举类型OpType,以及编写AdLevelDataHandler类实现数据与索引对象间的转换。全量索引的构建涉及多层级,包括独立的AdPlan和Creative索引,以及依赖于其他索引的AdUnit和CreativeUnit索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们之前实现了将数据库中的索引信息导入到文件中, 目的是在检索的过程中实现数据的加载,构造全量索引。 但是由于写入文件的数据格式和索引的数据格式不一样,于是我们要定义好索引的操作。但由于我们之前定义了索引的增删改查方法,于是我们只需要直接调用就可以实现从文件中获取信息构造全量索引。

1.在mysql.constant包内定义枚举类型OpType

package com.imooc.ad.mysql.constant;

public enum OpType {
    ADD,
    UPDATE,
    DELETE,
    OTHER;
}

2.编写AdLevelDataHandler类,实现写入到文件中的表数据和索引对象之间的转换

(1)索引之间存在层级的划分,也就是依赖关系的划分
(2)加载全量索引其实是增量索引“增加”的一种特殊实现

package com.imooc.ad.handler;

import com.imooc.ad.index.IndexAware;
import com.imooc.ad.mysql.constant.OpType;
import lombok.extern.slf4j.Slf4j;

//实现写入到文件中的表数据和索引对象之间的转换
//1.索引之间存在层级的划分,也就是依赖关系的划分
//2.加载全量索引其实是增量索引“增加”的一种特殊实现
@Slf4j
public class AdLevelDataHandler {

    private static <K,V> void handleBinlogEvent(
            IndexAware<K,V> index,
            K key,
            V value,
            OpType type){
        switch (type){
            case ADD:
                index.add(key,value);
                break;
            case UPDATE:
                index.update(key,value);
                break;
            case DELETE:
                index.delete(key,value);
                break;
            default:
                break;
        }

    }
}

3.构建第二层级索引,不依赖于其他索引,包括AdPlan和Creative两张数据表

//第二层级索引,不依赖于其他索引,包括AdPlan和Creative两张数据表
    public static void handleLevel2(AdPlanTable planTable,OpType opType){
        //把AdPlanTable转换成AdPlanObject
        AdPlanObject planObject = new AdPlanObject(
                planTable.getId(),
                planTable.getUserId(),
                planTable.getPlanStatus(),
                planTable.getStartDate(),
                planTable.getEndDate()
        );
        handleBinlogEvent(
                DataTable.of(AdPlanIndex.class),
                planObject.getPlanId(),
                planObject,
                opType
        );
    }

    public static void handleLevel2(AdCreativeTable creativeTable,OpType opType){
        //把AdCreativeTable转换成CreativeObject
        CreativeObject creativeObject = new CreativeObject(
                creativeTable.getAdId(),
                creativeTable.getName(),
                creativeTable.getType(),
                creativeTable.getMaterialType(),
                creativeTable.getHeight(),
                creativeTable.getWidth(),
                creativeTable.getAuditStatus(),
                creativeTable.getAdUrl()
        );
        handleBinlogEvent(
                DataTable.of(CreativeIndex.class),
                creativeObject.getAdId(),
                creativeObject,
                opType
        );
    }

4.第三层级索引,包括AdUnit和CreativeUnit两张表

//第三层级索引,包括AdUnit和CreativeUnit两张表
    public static void handleLevel3(AdUnitTable unitTable, OpType type) {

        //主要是这里需要加上一个判断,判断关联的AdPlan是否存在
        AdPlanObject adPlanObject = DataTable.of(
                AdPlanIndex.class
        ).get(unitTable.getPlanId());
        if (null == adPlanObject) {
            log.error("handleLevel3 found AdPlanObject error: {}",
                    unitTable.getPlanId());
            return;
        }

        AdUnitObject unitObject = new AdUnitObject(
                unitTable.getUnitId(),
                unitTable.getUnitStatus(),
                unitTable.getPositionType(),
                unitTable.getPlanId(),
                adPlanObject
        );

        handleBinlogEvent(
                DataTable.of(AdUnitIndex.class),
                unitTable.getUnitId(),
                unitObject,
                type
        );
    }

    public static void handlelevel3(AdCreativeUnitTable creativeUnitTable, OpType type){
        if(type == OpType.UPDATE){
            log.error("CreativeUnitIndex not support update");
            return;
        }

        AdUnitObject adUnitObject = DataTable.of(
                AdUnitIndex.class
        ).get(creativeUnitTable.getUnitId());
        CreativeObject creativeObject = DataTable.of(
                CreativeIndex.class
        ).get(creativeUnitTable.getAdId());

        if (null == adUnitObject || null == creativeObject) {
            log.error("AdCreativeUnitTable index error: {}",
                    JSON.toJSONString(creativeUnitTable));
            return;
        }

        CreativeUnitObject creativeUnitObject = new CreativeUnitObject(
                creativeUnitTable.getAdId(),
                creativeUnitTable.getUnitId()

        );

        handleBinlogEvent(
                DataTable.of(CreativeUnitIndex.class),
                Commonutils.stringConcat(
                        creativeUnitObject.getAdId().toString(),
                        creativeUnitObject.getUnitId().toString()
                ),
                creativeUnitObject,
                type
        );
    }

5.第四层级的索引,都与第三层级的索引之间有联系

public static void handleLevel4(AdUnitDistrictTable adUnitDistrictTable,OpType opType){
        if(opType == OpType.UPDATE){
            log.error("UnitDistrictIndex not support update");
            return;
        }
        //与unit相关联
        AdUnitObject adUnitObject = DataTable.of(AdUnitIndex.class).get(adUnitDistrictTable.getUnitId());
        if(adUnitObject == null){
            log.error("AdUnitDistrictTable index error:{}",adUnitDistrictTable.getUnitId());
            return;
        }

        String key = Commonutils.stringConcat(adUnitDistrictTable.getProvince(),adUnitDistrictTable.getCity());
        Set<Long> value = new HashSet<>(
                Collections.singleton(adUnitDistrictTable.getUnitId())
        );
        UnitDistrictObject unitDistrictObject = new UnitDistrictObject(adUnitDistrictTable.getUnitId(), adUnitDistrictTable.getProvince(), adUnitDistrictTable.getCity());

        handleBinlogEvent(
                DataTable.of(UnitDistrictIndex.class),
                key,
                value,
                opType
        );
    }

    public static void handleLevel4(AdUnitItTable adUnitItTable,OpType opType){
        if(opType == OpType.UPDATE){
            log.error("UnitItIndex not support update");
            return;
        }
        //与unit相关联
        AdUnitObject adUnitObject = DataTable.of(AdUnitIndex.class).get(adUnitItTable.getUnitId());
        if(adUnitObject == null){
            log.error("AdUnitItTableindex error:{}",adUnitItTable.getUnitId());
            return;
        }


        Set<Long> value = new HashSet<>(
                Collections.singleton(adUnitItTable.getUnitId())
        );
        UnitItObject unitItObject = new UnitItObject(adUnitItTable.getUnitId(), adUnitItTable.getItTag());

        handleBinlogEvent(
                DataTable.of(UnitDistrictIndex.class),
                adUnitItTable.getItTag(),
                value,
                opType
        );
    }

    public static void handleLevel4(AdUnitKeywordTable keywordTable,
                                    OpType type) {

        if (type == OpType.UPDATE) {
            log.error("keyword index can not support update");
            return;
        }

        AdUnitObject unitObject = DataTable.of(
                AdUnitIndex.class
        ).get(keywordTable.getUnitId());
        if (unitObject == null) {
            log.error("AdUnitKeywordTable index error: {}",
                    keywordTable.getUnitId());
            return;
        }

        Set<Long> value = new HashSet<>(
                Collections.singleton(keywordTable.getUnitId())
        );
        handleBinlogEvent(
                DataTable.of(UnitKeywordIndex.class),
                keywordTable.getKeyword(),
                value,
                type
        );
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值