-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Account Data Indexing #507
Open
raghukapur9
wants to merge
5
commits into
FuelLabs:main
Choose a base branch
from
DevForce99:feat/accountDataIndexer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9173cf
Account Data Indexing
raghukapur9 760f3a0
Merge branch 'FuelLabs:main' into feat/accountDataIndexer
raghukapur9 6ea9887
removed AccountModel class
raghukapur9 924c399
Merge branch 'FuelLabs:main' into feat/accountDataIndexer
raghukapur9 e8bd42e
Account Balance Changes
shivam-25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE indexer.accounts ( | ||
_id SERIAL PRIMARY KEY, | ||
account_id character varying(66) NOT NULL UNIQUE, | ||
transaction_count INTEGER NOT NULL DEFAULT 0, | ||
data jsonb NOT NULL DEFAULT '{}', | ||
first_transaction_timestamp timestamp without time zone NOT NULL, | ||
recent_transaction_timestamp timestamp without time zone NOT NULL | ||
); | ||
CREATE UNIQUE INDEX ON indexer.accounts(_id); | ||
CREATE UNIQUE INDEX ON indexer.accounts(account_id); | ||
CREATE INDEX ON indexer.accounts(transaction_count); | ||
CREATE INDEX ON indexer.accounts(recent_transaction_timestamp); | ||
CREATE INDEX ON indexer.accounts(first_transaction_timestamp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Hash256 } from '../../application/vo/Hash256'; | ||
import { Entity } from '../../core/Entity'; | ||
import { AccountData } from './vo/AccountData'; | ||
import { AccountModelID } from './vo/AccountModelID'; | ||
|
||
type AccountInputProps = { | ||
account_id: Hash256; | ||
data: AccountData; | ||
transactionCount: number; | ||
}; | ||
|
||
export class AccountEntity extends Entity<AccountInputProps, AccountModelID> { | ||
// Adjust the constructor to not require an ID initially | ||
static create(account: any) { | ||
const account_id = Hash256.create(account.account_id); | ||
const data = AccountData.create(account.data); | ||
const transactionCount = account.transactionCount || 0; | ||
|
||
const props: AccountInputProps = { | ||
account_id, | ||
data, | ||
transactionCount, | ||
}; | ||
|
||
// If _id is not provided, set it as undefined | ||
const id = account._id ? AccountModelID.create(account._id) : undefined; | ||
|
||
return new AccountEntity(props, id); | ||
} | ||
|
||
static toDBItem(account: AccountEntity): any { | ||
return { | ||
account_id: account.props.account_id.value(), | ||
data: AccountEntity.serializeData(account.props.data.value()), | ||
transaction_count: account.props.transactionCount, | ||
}; | ||
} | ||
|
||
static serializeData(data: any): string { | ||
return JSON.stringify(data, (_, value) => | ||
typeof value === 'bigint' ? value.toString() : value, | ||
); | ||
} | ||
|
||
get cursor() { | ||
return this.id ? this.id.value() : null; | ||
} | ||
|
||
get id() { | ||
return this._id; | ||
} | ||
|
||
get account_id() { | ||
return this.props.account_id.value(); | ||
} | ||
|
||
get transactionCount() { | ||
return this.props.transactionCount; | ||
} | ||
|
||
get data() { | ||
return this.props.data.value(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { integer, jsonb, pgTable, serial, text } from 'drizzle-orm/pg-core'; | ||
|
||
// Define the schema for the accounts table | ||
export const AccountsTable = pgTable('accounts', { | ||
_id: serial('_id').primaryKey(), | ||
account_id: text('account_id').notNull().unique(), | ||
transaction_count: integer('transaction_count').notNull().default(0), | ||
data: jsonb('data').notNull().default({}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { jsonb } from 'drizzle-orm/pg-core'; | ||
import { ValueObject } from '../../../core/ValueObject'; | ||
|
||
interface Props { | ||
value: any; | ||
} | ||
|
||
export class AccountData extends ValueObject<Props> { | ||
private constructor(props: Props) { | ||
super(props); | ||
} | ||
|
||
static type() { | ||
return jsonb('data').notNull(); | ||
} | ||
|
||
static create(value: any) { | ||
return new AccountData({ value }); | ||
} | ||
|
||
value() { | ||
return this.props.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { integer } from 'drizzle-orm/pg-core'; | ||
import { Identifier } from '../../../core/Identifier'; | ||
|
||
export class AccountModelID extends Identifier<number> { | ||
private constructor(id: number) { | ||
super(id); | ||
} | ||
|
||
static type() { | ||
return integer('_id').primaryKey(); | ||
} | ||
|
||
static create(id: number): AccountModelID { | ||
if (typeof id !== 'number' || Number.isNaN(id)) { | ||
throw new Error('Invalid ID: ID must be a valid number.'); | ||
} | ||
|
||
return new AccountModelID(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ValueObject } from '../../../core/ValueObject'; | ||
interface Props { | ||
value: number; | ||
} | ||
|
||
export class AccountRef extends ValueObject<Props> { | ||
private constructor(props: Props) { | ||
super(props); | ||
} | ||
|
||
static create(id: number) { | ||
return new AccountRef({ value: id }); | ||
} | ||
|
||
value() { | ||
return this.props.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { AccountEntity } from '../../domain/Account/AccountEntity'; | ||
import { DatabaseConnection } from '../database/DatabaseConnection'; | ||
|
||
export class AccountDAO { | ||
private databaseConnection: DatabaseConnection; | ||
|
||
constructor() { | ||
this.databaseConnection = DatabaseConnection.getInstance(); | ||
} | ||
|
||
// Custom function to stringify BigInt values | ||
private stringifyBigInt(data: any): string { | ||
return JSON.stringify(data, (_key, value) => | ||
typeof value === 'bigint' ? value.toString() : value, | ||
); | ||
} | ||
|
||
async save(account: AccountEntity) { | ||
const accountData = AccountEntity.toDBItem(account); | ||
|
||
const data = this.stringifyBigInt(accountData.data); | ||
|
||
// Use raw SQL query to insert or update the account record | ||
await this.databaseConnection.query( | ||
` | ||
INSERT INTO indexer.accounts (account_id, transaction_count, data, first_transaction_timestamp, recent_transaction_timestamp) | ||
VALUES ($1, $2, $3, $4, $5) | ||
ON CONFLICT (account_id) | ||
DO UPDATE SET | ||
transaction_count = EXCLUDED.transaction_count, | ||
data = EXCLUDED.data, | ||
recent_transaction_timestamp = CASE | ||
WHEN accounts.transaction_count <> EXCLUDED.transaction_count THEN EXCLUDED.recent_transaction_timestamp | ||
ELSE accounts.recent_transaction_timestamp | ||
END | ||
`, | ||
[ | ||
accountData.account_id, | ||
accountData.transaction_count, | ||
data, | ||
accountData.first_transaction_timestamp || new Date().toISOString(), | ||
new Date().toISOString(), | ||
], | ||
); | ||
} | ||
|
||
async getAccountById(id: string): Promise<AccountEntity | null> { | ||
const result = await this.databaseConnection.query( | ||
` | ||
SELECT * FROM indexer.accounts WHERE account_id = $1 | ||
`, | ||
[id], | ||
); | ||
|
||
return result.length ? AccountEntity.create(result[0]) : null; | ||
} | ||
|
||
async incrementTransactionCount(account_id: string, incrementBy = 1) { | ||
await this.databaseConnection.query( | ||
` | ||
UPDATE indexer.accounts | ||
SET transaction_count = transaction_count + $1, | ||
recent_transaction_timestamp = $2 | ||
WHERE account_id = $3 | ||
`, | ||
[incrementBy, new Date().toISOString(), account_id], | ||
); | ||
} | ||
|
||
// Updated method to update account data with BigInt handling | ||
async updateAccountData(account_id: string, newData: any) { | ||
const data = this.stringifyBigInt(newData); // Use custom function for BigInt serialization | ||
|
||
await this.databaseConnection.query( | ||
` | ||
UPDATE indexer.accounts | ||
SET data = $1, | ||
recent_transaction_timestamp = $2 | ||
WHERE account_id = $3 | ||
`, | ||
[data, new Date().toISOString(), account_id], | ||
); | ||
} | ||
|
||
async updateAccountTransactionCount( | ||
account_id: string, | ||
newTransactionCount: number, | ||
) { | ||
await this.databaseConnection.query( | ||
` | ||
UPDATE indexer.accounts | ||
SET transaction_count = $1, | ||
recent_transaction_timestamp = $2 | ||
WHERE account_id = $3 | ||
`, | ||
[newTransactionCount, new Date().toISOString(), account_id], | ||
); | ||
} | ||
|
||
// New method to get account data content | ||
async getAccountDataContent(account_id: string): Promise<any | null> { | ||
const result = await this.databaseConnection.query( | ||
` | ||
SELECT data FROM indexer.accounts WHERE account_id = $1 | ||
`, | ||
[account_id], | ||
); | ||
|
||
return result.length ? result[0].data : null; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are not using Drizzle anymore, don't need to create the mapping classes