Skip to content

Commit

Permalink
refactor: Simplify bucket handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smessie committed Oct 14, 2024
1 parent c0abf12 commit e5d6bb0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type Record = {

export type Bucket = {
id: string;
stream: string;
streamId: string;
immutable?: boolean;
root?: boolean;
empty?: boolean;
Expand Down
21 changes: 1 addition & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,7 @@ async function handleBuckets(
const buckets = extract.getBuckets();

for (const bucket of buckets) {
const set: {
immutable?: boolean;
root?: boolean;
empty?: boolean;
members?: string[];
} = {};
if (bucket.root !== undefined) {
set.root = bucket.root;
}

if (bucket.empty !== undefined) {
set.empty = bucket.empty;
set.members = [];
}

if (bucket.immutable !== undefined) {
set.immutable = bucket.immutable;
}

await repository.handleBucket(bucket, set, operations);
await repository.handleBucket(bucket, operations);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/repositories/MongoDBRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,20 @@ export class MongoDBRepository implements Repository {

async handleBucket(
bucket: Bucket,
parameters: any,
bulk: AnyBulkWriteOperation<TREEFragment>[],
): Promise<void> {
if (bucket.empty) {
(bucket as TREEFragment).members = [];
delete bucket.empty;
}
bulk.push({
updateOne: {
filter: {
streamId: bucket.stream,
streamId: bucket.streamId,
id: bucket.id,
},
update: {
$set: parameters,
$set: bucket,
},
upsert: true,
},
Expand Down
19 changes: 5 additions & 14 deletions src/repositories/RedisRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,22 @@ export class RedisRepository implements Repository {

async handleBucket(
bucket: Bucket,
parameters: any,
bulk: Promise<string | number | null>[],
): Promise<void> {
// If parameters contains `members`, use the handleMember method and remove the key.
if (parameters.members || parameters.empty) {
// If bucket contains `empty`, remove the members set
if (bucket.empty) {
bulk.push(
this.client.del(
`${this.index}:${encodeURIComponent(bucket.stream)}:${encodeURIComponent(bucket.id)}:members`,
`${this.index}:${encodeURIComponent(bucket.streamId)}:${encodeURIComponent(bucket.id)}:members`,
),
);
for (const member of parameters.members) {
await this.handleMember(member, bucket.id, bulk);
}
delete parameters.members;
}

// Set streamId and id parameters
parameters.streamId = bucket.stream;
parameters.id = bucket.id;

bulk.push(
this.client.json.set(
`${this.index}:${encodeURIComponent(bucket.stream)}:${encodeURIComponent(bucket.id)}`,
`${this.index}:${encodeURIComponent(bucket.streamId)}:${encodeURIComponent(bucket.id)}`,
".",
parameters,
bucket,
),
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/repositories/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface Repository {

handleBucket(
bucket: Bucket,
parameters: any,
bulk: IndexBulkOperations,
): Promise<void>;

Expand All @@ -64,14 +63,14 @@ export function getRepository(dbConfig: DBConfig): Repository {
const url =
dbConfig.url || env.DB_CONN_STRING || "mongodb://localhost:27017/ldes";

if (url.startsWith("mongodb://")) {
if (url.startsWith("mongodb")) {
return new MongoDBRepository(
url,
dbConfig.metadata,
dbConfig.data,
dbConfig.index,
);
} else if (url.startsWith("redis://")) {
} else if (url.startsWith("redis")) {
return new RedisRepository(
url,
dbConfig.metadata,
Expand Down
2 changes: 1 addition & 1 deletion src/shape.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "stream";
sh:name "streamId";
sh:path sds:stream;
sh:datatype xsd:string;
sh:minCount 1;
Expand Down
2 changes: 1 addition & 1 deletion test/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ _:n3-26 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://qudt.org/1.1/

const buckets = extract.getBuckets();
expect(buckets.length).toBe(4);
expect(buckets.map((x) => x.stream)).toEqual([
expect(buckets.map((x) => x.streamId)).toEqual([
"https://example.org/ns#",
"https://example.org/ns#",
"https://example.org/ns#",
Expand Down

0 comments on commit e5d6bb0

Please sign in to comment.