Updates
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const HyperP2PTemporalIndex = require('../index.js')
|
||||
const crypto = require('bare-crypto')
|
||||
const b4a = require('b4a')
|
||||
|
||||
async function main () {
|
||||
console.log('hyper-p2p-temporal-index basic usage demo')
|
||||
|
||||
const index = new HyperP2PTemporalIndex({
|
||||
localId: 'demo-peer-001',
|
||||
defaultTtlMs: 1000 * 60 * 60 * 24, // 1 day
|
||||
enableSigning: true
|
||||
})
|
||||
|
||||
// Insert several events across time
|
||||
const now = Date.now()
|
||||
const ev1 = await index.insertEvent({ type: 'metric', cpu: 45 }, { timestamp: now - 3600000 })
|
||||
const ev2 = await index.insertEvent({ type: 'metric', cpu: 78 }, { timestamp: now - 1800000 })
|
||||
const ev3 = await index.insertEvent({ type: 'alert', msg: 'high load' }, { timestamp: now })
|
||||
|
||||
console.log('Inserted 3 events')
|
||||
|
||||
// Range query last 2 hours
|
||||
const recent = await index.queryRange(now - 7200000, now + 1000, { limit: 50 })
|
||||
console.log('Recent events:', recent.length)
|
||||
recent.forEach(e => console.log(' -', new Date(e.timestamp).toISOString(), e.data))
|
||||
|
||||
// Nearest before now
|
||||
const nearest = await index.queryNearest(now - 100000, { direction: 'before' })
|
||||
console.log('Nearest before:', nearest ? nearest.data : null)
|
||||
|
||||
// Metrics
|
||||
console.log('Metrics:', index.getMetrics())
|
||||
|
||||
// Topic for P2P
|
||||
const topic = index.deriveTopic()
|
||||
console.log('Hyperswarm topic (hex):', b4a.toString(topic, 'hex').slice(0, 16) + '...')
|
||||
|
||||
// Simulate integration with vector clock (mock)
|
||||
const mockVC = { tick: () => ({ demo: 1 }), compare: () => 0 }
|
||||
index.setVectorClock(mockVC)
|
||||
|
||||
await index.insertEvent({ integrated: true })
|
||||
console.log('Integrated vector clock event')
|
||||
|
||||
await index.close()
|
||||
console.log('Demo complete')
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
Reference in New Issue
Block a user