本文整理汇总了TypeScript中@statecraft/core.bitHas函数的典型用法代码示例。如果您正苦于以下问题:TypeScript bitHas函数的具体用法?TypeScript bitHas怎么用?TypeScript bitHas使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bitHas函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: bitHas
;(async () => {
const qt = store.storeInfo.capabilities.queryTypes
const q: I.Query = bitHas(qt, I.QueryType.AllKV) ? {type: I.QueryType.AllKV, q:true}
: bitHas(qt, I.QueryType.StaticRange) ? {type: I.QueryType.StaticRange, q: [{low: sel(''), high: sel('\xff')}]}
// : qt.has('static range') ? {type: 'static range', q: [{low: sel('mdraw/'), high: sel('mdraw/~')}]}
: {type: I.QueryType.Single, q:true}
const rtype = queryTypes[q.type].resultType
// I would love to just use subResults here, but
for await (const {raw, results, versions} of subResults(rtype.type!, store.subscribe(q))) {
// console.log('results', results)
state.data = rtype.type! === I.ResultType.Range
? new Map(results[0])
: results
// console.log('val', results, state.data, versions)
const v = version.vRangeTo(versions)
if (raw.replace) {
rtype.mapReplace<any, void>(raw.replace.with, (val, k) => {
pushOp(k, {v, replace: val})
})
}
raw.txns.forEach(({txn}) => {
rtype.mapTxn<any, void>(txn, (op, k) => {
pushOp(k, {v, op})
return null as any as I.Op<void> // It'd be better to have a forEach .. .eh.
})
})
state.versions = v
emitter.emit('render')
}
})()
示例2: Error
// This function should really return an express / connect Router or
// something.
export default function handler<Val>(store: I.Store<Val>, optsIn?: HttpOpts) {
if (!bitHas(store.storeInfo.capabilities.queryTypes, I.QueryType.KV)) {
throw Error('Httpserver needs kv support')
}
const opts = Object.assign({}, defaultOpts, optsIn)
// const urlToKey = (opts && opts.urlToKey) ? opts.urlToKey : id
// const valToHttp = (opts && opts.valToHttp) ? opts.valToHttp : toJson
return async (req: http.IncomingMessage, res: http.ServerResponse) => {
const key = opts.urlToKey!(req.url!)
const result = await store.fetch({type: I.QueryType.KV, q: new Set([key])})
const value = result.results.get(key)
// console.log('key', key, 'value', value, result)
// TODO: Clean this up.
// res.setHeader('x-sc-version', JSON.stringify(result.versions))
// TODO: Add etag. Steal code from text demo for it.
const {mimeType, content} = opts.valToHttp!(value, key, req)
res.setHeader('content-type', mimeType)
res.writeHead(value == null ? 404 : 200)
res.end(content)
}
}
示例3: switch
const fetch: I.FetchFn<Val> = (query, opts = {}) => {
// console.log('lmdb fetch', query, opts)
if (!bitHas(capabilities.queryTypes, query.type)) return Promise.reject(new err.UnsupportedTypeError(`${query.type} not supported by lmdb store`))
const qops = queryTypes[query.type]
let bakedQuery: I.Query | undefined
const dbTxn = env.beginTxn({readOnly: true})
let results: I.ResultData<Val>
// KV txn. Query is a set of keys.
let maxVersion: I.Version
switch (query.type) {
case I.QueryType.KV:
results = new Map<I.Key, Val>()
maxVersion = getKVResults(dbTxn, query.q, opts, results)
break
case I.QueryType.AllKV:
results = new Map<I.Key, Val>()
maxVersion = getAllResults(dbTxn, opts, results)
break
case I.QueryType.Range:
bakedQuery = {type: I.QueryType.StaticRange, q: query.q.slice()}
case I.QueryType.StaticRange: {
const q = query.q as I.RangeQuery // | I.StaticRangeQuery
maxVersion = V_ZERO
results = q.map((range) => {
const staticRange = (query.type === I.QueryType.Range) ? bakeRange(dbTxn, range) : (range as I.StaticRange)
const docs = [] // A map might be nicer.
for (const [k, bytes] of rangeContentIter(dbTxn, staticRange)) {
const [lastMod, doc] = decode(bytes)
maxVersion = versionLib.vMax(maxVersion, lastMod)
docs.push([k, doc])
}
return docs
})
break
}
default: return Promise.reject(new err.UnsupportedTypeError(`${query.type} not supported by lmdb store`))
}
// = query.type === 'kv'
// ? getKVResults(dbTxn, query.q, opts, results)
// : getAllResults(dbTxn, opts, results)
dbTxn.abort()
// console.log('lmdb fetch', results, maxVersion, version)
return Promise.resolve({
bakedQuery,
results,
versions: [{from: maxVersion, to: version}]
})
}
示例4: async
const fetch: I.FetchFn<Val> = async (query, opts = {}) => {
if (!bitHas(capabilities.queryTypes, query.type)) throw new err.UnsupportedTypeError(`${query.type} not supported by lmdb store`)
const qops = queryTypes[query.type]
let bakedQuery: I.Query | undefined
let maxVersion: Uint8Array | null = null
let results: I.ResultData<Val>
const vs = await rawDb.doTn(async rawTn => {
// This could be way cleaner.
const tn = rawTn.scopedTo(rawDb.withValueEncoding({
pack() {throw Error('Cannot write')},
unpack: unpackVersion
}))
switch (query.type) {
case I.QueryType.KV: {
results = new Map<I.Key, Val>()
await Promise.all(Array.from(query.q).map(async k => {
const result = await tn.get(k)
if (result) {
const [stamp, value] = result
if (value != null) results.set(k, opts.noDocs ? 1 : value)
maxVersion = maxVersion ? versionLib.vMax(maxVersion, stamp) : stamp
}
}))
break
}
case I.QueryType.AllKV: {
// TODO: Make this work with larger databases (>5mb). Currently
// this is trying to fetch the whole db contents using a single
// txn, which will cause problems if the database is nontrivial.
// There's a few strategies we could implement here:
//
// - Lean on fdb's MVCC implementation and create a series of
// transactions at the same version
// - Use a series of transactions at incrementing versions, then
// fetch all the operations in the range and run catchup on any
// old data that was returned
// - Implement maxDocs and force the client to implement the
// iterative retry logic
// Whatever we do should also work on static ranges.
const resultsList = await tn.getRangeAll(START_KEY, END_KEY)
results = new Map<I.Key, Val>()
for (const [kbuf, [stamp, value]] of resultsList) {
results.set(kbuf.toString('utf8'), opts.noDocs ? 1 : value)
maxVersion = maxVersion ? versionLib.vMax(maxVersion, stamp) : stamp
}
break
}
case I.QueryType.StaticRange: {
// const q = query.q as I.RangeQuery
results = await Promise.all(query.q.map(async ({low, high, reverse}) => {
// This is so clean. Its almost like I designed statecraft with
// foundationdb in mind... ;)
return (await tn.getRangeAll(staticKStoFDBSel(low), staticKStoFDBSel(high), {reverse: reverse}))
.map(([kbuf, [stamp, value]]) => {
maxVersion = maxVersion ? versionLib.vMax(maxVersion, stamp) : stamp
return [kbuf.toString('utf8'), opts.noDocs ? 1 : value] as [I.Key, Val]
}) //.filter(([k, v]) => v != undefined)
}))
break
}
case I.QueryType.Range: {
// There's a bunch of ways I could write this, but a bit of copy + pasta is the simplest.
// bakedQuery = {type: 'static range', q:[]}
const baked: I.StaticRangeQuery = []
results = await Promise.all(query.q.map(async ({low, high, reverse, limit}, i) => {
// console.log('range results', (await tn.getRangeAll(kStoFDBSel(low), kStoFDBSel(high), {reverse: reverse, limit: limit})).length)
const vals = (await tn.getRangeAll(kStoFDBSel(low), kStoFDBSel(high), {reverse: reverse, limit: limit}))
.map(([kbuf, [stamp, value]]) => {
maxVersion = maxVersion ? versionLib.vMax(maxVersion, stamp) : stamp
// console.log('arr entry', [kbuf.toString('utf8'), value])
return [kbuf.toString('utf8'), opts.noDocs ? 1 : value] as [I.Key, Val]
}) //.filter(([k, v]) => v != undefined)
// Note: These aren't tested thoroughly yet. Probably a bit broken.
baked[i] = vals.length ? {
low: {k: reverse ? vals[vals.length-1][0] : vals[0][0], isAfter: !!reverse},
high: {k: reverse ? vals[0][0] : vals[vals.length-1][0], isAfter: !reverse},
reverse
} : {
low: {k: low.k, isAfter: low.isAfter},
high: {k: low.k, isAfter: low.isAfter},
reverse
}
// console.log('range ->', vals)
return vals
}))
// console.log('range query ->', results)
bakedQuery = {type: I.QueryType.StaticRange, q: baked}
break
}
//.........这里部分代码省略.........