{"id":13756176,"url":"https://github.com/fanliao/go-concurrentMap","last_synced_at":"2025-05-10T03:30:57.842Z","repository":{"id":21836707,"uuid":"25159768","full_name":"fanliao/go-concurrentMap","owner":"fanliao","description":null,"archived":false,"fork":false,"pushed_at":"2017-02-07T20:51:59.000Z","size":640,"stargazers_count":122,"open_issues_count":3,"forks_count":23,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-11-16T11:33:39.023Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fanliao.png","metadata":{"files":{"readme":"README.md","changelog":"change.log","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-13T13:29:28.000Z","updated_at":"2024-09-17T02:06:29.000Z","dependencies_parsed_at":"2022-07-17T00:00:54.804Z","dependency_job_id":null,"html_url":"https://github.com/fanliao/go-concurrentMap","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fanliao%2Fgo-concurrentMap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fanliao%2Fgo-concurrentMap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fanliao%2Fgo-concurrentMap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fanliao%2Fgo-concurrentMap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fanliao","download_url":"https://codeload.github.com/fanliao/go-concurrentMap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253358069,"owners_count":21895967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T11:00:38.404Z","updated_at":"2025-05-10T03:30:57.566Z","avatar_url":"https://github.com/fanliao.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"go-concurrentMap\n================\n\ngo-concurrentMap is a concurrent Map implement, it is ported from java.util.ConcurrentHashMap.\n\n##### Current version: 1.0 Beta\n\n## Quick start\n\n#### Put, Remove, Replace and Clear methods\n\n```go\nm := concurrent.NewConcurrentMap()\n\npreviou, err := m.Put(1, 10)                   //return nil, nil\npreviou, err = m.PutIfAbsent(1, 20)            //return 10, nil\n\nval, err := m.Get(1)                           //return 10, nil\ns := m.Size()                                  //return 1\n\nm.PutAll(map[interface{}]interface{}{\n\t1: 100,\n\t2: 200,\n})\n\nok, err := m.RemoveEntry(1, 100)               //return true, nil\n\npreviou, err = m.Replace(2, 20)                //return 200, nil\nok, err = m.CompareAndReplace(2, 200, 20)      //return false, nil\n\npreviou, err = m.Remove(2)                     //return 20, nil\n\nm.Clear()\ns = m.Size()                                   //return 0\n\n```\n\n#### Safely use composition operation to update the value from multiple threads\n\n```go\n/*---- group string by first char using ConcurrentMap ----*/\n//sliceAdd function returns a function that appends v into slice\nsliceAdd := func(v interface{}) (func(interface{}) interface{}){\n    return func(oldVal interface{})(newVal interface{}){\n\t\tif oldVal == nil {\n\t\t\tvs :=  make([]string, 0, 1)\n\t\t\treturn append(vs, v.(string))\n\t\t} else {\n\t\t\treturn append(oldVal.([]string), v.(string))\n\t\t}\n\t}\n}\n\nm := concurrent.NewConcurrentMap()\n//group by first char of str\ngroup := func(str string) {\n\tm.Update(string(str[0]), sliceAdd(str))\n}\n\ngo group(\"stone\")\ngo group(\"jack\")\ngo group(\"jackson\")\n\n/*m will include the below key-value pairs, but please note sequence may be different:\n{\n  s:[stone],\n  j:[jack jackson],\n}\n*/\n```\n\n#### Use Hashable interface to customize hash code and equals logic and support reference type and pointer type\n\n```go\n//user implements concurrent.Hasher interface\ntype user struct {\n\tid   string\n\tName string\n}\n\nfunc (u *user) HashBytes() []byte {\n\treturn []byte(u.id)\n}\nfunc (u *user) Equals(v2 interface{}) (equal bool) {\n\tu2, ok := v2.(*user)\n\treturn ok \u0026\u0026 u.id == u2.id\n}\n\nm := concurrent.NewConcurrentMap()\npreviou, err := m.Put(\u0026user, 10)                   //return nil, nil\nval, err := m.Get(\u0026user)                           //return 10, nil\n```\n\n#### Iterator and get key-value slice\n\n```go\n//iterate ConcurrentMap\nfor itr := m.Iterator();itr.HasNext(); {\n\tk, v, _ := itr.Next()\n}\n\n//only user Next method to iterate ConcurrentMap\nfor itr := m.Iterator();; {\n\tk, v, ok := itr.Next()\n\tif !ok {\n\t\tbreak\n\t}\n}\n\n//ToSlice\nfor _, entry := range m.ToSlice(){\n\tk, v := entry.Key(), entry.Value()\n}\n```\n\n#### More factory functions\n\n```go\n//new concurrentMap with specified initial capacity\nm = concurrent.NewConcurrentMap(32)\n\n//new concurrentMap with specified initial capacity and load factor\nm = concurrent.NewConcurrentMap(32, 0.75)\n\n//new concurrentMap with specified initial capacity, load factor and concurrent level\nm = concurrent.NewConcurrentMap(32, 0.75, 16)\n\n//new concurrentMap with the same mappings as the given map\nm = concurrent.NewConcurrentMapFromMap(map[interface{}]interface{}{\n\t\t\"x\":                      \"x1val\",\n\t\t\"xx\":                     \"x2val\",\n\t})\n\t\n```\n\n## Doc\n\n[Go Doc at godoc.org](https://godoc.org/github.com/fanliao/go-concurrentMap)\n\n## Limitations\n\n* Do not support the below types as key:\n\n   - pointer\n   - slice (do not support == operator)\n   - map (do not support == operator)\n   - channel \n   - function (do not support == operator)\n   - struct that includes field which type is above-mentioned or interface\n   - array which element type is above-mentioned or interface\n\n   Do not support pointer because the memory address of pointer may be changed after GC, so cannot get a invariant value as hash code for pointer type. Please refer to [when  in next releases of go compacting GC move pointers, does map on poiner types will work ?](https://groups.google.com/forum/#!topic/golang-nuts/AFEf6VM-qrY)\n\n## Performance\n\nBelow are the CPU, OS and parameters of benchmark testing: \n \nXeon E3-1230V3 3.30GHZ, Win7 64 OS\n\nUse 8 procs and 9 goroutines, every goroutines will put or get 100,000 key-value pairs.\n\nI used a thread safe implement that uses the RWMutex to compare the performance,. The below are the test results:\n\n\u003ctable\u003e\n\t\u003ctr\u003e\n\t\t\u003cth\u003e\u003c/th\u003e\n\t\t\u003cth\u003eUse RWMutex\u003c/th\u003e\n\t\t\u003cth\u003eConcurrentMap\u003c/th\u003e\n\t\u003c/tr\u003e\n\t\u003ctr\u003e\n\t\t\u003ctd\u003ePut\u003c/td\u003e\n\t\t\u003ctd\u003e480.000 ms/op\u003c/td\u003e\n\t\t\u003ctd\u003e130.207 ms/op\u003c/td\u003e\n\t\u003c/tr\u003e\n\t\t\u003ctd\u003eGet\u003c/td\u003e\n\t\t\u003ctd\u003e45.643 ms/op\u003c/td\u003e\n\t\t\u003ctd\u003e69.464 ms/op\u003c/td\u003e\n\t\u003ctr\u003e\n\t\t\u003ctd\u003ePut and Get\u003c/td\u003e\n\t\t\u003ctd\u003e729.534 ms/op\u003c/td\u003e\n\t\t\u003ctd\u003e166.610 ms/op\u003c/td\u003e\n\t\u003c/tr\u003e\n\u003c/table\u003e\n\nNote the performance of LockMap's Get operation is better than concurrentMap, the reason is that RWMutex supports parallel read. But if multiple threads put and get at same time, ConcurrentMap will be better than LockMap.\n\n## License\n\ngo-concurrentMap is licensed under the MIT Licence, (http://www.apache.org/licenses/LICENSE-2.0.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffanliao%2Fgo-concurrentMap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffanliao%2Fgo-concurrentMap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffanliao%2Fgo-concurrentMap/lists"}