Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
*/
export class BTMSStorageManager {
private readonly records: Collection<BTMSRecord>
private indexInit?: Promise<void>

/**
* @param db A connected MongoDB database handle.
*/
constructor (private readonly db: Db) {
this.records = db.collection<BTMSRecord>('btmsRecords')
}

// Create index on assetId for efficient lookups
this.records
.createIndex({ assetId: 1 })
.catch(console.error)

// Create index on ownerKey for efficient lookups
this.records
.createIndex({ ownerKey: 1 })
.catch(console.error)

// Create compound index for txid and outputIndex (unique identifier)
this.records
.createIndex({ txid: 1, outputIndex: 1 }, { unique: true })
.catch(console.error)
private ensureIndexes (): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await Promise.all([
this.records.createIndex({ assetId: 1 }),
this.records.createIndex({ ownerKey: 1 }),
this.records.createIndex({ txid: 1, outputIndex: 1 }, { unique: true })
])
})()
}

Check warning on line 28 in packages/overlays/btms-backend/src/lookup-services/BTMSStorageManager.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxkwmG8B6jx0f3VS5&open=AZ4FxkwmG8B6jx0f3VS5&pullRequest=104
return this.indexInit
}

/**
Expand All @@ -41,6 +40,7 @@
ownerKey: PubKeyHex,
metadata?: string
): Promise<void> {
await this.ensureIndexes()
const record: BTMSRecord = {
txid,
outputIndex,
Expand All @@ -57,6 +57,7 @@
* Remove a BTMS record identified by its UTXO.
*/
async deleteRecord (txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

Expand All @@ -72,6 +73,7 @@
skip: number = 0,
sortOrder: 'asc' | 'desc' = 'desc'
): Promise<BTMSRecord[]> {
await this.ensureIndexes()
const query: Record<string, unknown> = {}

if (filters.assetId) {
Expand All @@ -93,13 +95,15 @@
skip: number = 0,
sortOrder: 'asc' | 'desc' = 'desc'
): Promise<BTMSRecord[]> {
await this.ensureIndexes()
return await this.findRecordWithQuery({}, limit, skip, sortOrder)
}

/**
* Find a specific record by txid and outputIndex.
*/
async findByTxidOutputIndex (txid: string, outputIndex: number): Promise<BTMSRecord | null> {
await this.ensureIndexes()
return await this.records.findOne({ txid, outputIndex })
}

Expand Down
16 changes: 13 additions & 3 deletions packages/overlays/topics/src/any/AnyStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
// Implements a Lookup StorageEngine for Any
export class AnyStorage {
private readonly records: Collection<AnyRecord>
private indexInit?: Promise<void>

constructor(private readonly db: Db) {
this.records = db.collection<AnyRecord>('anyRecords')
this.createSearchableIndex()
}

private async createSearchableIndex(): Promise<void> {
await this.records.createIndex({ txid: 1 }, { name: 'txidIndex' })
private ensureIndexes(): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await this.records.createIndex({ txid: 1 }, { name: 'txidIndex' })
})()
}

Check warning on line 18 in packages/overlays/topics/src/any/AnyStorage.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxkwSG8B6jx0f3VS4&open=AZ4FxkwSG8B6jx0f3VS4&pullRequest=104
return this.indexInit
}

async storeRecord(txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.insertOne({
txid,
outputIndex,
Expand All @@ -23,14 +29,17 @@
}

async spendRecord(txid: string, outputIndex: number, spendingTxid: string): Promise<void> {
await this.ensureIndexes()
await this.records.updateOne({ txid, outputIndex }, { $set: { spendingTxid } })
}

async deleteRecord(txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

async findByTxid(txid: string): Promise<UTXOReference | null> {
await this.ensureIndexes()
if (!txid) return null
return this.records.findOne(
{ txid },
Expand All @@ -45,6 +54,7 @@
endDate?: Date,
sortOrder: 'asc' | 'desc' = 'desc'
): Promise<UTXOReference[]> {
await this.ensureIndexes()
const query: any = {}
if (startDate || endDate) {
query.createdAt = {}
Expand Down
30 changes: 24 additions & 6 deletions packages/overlays/topics/src/apps/AppsStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,73 @@

export class AppsStorageManager {
private readonly records: Collection<AppCatalogRecord>
private indexInit?: Promise<void>

constructor(private readonly db: Db) {
this.records = db.collection<AppCatalogRecord>('appsCatalogRecords')
this.records.createIndex({
'metadata.name': 'text',
'metadata.description': 'text',
'metadata.tags': 'text',
'metadata.domain': 'text'
}).catch(console.error)
}

private ensureIndexes(): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await this.records.createIndex({
'metadata.name': 'text',
'metadata.description': 'text',
'metadata.tags': 'text',
'metadata.domain': 'text'
})
})()
}

Check warning on line 23 in packages/overlays/topics/src/apps/AppsStorageManager.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxkuoG8B6jx0f3VSy&open=AZ4FxkuoG8B6jx0f3VSy&pullRequest=104
return this.indexInit
}

async storeRecord(txid: string, outputIndex: number, metadata: PublishedAppMetadata): Promise<void> {
await this.ensureIndexes()
await this.records.insertOne({ txid, outputIndex, metadata, createdAt: new Date() })
}

async deleteRecord(txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

async findByDomain(domain: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
return this.findRecordWithQuery({ 'metadata.domain': domain }, limit, skip, sortOrder)
}

async findByPublisher(publisher: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
return this.findRecordWithQuery({ 'metadata.publisher': publisher }, limit, skip, sortOrder)
}

async findByOutpoint(outpoint: string): Promise<LookupFormula> {
await this.ensureIndexes()
const [txid, indexStr] = outpoint.split('.')
const outputIndex = Number(indexStr)
if (!txid || Number.isNaN(outputIndex)) throw new Error('Invalid outpoint format')
return this.findRecordWithQuery({ txid, outputIndex }, 1, 0, 'desc')
}

async findByNameFuzzy(partialName: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
const escaped = partialName.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`)
const regex = new RegExp(escaped, 'i')
return this.findRecordWithQuery({ 'metadata.name': { $regex: regex } }, limit, skip, sortOrder)
}

async findByTags(tags: string[], limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
return this.findRecordWithQuery({ 'metadata.tags': { $in: tags } }, limit, skip, sortOrder)
}

async findByCategory(category: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
return this.findRecordWithQuery({ 'metadata.category': category }, limit, skip, sortOrder)
}

async findAllApps(limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<LookupFormula> {
await this.ensureIndexes()
return this.findRecordWithQuery({}, limit, skip, sortOrder)
}

Expand Down
32 changes: 18 additions & 14 deletions packages/overlays/topics/src/btms/BTMSStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
*/
export class BTMSStorageManager {
private readonly records: Collection<BTMSRecord>
private indexInit?: Promise<void>

/**
* @param db A connected MongoDB database handle.
*/
constructor (private readonly db: Db) {
this.records = db.collection<BTMSRecord>('btmsRecords')
}

// Create index on assetId for efficient lookups
this.records
.createIndex({ assetId: 1 })
.catch(console.error)

// Create index on ownerKey for efficient lookups
this.records
.createIndex({ ownerKey: 1 })
.catch(console.error)

// Create compound index for txid and outputIndex (unique identifier)
this.records
.createIndex({ txid: 1, outputIndex: 1 }, { unique: true })
.catch(console.error)
private ensureIndexes (): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await Promise.all([
this.records.createIndex({ assetId: 1 }),
this.records.createIndex({ ownerKey: 1 }),
this.records.createIndex({ txid: 1, outputIndex: 1 }, { unique: true })
])
})()
}

Check warning on line 28 in packages/overlays/topics/src/btms/BTMSStorageManager.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxkpWG8B6jx0f3VSs&open=AZ4FxkpWG8B6jx0f3VSs&pullRequest=104
return this.indexInit
}

/**
Expand All @@ -41,6 +40,7 @@
ownerKey: PubKeyHex,
metadata?: string
): Promise<void> {
await this.ensureIndexes()
const record: BTMSRecord = {
txid,
outputIndex,
Expand All @@ -57,6 +57,7 @@
* Remove a BTMS record identified by its UTXO.
*/
async deleteRecord (txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

Expand All @@ -72,6 +73,7 @@
skip: number = 0,
sortOrder: 'asc' | 'desc' = 'desc'
): Promise<BTMSRecord[]> {
await this.ensureIndexes()
const query: Record<string, unknown> = {}

if (filters.assetId) {
Expand All @@ -93,13 +95,15 @@
skip: number = 0,
sortOrder: 'asc' | 'desc' = 'desc'
): Promise<BTMSRecord[]> {
await this.ensureIndexes()
return await this.findRecordWithQuery({}, limit, skip, sortOrder)
}

/**
* Find a specific record by txid and outputIndex.
*/
async findByTxidOutputIndex (txid: string, outputIndex: number): Promise<BTMSRecord | null> {
await this.ensureIndexes()
return await this.records.findOne({ txid, outputIndex })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@

export class DesktopIntegrityStorage {
private readonly records: Collection<DesktopIntegrityRecord>
private indexInit?: Promise<void>

constructor(private readonly db: Db) {
this.records = db.collection<DesktopIntegrityRecord>('desktopIntegrityRecords')
this.createSearchableIndex()
}

private async createSearchableIndex(): Promise<void> {
await this.records.createIndex({ fileHash: 1 }, { name: 'fileHashIndex' })
private ensureIndexes(): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await this.records.createIndex({ fileHash: 1 }, { name: 'fileHashIndex' })
})()
}

Check warning on line 17 in packages/overlays/topics/src/desktopintegrity/DesktopIntegrityStorage.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxktiG8B6jx0f3VSu&open=AZ4FxktiG8B6jx0f3VSu&pullRequest=104
return this.indexInit
}

async storeRecord(txid: string, outputIndex: number, fileHash: string): Promise<void> {
await this.ensureIndexes()
await this.records.insertOne({ txid, outputIndex, fileHash, createdAt: new Date() })
}

async deleteRecord(txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

async findByFileHash(fileHash: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<UTXOReference[]> {
await this.ensureIndexes()
if (!fileHash) return []
const direction = sortOrder === 'asc' ? 1 : -1
const results = await this.records.find({ fileHash })
Expand All @@ -34,6 +42,7 @@
}

async findByTxid(txid: string, limit = 50, skip = 0, sortOrder: 'asc' | 'desc' = 'desc'): Promise<UTXOReference[]> {
await this.ensureIndexes()
if (!txid) return []
const direction = sortOrder === 'asc' ? 1 : -1
const results = await this.records.find({ txid })
Expand All @@ -46,6 +55,7 @@
}

async findAll(limit = 50, skip = 0, startDate?: Date, endDate?: Date, sortOrder: 'asc' | 'desc' = 'desc'): Promise<UTXOReference[]> {
await this.ensureIndexes()
const query: any = {}
if (startDate || endDate) {
query.createdAt = {}
Expand Down
15 changes: 14 additions & 1 deletion packages/overlays/topics/src/did/DIDStorageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@

export class DIDStorageManager {
private readonly records: Collection<DIDRecord>
private indexInit?: Promise<void>

constructor(private readonly db: Db) {
this.records = db.collection<DIDRecord>('didRecords')
this.records.createIndex({ searchableAttributes: 'text' }).catch((e) => console.error(e))
}

private ensureIndexes(): Promise<void> {
if (this.indexInit === undefined) {
this.indexInit = (async () => {
await this.records.createIndex({ searchableAttributes: 'text' })
})()
}

Check warning on line 19 in packages/overlays/topics/src/did/DIDStorageManager.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read.

See more on https://sonarcloud.io/project/issues?id=bsv-blockchain_ts-stack&issues=AZ4FxkvwG8B6jx0f3VS2&open=AZ4FxkvwG8B6jx0f3VS2&pullRequest=104
return this.indexInit
}

async storeRecord(txid: string, outputIndex: number, serialNumber: Base64String): Promise<void> {
await this.ensureIndexes()
await this.records.insertOne({ txid, outputIndex, serialNumber, createdAt: new Date() })
}

async deleteRecord(txid: string, outputIndex: number): Promise<void> {
await this.ensureIndexes()
await this.records.deleteOne({ txid, outputIndex })
}

async findByCertificateSerialNumber(serialNumber: Base64String): Promise<LookupFormula> {
await this.ensureIndexes()
return await this.findRecordWithQuery({ serialNumber })
}

async findByOutpoint(outpoint: string): Promise<LookupFormula> {
await this.ensureIndexes()
const [txid, outputIndexStr] = outpoint.split('.')
const outputIndex = Number.parseInt(outputIndexStr, 10)
if (!txid || Number.isNaN(outputIndex)) {
Expand Down
Loading
Loading