-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add setHeadData, getHeadData, and autoIncrement methods due to …
…issue #1 (comment)
- Loading branch information
Showing
9 changed files
with
209 additions
and
137 deletions.
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
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
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 |
---|---|---|
@@ -1,48 +1,65 @@ | ||
import { BPTreeNode } from './base/BPTree' | ||
import { SerializeStrategy, SerializeStrategyHead } from './base/SerializeStrategy' | ||
import { Json } from './utils/types' | ||
|
||
export abstract class SerializeStrategyAsync<K, V> extends SerializeStrategy<K, V> { | ||
abstract id(): Promise<number> | ||
abstract read(id: number): Promise<BPTreeNode<K, V>> | ||
abstract write(id: number, node: BPTreeNode<K, V>): Promise<void> | ||
abstract readHead(): Promise<SerializeStrategyHead|null> | ||
abstract writeHead(head: SerializeStrategyHead): Promise<void> | ||
|
||
async getHeadData(key: string, defaultValue: Json): Promise<Json> { | ||
if (!Object.hasOwn(this.head.data, key)) { | ||
return defaultValue | ||
} | ||
return this.head.data[key] | ||
} | ||
|
||
async setHeadData(key: string, data: Json): Promise<void> { | ||
this.head.data[key] = data | ||
await this.writeHead(this.head) | ||
} | ||
|
||
async autoIncrement(key: string, defaultValue: number): Promise<number> { | ||
const current = await this.getHeadData(key, defaultValue) as number | ||
const next = current+1 | ||
await this.setHeadData(key, next) | ||
return current | ||
} | ||
} | ||
|
||
export class InMemoryStoreStrategyAsync<K, V> extends SerializeStrategyAsync<K, V> { | ||
protected readonly data: { | ||
head: SerializeStrategyHead|null, | ||
node: Record<number, BPTreeNode<K, V>> | ||
} | ||
protected readonly node: Record<number, BPTreeNode<K, V>> | ||
|
||
constructor(order: number) { | ||
super(order) | ||
this.data = { | ||
head: null, | ||
node: {} | ||
} | ||
this.node = {} | ||
} | ||
|
||
async id(): Promise<number> { | ||
return Math.ceil(Math.random()*Number.MAX_SAFE_INTEGER-1) | ||
return await this.autoIncrement('index', 1) | ||
} | ||
|
||
async read(id: number): Promise<BPTreeNode<K, V>> { | ||
if (!Object.prototype.hasOwnProperty.call(this.data.node, id)) { | ||
if (!Object.hasOwn(this.node, id)) { | ||
throw new Error(`The tree attempted to reference node '${id}', but couldn't find the corresponding node.`) | ||
} | ||
return this.data.node[id] | ||
return this.node[id] as BPTreeNode<K, V> | ||
} | ||
|
||
async write(id: number, node: BPTreeNode<K, V>): Promise<void> { | ||
this.data.node[id] = node | ||
this.node[id] = node | ||
} | ||
|
||
async readHead(): Promise<SerializeStrategyHead|null> { | ||
return this.data.head | ||
if (this.head.root === 0) { | ||
return null | ||
} | ||
return this.head | ||
} | ||
|
||
async writeHead(head: SerializeStrategyHead): Promise<void> { | ||
this.data.head = head | ||
(this as any).head = head | ||
} | ||
} |
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 |
---|---|---|
@@ -1,48 +1,65 @@ | ||
import { BPTreeNode } from './base/BPTree' | ||
import { SerializeStrategy, SerializeStrategyHead } from './base/SerializeStrategy' | ||
import { Json } from './utils/types' | ||
|
||
export abstract class SerializeStrategySync<K, V> extends SerializeStrategy<K, V> { | ||
abstract id(): number | ||
abstract read(id: number): BPTreeNode<K, V> | ||
abstract write(id: number, node: BPTreeNode<K, V>): void | ||
abstract readHead(): SerializeStrategyHead|null | ||
abstract writeHead(head: SerializeStrategyHead): void | ||
|
||
getHeadData(key: string, defaultValue: Json): Json { | ||
if (!Object.hasOwn(this.head.data, key)) { | ||
return defaultValue | ||
} | ||
return this.head.data[key] | ||
} | ||
|
||
setHeadData(key: string, data: Json): void { | ||
this.head.data[key] = data | ||
this.writeHead(this.head) | ||
} | ||
|
||
autoIncrement(key: string, defaultValue: number): number { | ||
const current = this.getHeadData(key, defaultValue) as number | ||
const next = current+1 | ||
this.setHeadData(key, next) | ||
return current | ||
} | ||
} | ||
|
||
export class InMemoryStoreStrategySync<K, V> extends SerializeStrategySync<K, V> { | ||
protected readonly data: { | ||
head: SerializeStrategyHead|null, | ||
node: Record<number, BPTreeNode<K, V>> | ||
} | ||
protected readonly node: Record<number, BPTreeNode<K, V>> | ||
|
||
constructor(order: number) { | ||
super(order) | ||
this.data = { | ||
head: null, | ||
node: {} | ||
} | ||
this.node = {} | ||
} | ||
|
||
id(): number { | ||
return Math.ceil(Math.random()*Number.MAX_SAFE_INTEGER-1) | ||
return this.autoIncrement('index', 1) | ||
} | ||
|
||
read(id: number): BPTreeNode<K, V> { | ||
if (!Object.prototype.hasOwnProperty.call(this.data.node, id)) { | ||
if (!Object.hasOwn(this.node, id)) { | ||
throw new Error(`The tree attempted to reference node '${id}', but couldn't find the corresponding node.`) | ||
} | ||
return this.data.node[id] | ||
return this.node[id] as BPTreeNode<K, V> | ||
} | ||
|
||
write(id: number, node: BPTreeNode<K, V>): void { | ||
this.data.node[id] = node | ||
this.node[id] = node | ||
} | ||
|
||
readHead(): SerializeStrategyHead|null { | ||
return this.data.head | ||
if (this.head.root === 0) { | ||
return null | ||
} | ||
return this.head | ||
} | ||
|
||
writeHead(head: SerializeStrategyHead): void { | ||
this.data.head = head | ||
(this as any).head = head | ||
} | ||
} |
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
Oops, something went wrong.