断断续续的做了一个月(主要是懒)
完成了
Server负责接受并返回Client的公网IP
Client负责定时轮询Server并在IP更新至阿里的DNS服务器
在client和Sever,go build main.go
client的启动参数分别为 accessKey accessId severAccessKey severIP:port
server启动参数为
severAccessKey
下一步将domain和RR放在配置文件中
测试代码在上
测试结果:
build avl tree cost time: 52999 ms
build binary tree cost time: 41526 ms
build FuzzyLookUpList cost time: 860 ms
avl queryTime:40 ms
binary queryTime:45231 ms
FuzzyLookUpList queryTime:9 ms
SimpleMap insertTime:51 ms
SimpleMap queryTime:7 ms
结论:多用数组多用数组
一般意义上来说,程序的重构和文章的修辞变更有着相同点
比如,购买这个操作:
伪代码可以写成:
Def buy:
{
Do transaction:
Service.Good -1
Customer.Good+1
Service.money+
Customer.money-1
Transaction end
}
也可以写成:
Def buy{
Do transaction
Service.Good-1
Customer.Good+1
End transaction
Defer:
Transfer money
}
两段伪代码都描述了这么一件事情
顾客购买商品的操作抽象为:
顾客拥有+1
商户拥有-1
顾客金钱-1
商户金钱+1
第二种写法在一般的做法上会比第一种写法更加灵活,因为我们将不同阶段的事情分开了,转账和库存变成了两个事情,可以异步进行
但是如果我们要求不高,第一种写法也是绝对可以满足需要的
这就是重构。
所谓代码的重构无非也就是将一开始语义不显著的代码,变为语义显著的代码,减少代码之间的不确定性,优化代码描述事件的模型。所谓clean code更多的是表现在能够准确的反映业务模型的操作。
第一版:
package com.michaelssss; import java.util.ArrayList; import java.util.List; /** * @author michaelssss * @since 2017/12/7 */ public class SimpleMap<T, P> { private List<Pair<T, P>>[] buckets; public SimpleMap() { buckets = new ArrayList[64]; } public void put(T key, P value) { int hashCode = key.hashCode(); if (buckets[(hashCode % 64)] == null || buckets[(hashCode % 64)].isEmpty()) { buckets[(hashCode % 64)] = new ArrayList<>(); buckets[(hashCode % 64)].add(new Pair<>(key, value)); } else { boolean found = false; for (Pair<T, P> pair : buckets[(hashCode % 64)]) { if (pair.key.equals(key)) { pair.value = value; found = true; } } if (!found) { buckets[(hashCode % 64)].add(new Pair<>(key, value)); } } } public P get(T t) { P value = null; int hashCode = t.hashCode(); if (null == buckets[(hashCode % 64)] || buckets[(hashCode % 64)].isEmpty()) { return null; } else { for (Pair<T, P> pair : buckets[(hashCode % 64)]) { if (pair.key.equals(t)) { value = pair.value; break; } } } return value; } private static class Pair<T, P> { T key; P value; Pair(T key, P value) { this.key = key; this.value = value; } } }
第二版(加入resize操作):
package com.michaelssss; import java.util.ArrayList; import java.util.List; /** * @author michaelssss * @since 2017/12/7 */ public class SimpleMap<T, P> { private List<Pair<T, P>>[] buckets; private float loadfactory = 0.75f; private int bucketSize; private int count; public SimpleMap() { bucketSize = 64; buckets = new ArrayList[64]; } public SimpleMap(int initSize) { bucketSize = initSize; buckets = new ArrayList[initSize]; } public SimpleMap(int initSize, float loadfactory) { this(initSize); this.loadfactory = loadfactory; } public void put(T key, P value) { int hashCode = key.hashCode(); if (buckets[(hashCode % bucketSize)] == null || buckets[(hashCode % bucketSize)].isEmpty()) { buckets[(hashCode % bucketSize)] = new ArrayList<>(); buckets[(hashCode % bucketSize)].add(new Pair<>(key, value)); count++; } else { boolean found = false; for (Pair<T, P> pair : buckets[(hashCode % bucketSize)]) { if (pair.key.equals(key)) { pair.value = value; found = true; } } if (!found) { buckets[(hashCode % bucketSize)].add(new Pair<>(key, value)); count++; } } if (loadfactory * count > bucketSize) { resize(); } } private void resize() { int newBucketSeize = bucketSize * 2; List<Pair<T, P>>[] buckets = new ArrayList[newBucketSeize]; for (List<Pair<T, P>> bucket : this.buckets) { if (null != bucket) { for (Pair<T, P> pair : bucket) { int hashCode = pair.key.hashCode(); if (null == buckets[hashCode % newBucketSeize]) { buckets[hashCode % newBucketSeize] = new ArrayList<>(); } buckets[hashCode % newBucketSeize].add(pair); } } } this.buckets = buckets; this.bucketSize = newBucketSeize; } public P get(T t) { P value = null; int hashCode = t.hashCode(); if (null == buckets[(hashCode % bucketSize)] || buckets[(hashCode % bucketSize)].isEmpty()) { return null; } else { for (Pair<T, P> pair : buckets[(hashCode % bucketSize)]) { if (pair.key.equals(t)) { value = pair.value; break; } } } return value; } private final static class Pair<T, P> { T key; P value; Pair(T key, P value) { this.key = key; this.value = value; } } }