hash

How to permit hash with * key => values?

一个人想着一个人 提交于 2020-04-10 06:21:07
问题 I want to create an object with strong params that can accept dynamic hash keys. This is my code, Quiz.create(quiz_params) def quiz_params params.require(:quiz).permit(:user_id, :percent, :grade, questions: {}) end data that gets passed in would look something like this. // the keys that get passed into question is always different quiz: { user_id: 1, percent: 80, grade: "B", questions: { "12": "24", "1": "12", "4": "3", "5": "22" } } Currently however, when I try to create a Quiz, the

Java HashMap源码详解

夙愿已清 提交于 2020-04-08 01:30:17
Java数据结构-HashMap 目录 Java数据结构-HashMap 1. HashMap 1.1 HashMap介绍 1.1.1 HashMap介绍 1.1.2 HashMap继承图 1.2 HashMap 组成结构 1.2.1 Hashmap底层数据结构 2.HashMap源码解析 2.1 HashMap属性源码解析 2.1.1 HashMap中的静态常量 2.1.2 HashMap中的属性 2.1.2 HashMap中的存储结点 2.1.3 Hash表 2.2 方法源码分析 2.2.1 构造方法分析 2.2.2 Put(K key,V value) 待续... 1. HashMap 1.1 HashMap介绍 1.1.1 HashMap介绍 HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做 Entry ,有着key与value两个基本属性以及有其他的包含其他结点位置信息的属性 通过HashMap我们可以存储键值对,并且可以在较短的时间复杂度内, 1.1.2 HashMap继承图 HashMap通过继承 AbstractMap 实现了 Map 接口,且本身也实现了 Map 接口 在接口实现关系上来看为多余操作 但有一点要注意,在使用 反射获取实现接口 时,如果不是显示实现某接口而是通过继承来实现该接口,则不会获取该接口类型,这一点在使用

【转】cuckoo hash

夙愿已清 提交于 2020-04-07 21:35:46
【转】cuck hash https://coolshell.cn/articles/17225.html (感谢网友 @我的上铺叫路遥 投稿) 对于海量数据处理业务,我们通常需要一个索引数据结构,用来帮助查询,快速判断数据记录是否存在,这种数据结构通常又叫过滤器(filter)。考虑这样一个场景,上网的时候需要在浏览器上输入URL,这时浏览器需要去判断这是否一个恶意的网站,它将对本地缓存的成千上万的URL索引进行过滤,如果不存在,就放行,如果(可能)存在,则向远程服务端发起验证请求,并回馈客户端给出警告。 索引的存储又分为有序和无序,前者使用关联式容器,比如B树,后者使用哈希算法。这两类算法各有优劣:比如,关联式容器时间复杂度稳定O(logN),且支持范围查询;又比如哈希算法的查询、增删都比较快O(1),但这是在理想状态下的情形,遇到碰撞严重的情况,哈希算法的时间复杂度会退化到O(n)。因此,选择一个好的哈希算法是很重要的。 时下一个非常流行的哈希索引结构就是 bloom filter ,它类似于bitmap这样的hashset,所以空间利用率很高。其独特的地方在于它使用多个哈希函数来避免哈希碰撞,如图所示( 来源wikipedia ),bit数组初始化为全0,插入x时,x被3个哈希函数分别映射到3个不同的bit位上并置1,查询x时

Comparing large strings in JavaScript with a hash

会有一股神秘感。 提交于 2020-04-07 18:53:27
问题 I have a form with a textarea that can contain large amounts of content (say, articles for a blog) edited using one of a number of third party rich text editors. I'm trying to implement something like an autosave feature, which should submit the content through ajax if it's changed. However, I have to work around the fact that some of the editors I have as options don't support an "isdirty" flag, or an "onchange" event which I can use to see if the content has changed since the last save. So,

When is hash(n) == n in Python?

与世无争的帅哥 提交于 2020-04-07 10:58:50
问题 I've been playing with Python's hash function. For small integers, it appears hash(n) == n always. However this does not extend to large numbers: >>> hash(2**100) == 2**100 False I'm not surprised, I understand hash takes a finite range of values. What is that range? I tried using binary search to find the smallest number hash(n) != n >>> import codejamhelpers # pip install codejamhelpers >>> help(codejamhelpers.binary_search) Help on function binary_search in module codejamhelpers.binary

Why hash function on two different objects return same value?

柔情痞子 提交于 2020-04-07 02:57:12
问题 I used Spyder, run Python 2.7. Just found interesting things: hash(-1) and hash(-2) both return -2, is there a problem? I though hash function on different object should return different values. I read previous posts that -1 is reserved as an error in Python. hash('s') returns 1835142386, then hash(1835142386) returns the same value. Is this another problem? Thanks. 回答1: -1 is not "reserved as an error" in Python. Not sure what that would even mean. There are a huge number of programs you

Why hash function on two different objects return same value?

孤人 提交于 2020-04-07 02:55:05
问题 I used Spyder, run Python 2.7. Just found interesting things: hash(-1) and hash(-2) both return -2, is there a problem? I though hash function on different object should return different values. I read previous posts that -1 is reserved as an error in Python. hash('s') returns 1835142386, then hash(1835142386) returns the same value. Is this another problem? Thanks. 回答1: -1 is not "reserved as an error" in Python. Not sure what that would even mean. There are a huge number of programs you

dubbo的负载均衡的几种算法

别等时光非礼了梦想. 提交于 2020-04-06 23:49:35
Random LoadBalance 随机,按权重设置随机概率。 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。 RoundRobin LoadBalance 轮循,按公约后的权重设置轮循比率。 存在慢的提供者累积请求问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。 LeastActive LoadBalance 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。 ConsistentHash LoadBalance 一致性Hash,相同参数的请求总是发到同一提供者。 当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。 算法参见: http://en.wikipedia.org/wiki/Consistent_hashing 。 缺省只对第一个参数Hash,如果要修改,请配置<dubbo:parameter key="hash.arguments" value="0,1" /> 缺省用160份虚拟节点,如果要修改,请配置<dubbo:parameter key="hash.nodes" value="320" /> 这几种算法是很常见的

大型网站技术架构——网站架构的伸缩性设计

眉间皱痕 提交于 2020-04-06 22:45:28
首先,所谓网站的伸缩性,指 不需要改变网站的软硬件设计,仅仅通过改变部署的服务器数量就可以扩大或者缩小网站的服务处理能力 。在整个互联网行业的发展渐进演化中,最重要的技术就是 服务器集群 ,通过不断地向集群中添加服务器来增强整个集群的处理能力。 一、网站架构的伸缩性设计 1.1 不同功能进行物理分离实现伸缩   (1)纵向分离:将业务处理流程上得不同部分分离部署,实现系统的伸缩性;   (2)横向分离:将不同的业务模块分离部署,实现系统的伸缩性; 1.2 单一功通过集群规模实现伸缩   使用服务器集群,即将相同服务部署在多台服务器上构成一个集群整体对外提供服务。具体来说,集群伸缩性又分为应用服务器集群伸缩性和数据服务器集群伸缩性。这两种集群对于数据状态管理的不同,技术实现也有很大的区别。  It is said that 当一头牛拉不动车的时候,不要去寻找一头更强壮的牛,而是用两头牛来拉车 。 二、应用服务器集群的伸缩性设计 2.1 应用服务器那点必须知道的事儿   (1)应用服务器应该被设计成 无状态 的,即应用服务器不存储请求上下文信息;构建集群后,每次用户的请求都可以发到集群中任意一台服务器上处理,任何一台服务器的处理结果都是相同的;   (2)HTTP本身是一个无状态的连接协议,为了支持客户端与服务器之间的交互,我们就需要通过不同的技术为交互存储状态

HashMap的ReHash图解

大城市里の小女人 提交于 2020-04-06 18:26:00
昨天在看redis的hash扩容时提到了与java的hashmap类似,之前一直没有仔细研究过,翻了几篇博客,选了容易理解的一片转载下。 resize方法 void resize ( intnewCapacity ) { Entry [ ] oldTable = table ; intoldCapacity = oldTable . length ; . . . . . . //创建一个新的Hash Table Entry [ ] newTable = new Entry [ newCapacity ] ; //将Old Hash Table上的数据迁移到New Hash Table上 transfer ( newTable ) ; table = newTable ; threshold = ( int ) ( newCapacity * loadFactor ) ; } transfer方法 void transfer ( Entry [ ] newTable ) { Entry [ ] src = table ; intnewCapacity = newTable . length ; //下面这段代码的意思是: // 从OldTable里摘一个元素出来,然后放到NewTable中 for ( int j = 0 ; j < src . length ; j ++ ) {