1に、我々は2があり、広告の索引メンテナンスを解析し、全量索引加载
そして增量索引维护
。広告は、広告システムは、最も重要な部分を取得し、Googleのインデックスのデザインのアイデアを理解することは注意しなければならないですので、我々は、インデックスのメンテナンス機能をコーディング来ます。
私たちは、屈折率変化の検索操作のすべての追加を受信するためのインタフェースは、インタフェースは、パラダイムを定義し、2つのパラメータを受信定めるK
Googleのインデックスの代表キー値、V
戻り値の代表を。
/**
* IIndexAware for 实现广告索引的增删改查
*
* @author <a href="mailto:[email protected]">Isaac.Zhang | 若初</a>
*/
public interface IIndexAware<K, V> {
/**
* 通过key 获取索引
*/
V get(K key);
/**
* 添加索引
* @param key
* @param value
*/
void add(K key, V value);
/**
* 更新索引
*/
void update(K key, V value);
/**
* 删除索引
*/
void delete(K key, V value);
}
私たちのような、いないすべてのデータベーステーブルのインデックスを作成する必要があることを知っている必要がありUser
、我々はデータが実際に必要とされている取得したときに、当然のことながら、インデックスを作成する必要はありません、テーブル、およびテーブル内のすべてのフィールドは、インデックスを必要としませんこれはまた、我々は書くつもりされているような、情報の分野を決定するために、特定のビジネスに基づいて推广计划
インデックスを、販売促進プログラム名が必要とされないことがあります。次に、私たちは私たちの最初を達成しました正向索引
。
- まず、エンティティオブジェクト操作推進計画を作成します
/**
* AdPlanIndexObject for 推广计划索引对象
* 这个索引对象我们没有添加 推广计划名称
* @author <a href="mailto:[email protected]">Isaac.Zhang | 若初</a>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanIndexObject {
private Long planId;
private Long userId;
private Integer planStatus;
private Date startDate;
private Date endDate;
/**
* 根据实际字段来更新索引
*/
public void update(AdPlanIndexObject newObject) {
if (null != newObject.getPlanId()) {
this.planId = newObject.getPlanId();
}
if (null != newObject.getUserId()) {
this.userId = newObject.getUserId();
}
if (null != newObject.getPlanStatus()) {
this.planStatus = newObject.getPlanStatus();
}
if (null != newObject.getStartDate()) {
this.startDate = newObject.getStartDate();
}
if (null != newObject.getEndDate()) {
this.endDate = newObject.getEndDate();
}
}
}
- そして、インデックスクラスの実現を促進し、実装するための計画作成
IIndexAware
のインターフェースを。
/**
* AdPlanIndexAwareImpl for 推广计划索引实现类
*
* @author <a href="mailto:[email protected]">Isaac.Zhang | 若初</a>
*/
@Slf4j
@Component
public class AdPlanIndexAwareImpl implements IIndexAware<Long, AdPlanIndexObject> {
private static Map<Long, AdPlanIndexObject> planIndexObjectMap;
/**
* 因为操作索引的过程中有可能对索引进行更新,为了防止多线程造成的线程不安全问题,我们不能使用hashmap,需要实现ConcurrentHashMap
*/
static {
planIndexObjectMap = new ConcurrentHashMap<>();
}
@Override
public AdPlanIndexObject get(Long key) {
return planIndexObjectMap.get(key);
}
@Override
public void add(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before add::{}", planIndexObjectMap);
planIndexObjectMap.put(key, value);
log.info("AdPlanIndexAwareImpl after add::{}", planIndexObjectMap);
}
@Override
public void update(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before update::{}", planIndexObjectMap);
//查询当前的索引信息,如果不存在,直接新增索引信息
AdPlanIndexObject oldObj = planIndexObjectMap.get(key);
if (null == oldObj) {
planIndexObjectMap.put(key, value);
} else {
oldObj.update(value);
}
log.info("AdPlanIndexAwareImpl after update::{}", planIndexObjectMap);
}
@Override
public void delete(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before delete::{}", planIndexObjectMap);
planIndexObjectMap.remove(key);
log.info("AdPlanIndexAwareImpl after delete::{}", planIndexObjectMap);
}
}
これまでのところ、我々は、コードインデックス対象のインデックス操作を完了し、推進計画は、我々が完了するためには、上記の例を参照することができ、準備推广单元
、推广创意
、地域
、兴趣
、关键词
および推广创意和推广单元的关联索引
、または直接からGithubのポータル / Giteeポータルソースコードをダウンロードします。
上記のコードによると、我々が定義されたインデックス操作のすべてを達成していることを示しているが、現実には、我々はあなたがそれぞれにサービスを必要とし、これらのサービスを使用する必要があります
@Autowired
また、フォローアップが含まれていない、私たちのインデックス操作のクラスのように多く、注入そこに作業負荷が大きすぎる、インデックスの次元を追加する必要があり、かつ維持するために不便、資格のプログラマーとして、これは非常に非友好的で、そしておそらく、その後の開発者のBS-INGを行うことができます。
叱られるのフォローアップを防ぐために、我々は、インデックスキャッシュツールを準備する必要がcom.sxzhongf.ad.index.IndexDataTableUtils
最初の不安に対処するためのインデックスバッファツールを注入することによって、これを達成するために。このユーティリティクラスを達成するために、我々は二つのインタフェースを実装する必要がありますorg.springframework.context.ApplicationContextAware
し、org.springframework.core.PriorityOrdered
org.springframework.context.ApplicationContextAware
、その中で動作し、Beanインスタンススプリング容器に統一されたインタフェースを実装することによって、クラス。
春には、するAware
ように、クラスの終わりをサフィックス、それは単純に理解することができるアプリケーションがXXXしたい、たとえば、ApplicationContextAware
あなたがしたいアプリケーションのためにApplicationContext
、BeanFactoryAware
アプリケーションは、あなたがしたいことBeanFactory
など...org.springframework.core.PriorityOrdered
アセンブリのロード・シーケンスは、使用することができますorg.springframework.core.Ordered
当社のツールについては、次のコードを:
package com.sxzhongf.ad.index;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* IndexDataTableUtils for 所有索引服务需要缓存的Java Bean
*
* 使用方式:
* 获取{@link com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl}索引服务类
* 如下:
* {@code
* IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
* }
* @author <a href="mailto:[email protected]">Isaac.Zhang | 若初</a>
*/
@Component
public class IndexDataTableUtils implements ApplicationContextAware, PriorityOrdered {
//注入ApplicationContext
private static ApplicationContext applicationContext;
/**
* 定义用于保存所有Index的Map
* Class标示我们的索引类
*/
private static final Map<Class, Object> dataTableMap = new ConcurrentHashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
IndexDataTableUtils.applicationContext = applicationContext;
}
/**
* 获取索引服务缓存
*/
public static <T> T of(Class<T> klass) {
T instance = (T) dataTableMap.get(klass);
//如果获取到索引bean,直接返回当前bean
if (null != instance) {
return instance;
}
//首次获取索引bean为空,写入Map
dataTableMap.put(klass, bean(klass));
return (T) dataTableMap.get(klass);
}
/**
* 获取Spring 容器中的Bean对象
*/
private static <T> T bean(String beanName) {
return (T) applicationContext.getBean(beanName);
}
/**
* 获取Spring 容器中的Bean对象
*/
private static <T> T bean(Class klass) {
return (T) applicationContext.getBean(klass);
}
@Override
public int getOrder() {
return PriorityOrdered.HIGHEST_PRECEDENCE;
}
}