Skip to content
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

Refactor optimistic locking loop into an abstract base class #26

Merged
merged 1 commit into from
Oct 31, 2023
Merged
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
120 changes: 66 additions & 54 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@
dangerously?: boolean
}

abstract class BaseDynamoDBAutoIncrement {
constructor(readonly props: DynamoDBAutoIncrementProps) {}

protected abstract next(
item: Record<string, NativeAttributeValue>
): Promise<{ puts: PutCommandInput[]; nextCounter: number }>

async put(item: Record<string, NativeAttributeValue>) {
for (;;) {
const { puts, nextCounter } = await this.next(item)

if (this.props.dangerously) {
await Promise.all(puts.map((obj) => this.props.doc.put(obj)))
} else {
try {
await this.props.doc.transactWrite({
TransactItems: puts.map((Put) => ({ Put })),
})
} catch (e) {
if (e instanceof TransactionCanceledException) {
continue
} else {
throw e

Check warning on line 53 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L53

Added line #L53 was not covered by tests
}
}
}

return nextCounter
}
}
}

/**
* Update an auto-incrementing partition key in DynamoDB.
*
Expand Down Expand Up @@ -55,9 +87,7 @@
* })
* ```
*/
export class DynamoDBAutoIncrement {
constructor(readonly props: DynamoDBAutoIncrementProps) {}

export class DynamoDBAutoIncrement extends BaseDynamoDBAutoIncrement {
async #getLast(): Promise<number | undefined> {
return (
(
Expand All @@ -70,62 +100,44 @@
)
}

async put(item: Record<string, NativeAttributeValue>) {
for (;;) {
const counter = await this.#getLast()
protected async next(item: Record<string, NativeAttributeValue>) {
const counter = await this.#getLast()

let nextCounter, ConditionExpression, ExpressionAttributeValues
if (counter === undefined) {
nextCounter = this.props.initialValue
ConditionExpression = 'attribute_not_exists(#counter)'
} else {
nextCounter = counter + 1
ConditionExpression = '#counter = :counter'
ExpressionAttributeValues = {
':counter': counter,
}
let nextCounter, ConditionExpression, ExpressionAttributeValues
if (counter === undefined) {
nextCounter = this.props.initialValue
ConditionExpression = 'attribute_not_exists(#counter)'
} else {
nextCounter = counter + 1
ConditionExpression = '#counter = :counter'
ExpressionAttributeValues = {
':counter': counter,
}
}

const puts: PutCommandInput[] = [
{
ConditionExpression,
ExpressionAttributeNames: {
'#counter': this.props.counterTableAttributeName,
},
ExpressionAttributeValues,
Item: {
...this.props.counterTableKey,
[this.props.counterTableAttributeName]: nextCounter,
},
TableName: this.props.counterTableName,
const puts: PutCommandInput[] = [
{
ConditionExpression,
ExpressionAttributeNames: {
'#counter': this.props.counterTableAttributeName,
},
{
ConditionExpression: 'attribute_not_exists(#counter)',
ExpressionAttributeNames: {
'#counter': this.props.tableAttributeName,
},
Item: { [this.props.tableAttributeName]: nextCounter, ...item },
TableName: this.props.tableName,
ExpressionAttributeValues,
Item: {
...this.props.counterTableKey,
[this.props.counterTableAttributeName]: nextCounter,
},
]

if (this.props.dangerously) {
await Promise.all(puts.map((obj) => this.props.doc.put(obj)))
} else {
try {
await this.props.doc.transactWrite({
TransactItems: puts.map((Put) => ({ Put })),
})
} catch (e) {
if (e instanceof TransactionCanceledException) {
continue
} else {
throw e
}
}
}
TableName: this.props.counterTableName,
},
{
ConditionExpression: 'attribute_not_exists(#counter)',
ExpressionAttributeNames: {
'#counter': this.props.tableAttributeName,
},
Item: { [this.props.tableAttributeName]: nextCounter, ...item },
TableName: this.props.tableName,
},
]

return nextCounter
}
return { puts, nextCounter }
}
}
Loading