為什么寫這個(gè)類丁溅?
- 投放系統(tǒng)每條投放都會(huì)附帶一組城市id怪嫌,如果投放全國(guó)义锥,或者海外全部,或某種城市組合岩灭,導(dǎo)致內(nèi)容放入redis過大(超過2MB無(wú)法放入redis)拌倍,經(jīng)過分析城市id大多是連續(xù)的,因此采用該類對(duì)城市id列表做壓縮
為什么不使用bitmap或bitset噪径?
- 看bitset源碼發(fā)現(xiàn) 一個(gè)bitmap, bitset受最大值的影響柱恤,而不因?yàn)閿?shù)字個(gè)數(shù)影響其內(nèi)存占用
如何兼顧查找效率?
- 從一組城市id轉(zhuǎn)變?yōu)檎麛?shù)區(qū)間時(shí)找爱,會(huì)對(duì)區(qū)間進(jìn)行排序形成有序區(qū)間梗顺,數(shù)據(jù)結(jié)構(gòu)選用ArrayList,算法采取二分查找车摄,以此達(dá)到更好的查找效率
使用區(qū)間類替換Set之后的效果寺谤?
- 對(duì)比redis里面的數(shù)據(jù)大小,同一個(gè)運(yùn)營(yíng)位的內(nèi)容從2.05MB下降到 115KB, 壓縮效果明顯
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* 功能描述: 用于表達(dá)連續(xù)的整數(shù)區(qū)間
*
* @author : yuanchao.he
* @version 1.0 2017-04-06
*/
@Getter
@Setter
public class LocalRange implements Serializable {
private int lowerRange;
private int upperRange;
public LocalRange(int lowerRange, int upperRange) throws Exception {
if (lowerRange > upperRange) {
throw new Exception(
"invalid range number, upperRange must greater or equal to lowerRange");
}
this.lowerRange = lowerRange;
this.upperRange = upperRange;
}
private LocalRange() {}
/**
* 單個(gè)區(qū)間內(nèi)是否包含特定的整數(shù)
* @param current
* @return
*/
public boolean contains(final int number) {
return this.lowerRange <= number && number <= this.upperRange;
}
/**
* 一組區(qū)間內(nèi)是否包含特定的整數(shù)
* @param ranges
* @param number
* @return
*/
public static boolean contains(final List<LocalRange> ranges, final int number){
return binarySearch(ranges, number)!=null;
}
/**
* 采用二分查找的方式來(lái)查詢
* @param ranges
* @param number
* @return
*/
public static LocalRange binarySearch(List<LocalRange> ranges, int number) {
if (CollectionUtils.isEmpty(ranges)) {
return null;
}
int from = 0;
int to = ranges.size() - 1;
while (from <= to) {
int middle = (to - from) / 2 + from;
if (ranges.get(middle).contains(number)) {
return ranges.get(middle);
}
if (ranges.get(middle).getUpperRange() < number) {
from = middle + 1;
}
if (ranges.get(middle).getLowerRange() > number) {
to = middle - 1;
}
}
return null;
}
/**
* 將集合中的整數(shù)轉(zhuǎn)化為一組區(qū)間
* @param collection
* @return
*/
public static List<LocalRange> fromCollection(Collection<Integer> collection) {
if (CollectionUtils.isEmpty(collection)) {
return Collections.emptyList();
}
List<Integer> numbers = Lists.newArrayList(collection);
Collections.sort(numbers);
List<LocalRange> ranges = Lists.newArrayList();
int lowerRange = numbers.get(0);
int upperRange = numbers.get(0);
for (int index = 1; index < numbers.size(); index++) {
if ((upperRange + 1) == numbers.get(index)) {
upperRange = numbers.get(index);
} else {
ranges.add(LocalRange.init(lowerRange, upperRange));
lowerRange = numbers.get(index);
upperRange = lowerRange;
}
}
ranges.add(LocalRange.init(lowerRange, upperRange));
return ranges;
}
private static LocalRange init(int lowerRange, int upperRange) {
LocalRange localRange = new LocalRange();
localRange.lowerRange = lowerRange;
localRange.upperRange = upperRange;
return localRange;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者