|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +var SHARD_COUNT = 32 |
| 9 | + |
| 10 | +// A "thread" safe map of type string:Anything. |
| 11 | +// To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards. |
| 12 | +type ConcurrentMap []*ConcurrentMapShared |
| 13 | + |
| 14 | +// A "thread" safe string to anything map. |
| 15 | +type ConcurrentMapShared struct { |
| 16 | + items map[string]interface{} |
| 17 | + sync.RWMutex // Read Write mutex, guards access to internal map. |
| 18 | +} |
| 19 | + |
| 20 | +// Creates a new concurrent map. |
| 21 | +func NewConcurrentMap() ConcurrentMap { |
| 22 | + m := make(ConcurrentMap, SHARD_COUNT) |
| 23 | + for i := 0; i < SHARD_COUNT; i++ { |
| 24 | + m[i] = &ConcurrentMapShared{items: make(map[string]interface{})} |
| 25 | + } |
| 26 | + return m |
| 27 | +} |
| 28 | + |
| 29 | +// Returns shard under given key |
| 30 | +func (m ConcurrentMap) GetShard(key string) *ConcurrentMapShared { |
| 31 | + return m[uint(fnv32(key))%uint(SHARD_COUNT)] |
| 32 | +} |
| 33 | + |
| 34 | +// Sets the given value under the specified key. |
| 35 | +func (m ConcurrentMap) Set(key string, value interface{}) { |
| 36 | + // Get map shard. |
| 37 | + shard := m.GetShard(key) |
| 38 | + shard.Lock() |
| 39 | + shard.items[key] = value |
| 40 | + shard.Unlock() |
| 41 | +} |
| 42 | + |
| 43 | +// Retrieves an element from map under given key. |
| 44 | +func (m ConcurrentMap) Get(key string) (interface{}, bool) { |
| 45 | + // Get shard |
| 46 | + shard := m.GetShard(key) |
| 47 | + shard.RLock() |
| 48 | + // Get item from shard. |
| 49 | + val, ok := shard.items[key] |
| 50 | + shard.RUnlock() |
| 51 | + return val, ok |
| 52 | +} |
| 53 | + |
| 54 | +// Used by the Iter & IterBuffered functions to wrap two variables together over a channel, |
| 55 | +type Tuple struct { |
| 56 | + Key string |
| 57 | + Val interface{} |
| 58 | +} |
| 59 | + |
| 60 | +// Returns a buffered iterator which could be used in a for range loop. |
| 61 | +func (m ConcurrentMap) IterBuffered() <-chan Tuple { |
| 62 | + chans := snapshot(m) |
| 63 | + total := 0 |
| 64 | + for _, c := range chans { |
| 65 | + total += cap(c) |
| 66 | + } |
| 67 | + ch := make(chan Tuple, total) |
| 68 | + go fanIn(chans, ch) |
| 69 | + return ch |
| 70 | +} |
| 71 | + |
| 72 | +// Returns a array of channels that contains elements in each shard, |
| 73 | +// which likely takes a snapshot of `m`. |
| 74 | +// It returns once the size of each buffered channel is determined, |
| 75 | +// before all the channels are populated using goroutines. |
| 76 | +func snapshot(m ConcurrentMap) (chans []chan Tuple) { |
| 77 | + chans = make([]chan Tuple, SHARD_COUNT) |
| 78 | + wg := sync.WaitGroup{} |
| 79 | + wg.Add(SHARD_COUNT) |
| 80 | + // Foreach shard. |
| 81 | + for index, shard := range m { |
| 82 | + go func(index int, shard *ConcurrentMapShared) { |
| 83 | + // Foreach key, value pair. |
| 84 | + shard.RLock() |
| 85 | + chans[index] = make(chan Tuple, len(shard.items)) |
| 86 | + wg.Done() |
| 87 | + for key, val := range shard.items { |
| 88 | + chans[index] <- Tuple{key, val} |
| 89 | + } |
| 90 | + shard.RUnlock() |
| 91 | + close(chans[index]) |
| 92 | + }(index, shard) |
| 93 | + } |
| 94 | + wg.Wait() |
| 95 | + return chans |
| 96 | +} |
| 97 | + |
| 98 | +// fanIn reads elements from channels `chans` into channel `out` |
| 99 | +func fanIn(chans []chan Tuple, out chan Tuple) { |
| 100 | + wg := sync.WaitGroup{} |
| 101 | + wg.Add(len(chans)) |
| 102 | + for _, ch := range chans { |
| 103 | + go func(ch chan Tuple) { |
| 104 | + for t := range ch { |
| 105 | + out <- t |
| 106 | + } |
| 107 | + wg.Done() |
| 108 | + }(ch) |
| 109 | + } |
| 110 | + wg.Wait() |
| 111 | + close(out) |
| 112 | +} |
| 113 | + |
| 114 | +// Reviles ConcurrentMap "private" variables to json marshal. |
| 115 | +func (m ConcurrentMap) MarshalJSON() ([]byte, error) { |
| 116 | + // Create a temporary map, which will hold all item spread across shards. |
| 117 | + tmp := make(map[string]interface{}) |
| 118 | + |
| 119 | + // Insert items to temporary map. |
| 120 | + for item := range m.IterBuffered() { |
| 121 | + tmp[item.Key] = item.Val |
| 122 | + } |
| 123 | + return json.Marshal(tmp) |
| 124 | +} |
| 125 | + |
| 126 | +func fnv32(key string) uint32 { |
| 127 | + hash := uint32(2166136261) |
| 128 | + const prime32 = uint32(16777619) |
| 129 | + for i := 0; i < len(key); i++ { |
| 130 | + hash *= prime32 |
| 131 | + hash ^= uint32(key[i]) |
| 132 | + } |
| 133 | + return hash |
| 134 | +} |
| 135 | + |
| 136 | +// Concurrent map uses Interface{} as its value, therefor JSON Unmarshal |
| 137 | +// will probably won't know which to type to unmarshal into, in such case |
| 138 | +// we'll end up with a value of type map[string]interface{}, In most cases this isn't |
| 139 | +// out value type, this is why we've decided to remove this functionality. |
| 140 | + |
| 141 | +// func (m *ConcurrentMap) UnmarshalJSON(b []byte) (err error) { |
| 142 | +// // Reverse process of Marshal. |
| 143 | + |
| 144 | +// tmp := make(map[string]interface{}) |
| 145 | + |
| 146 | +// // Unmarshal into a single map. |
| 147 | +// if err := json.Unmarshal(b, &tmp); err != nil { |
| 148 | +// return nil |
| 149 | +// } |
| 150 | + |
| 151 | +// // foreach key,value pair in temporary map insert into our concurrent map. |
| 152 | +// for key, val := range tmp { |
| 153 | +// m.Set(key, val) |
| 154 | +// } |
| 155 | +// return nil |
| 156 | +// } |
0 commit comments