Skip to content

Commit

Permalink
Code reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
subramanian-elavathur committed Nov 5, 2021
1 parent ac24c45 commit b3a1f1f
Show file tree
Hide file tree
Showing 4 changed files with 546 additions and 532 deletions.
87 changes: 87 additions & 0 deletions src/GlitchDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import tar = require("tar");
import { DEFAULT_CACHE_SIZE } from "./constants";
import GlitchPartitionImpl, {
GlitchPartition,
GlitchUnitemporallyVersionedPartition,
} from "./GlitchPartition";

export default class GlitchDB {
#baseDir: string;
#defaultCacheSize?: number;
#partitions: {
[key: string]: {
name: string;
cache: number;
versioned: boolean;
};
};

constructor(baseDir: string, defaultCacheSize?: number) {
this.#baseDir = baseDir;
this.#defaultCacheSize = defaultCacheSize ?? DEFAULT_CACHE_SIZE;
this.#partitions = {};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
getPartitionByName(name: string): GlitchPartition<any> {
if (name in this.#partitions) {
const partition = this.#partitions[name];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.getPartition<any>(partition.name, null, partition.cache);
}
throw new Error(`Glitch Partition with name ${name} not found`);
}

backup(outputDirectory: string): string {
const fileLocation = `${outputDirectory}/backup-${new Date().getTime()}.tgz`;
tar.create(
{
gzip: true,
sync: true,
cwd: this.#baseDir,
file: fileLocation,
},
["."]
);
return fileLocation;
}

getPartition<Type>(
name: string,
indices?: string[],
cacheSize?: number
): GlitchPartition<Type> {
const cacheSizeWithDefault = cacheSize ?? this.#defaultCacheSize;
this.#partitions[name] = {
name,
cache: cacheSizeWithDefault,
versioned: false,
};
return new GlitchPartitionImpl<Type>(
this,
`${this.#baseDir}/${name}`,
cacheSizeWithDefault,
indices
);
}

getVersionedPartition<Type>(
name: string,
indices?: string[],
cacheSize?: number
): GlitchUnitemporallyVersionedPartition<Type> {
const cacheSizeWithDefault = cacheSize ?? this.#defaultCacheSize;
this.#partitions[name] = {
name,
cache: cacheSizeWithDefault,
versioned: true,
};
return new GlitchPartitionImpl<Type>(
this,
`${this.#baseDir}/${name}`,
cacheSizeWithDefault,
indices,
true
);
}
}
Loading

0 comments on commit b3a1f1f

Please sign in to comment.