Redis源码阅读之字典(一)

数据结构 字典的基本单元——dictEntry 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 typedef struct dictEntry { void *key; union { void *val; uint64_t u64; int64_t s64; double d; } v; struct dictEntry *next; } dictEntry; dictEntry是Redis中的哈希表数据接口的基本单元,有一个指向key的指针,还有一个联合体v,代表的是字典的值,它可以是一个数字(浮点或者有符号整型或者无符号整型),还有一个next字段指向下一个dictEntry的指针。说明了一个问题,dictEntry其实是一个链表节点。 哈希表——dictht 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* This is our hash table structure.……

阅读全文

Centos下,源码安装Python的h3包

公司的渣渣网速,在服务器上pip install h3的时候老是git clone 失败,不得已,只能手动安装h3的包,下面就是我安装h3包的全部过程。 检查cmake版本 1 2 3 cmake --version 如果版本显示是3.1以上就算OK的,但是在centos下面使用yum安装的默认就是2.8的版本,所以需要自己手动安装一下,如何安装我就不说了,传送门给你们:Centos7安装高版本Cmake 下载文件 自己下载对于的master压缩包 https://github.com/uber/h3 https://github.com/uber/h3-py 解压文件 1 2 3 4 5 unzip h3-master.zip unzip h3-py-master.zip 修改.install.sh文件 在解压后的h3-py-master文件夹中有一个.install.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 #!……

阅读全文

Golang重复对一个slice进行append造成数据错误

slice的属性 切片(slice)是建立在数组之上的更方便,更灵活,更强大的数据结构。切片并不存储任何元素而只是对现有数组的引用。 切片的长度是指切片中元素的个数。切片的容量是指从切片的起始元素开始到其底层数组中的最后一个元素的个数。 那么问题来了,几个不同的切片指向同一个数组,如果数组的值被改变会出现什么神奇的操作? 问题实例 slice的cap是怎么增长的 参考文章:How does Go slice capacity change on append? [duplicate] 有兴趣也可以看看golang的源码实现,在$GOPATH/src/runtime/slice.go slice的结构 1 2 3 4 5 6 7 8 9 10 11 type slice struct { array unsafe.Pointer len int cap int } 多次append造成的数据错误 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package main import "fmt" func main() { temp := []int{} temp = append(temp, 1) printSlice("temp", temp) temp = append(temp, 2) printSlice("temp", temp) temp = append(temp, 3) // temp := []int{1, 2, 3} printSlice("temp", temp) temp2 := append(temp, 4) printSlice("temp2", temp2) temp4 := append(temp, 5, 6) printSlice("temp4", temp4) temp3 := append(temp, 5) printSlice("temp3", temp3) printSlice("temp2", temp2) } func printSlice(valName string, s []int) { fmt.……

阅读全文

乐观锁(CAS)

什么是CAS CAS(compare and swap) 比较并替换,比较和替换是线程并发算法时用到的一种技术 CAS是原子操作,保证并发安全,而不是保证并发同步 CAS是CPU的一个指令 CAS是非阻塞的、轻量级的乐观锁 为什么说CAS是乐观锁 乐观锁,严格来说并不是锁,通过原子性来保证数据的同步,比如说数据库的乐观锁,通过版本控制来实现等,所以CAS不会保证线程同步。乐观的认为在数据更新期间没有其他线程影响 CAS原理 CAS(compare and swap) 比较并替换,就是将内存值更新为需要的值,但是有个条件,内存值必须与期望值相同。举个例子,期望值 E、内存值M、更新值U,当E == M的时候将M更新为U。 CAS优缺点 优点 非阻塞的轻量级的乐观锁,通过CPU指令实现,在资源竞争不激烈的情况下性能高,相比synchronized重量锁,synchronized会进行比较复杂的加锁,解锁和唤醒操作。 缺点 ABA问题 线程C、D,线程D将A修改为B后又修改为A,此时C线程以为A没有改变过,java的原子类AtomicStampedReference,通过控制变量值的版本来保证CAS的正确性。 自旋时间过长,消耗CPU资源, 如果资源竞争激烈,多线程自旋长时间消耗资源。 CAS例子 不加锁 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package main import "fmt" var p uint64 var end = make(chan struct{}, 2) func changeP() { for i := 0; i < 100000; i++ { p++ } end <- struct{}{} } func main() { go changeP() go changeP() <-end <-end fmt.……

阅读全文

Ubuntu 18.04换国内源

国内有很多Ubuntu的镜像源,例如阿里、网易、清华、中科大。 我们这里以阿里云的源为例展示如何修改Ubuntu 18.04里面默认的源。 编辑/etc/apt/sources.list文件: deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 中科大源 deb https://mirrors.……

阅读全文

Windows下如何使用pip安装Uber H3

最近项目中会用到uber h3,而我写一个小工具或者去验证一些思路的时候常常会使用Python来着,方便嘛!而Uber H3是C写的,只是为Python提供了拓展,所以需要安装Vs14。这个需要自己提前安装好。 在使用pip安装时直接报错了! 大概的报错内容就是需要cmake,bash等东西,cmake好解决,自己下载到本地,放到环境变量里面就行,bash可以使用git bash来代替,所以只能跟Power shell暂时说拜拜,使用Git Bash来安装H3。Ok, 你运行pip install h3就会看见如下界面……

阅读全文

远程办公的优劣

优势 起床就能工作,累了就能马上躺下休息。 事儿干完了能够开心的摸鱼,想看书看书,想刷题刷题。 遇到烦心事可以破口大骂不怕人听见,哈哈。 劣势 如果自制力不够,无法专心工作,容易摸鱼。 沟通困难,可能你想找的人也找不到。 没有工作氛围,没有工作节奏,等12点的时候,你发现厨房里面没饭吃。 生活容易被打扰,有些人会更加容易把工作和生活混在一起,那你的休息时间可就没了! ……

阅读全文