MCPcopy
hub / github.com/gruns/ImmortalDB / get

Method get

src/index.js:113–150  ·  view source on GitHub ↗
(key, _default = null)

Source from the content-addressed store, hash-verified

111 }
112
113 async get (key, _default = null) {
114 await this.onReady
115
116 const prefixedKey = `${DEFAULT_KEY_PREFIX}${key}`
117
118 const values = await Promise.all(
119 this.stores.map(async store => {
120 try {
121 return await store.get(prefixedKey)
122 } catch (err) {
123 cl(err)
124 }
125 }),
126 )
127
128 const counted = Array.from(countUniques(values).entries())
129 counted.sort((a, b) => a[1] <= b[1])
130
131 let value
132 const [firstValue, firstCount] = arrayGet(counted, 0, [undefined, 0])
133 const [secondValue, secondCount] = arrayGet(counted, 1, [undefined, 0])
134 if (
135 firstCount > secondCount ||
136 (firstCount === secondCount && firstValue !== undefined)
137 ) {
138 value = firstValue
139 } else {
140 value = secondValue
141 }
142
143 if (value !== undefined) {
144 await this.set(key, value)
145 return value
146 } else {
147 await this.remove(key)
148 return _default
149 }
150 }
151
152 async set (key, value) {
153 await this.onReady

Callers

nothing calls this directly

Calls 5

setMethod · 0.95
removeMethod · 0.95
countUniquesFunction · 0.85
arrayGetFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected