Standardise database/dataset accessors as database() and dataset() (#1607)

In JS they are either set() or store(). In Go they are either Dataset()
Store(), or Database().
This commit is contained in:
Ben Kalman
2016-05-24 10:25:16 -07:00
parent 67c43db4de
commit e34551cd54
22 changed files with 96 additions and 96 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@attic/noms",
"version": "38.0.0",
"version": "39.0.0",
"description": "Noms JS SDK",
"repository": "https://github.com/attic-labs/noms",
"main": "dist/commonjs/noms.js",
+2 -2
View File
@@ -148,11 +148,11 @@ export default class Database {
}
}
async function getAncestors(commits: Set<Ref<Commit>>, store: Database):
async function getAncestors(commits: Set<Ref<Commit>>, database: Database):
Promise<Set<Ref<Commit>>> {
let ancestors = new Set();
await commits.map(async (commitRef) => {
const commit = await store.readValue(commitRef.targetHash);
const commit = await database.readValue(commitRef.targetHash);
await commit.parents.map(async (ref) => ancestors = await ancestors.insert(ref));
});
return ancestors;
+9 -9
View File
@@ -9,19 +9,19 @@ import Set from './set.js';
const idRe = /^[a-zA-Z0-9\-_/]+$/;
export default class Dataset {
_store: Database;
_database: Database;
_id: string;
constructor(store: Database, id: string) {
constructor(database: Database, id: string) {
if (!idRe.test(id)) {
throw new TypeError(`Invalid dataset ID: ${id}`);
}
this._store = store;
this._database = database;
this._id = id;
}
get store(): Database {
return this._store;
get database(): Database {
return this._database;
}
get id(): string {
@@ -29,11 +29,11 @@ export default class Dataset {
}
headRef(): Promise<?Ref<Commit>> {
return this._store.headRef(this._id);
return this._database.headRef(this._id);
}
head(): Promise<?Commit> {
return this._store.head(this._id);
return this._database.head(this._id);
}
// Commit updates the commit that a dataset points at. If parents is provided then an the promise
@@ -45,7 +45,7 @@ export default class Dataset {
parents = headRef ? [headRef] : [];
}
const commit = new Commit(v, new Set(parents));
const store = await this._store.commit(this._id, commit);
return new Dataset(store, this._id);
const database = await this._database.commit(this._id, commit);
return new Dataset(database, this._id);
}
}
+22 -22
View File
@@ -19,20 +19,20 @@ suite('Specs', () => {
invariant(spec);
assert.equal(spec.scheme, 'mem');
assert.equal(spec.path, '');
let store = spec.store();
assert.instanceOf(store, Database);
assert.instanceOf(store._vs._bs, BatchStoreAdaptor);
await store.close();
let database = spec.database();
assert.instanceOf(database, Database);
assert.instanceOf(database._vs._bs, BatchStoreAdaptor);
await database.close();
spec = DatabaseSpec.parse('http://foo');
invariant(spec);
assert.isNotNull(spec);
assert.equal(spec.scheme, 'http');
assert.equal(spec.path, '//foo');
store = spec.store();
assert.instanceOf(store, Database);
assert.instanceOf(store._vs._bs, HttpBatchStore);
await store.close();
database = spec.database();
assert.instanceOf(database, Database);
assert.instanceOf(database._vs._bs, HttpBatchStore);
await database.close();
spec = DatabaseSpec.parse('https://foo');
invariant(spec);
@@ -59,22 +59,22 @@ suite('Specs', () => {
let spec = DatasetSpec.parse('mem:ds');
invariant(spec);
assert.equal(spec.name, 'ds');
assert.equal(spec.store.scheme, 'mem');
assert.equal(spec.store.path, '');
let ds = spec.set();
assert.equal(spec.database.scheme, 'mem');
assert.equal(spec.database.path, '');
let ds = spec.dataset();
assert.instanceOf(ds, Dataset);
assert.instanceOf(ds.store._vs._bs, BatchStoreAdaptor);
await ds.store.close();
assert.instanceOf(ds.database._vs._bs, BatchStoreAdaptor);
await ds.database.close();
spec = DatasetSpec.parse('http://localhost:8000/foo:ds');
invariant(spec);
assert.equal(spec.name, 'ds');
assert.equal(spec.store.scheme, 'http');
assert.equal(spec.store.path, '//localhost:8000/foo');
ds = spec.set();
assert.equal(spec.database.scheme, 'http');
assert.equal(spec.database.path, '//localhost:8000/foo');
ds = spec.dataset();
assert.instanceOf(ds, Dataset);
assert.instanceOf(ds.store._vs._bs, HttpBatchStore);
await ds.store.close();
assert.instanceOf(ds.database._vs._bs, HttpBatchStore);
await ds.database.close();
});
test('HashSpec', async () => {
@@ -89,8 +89,8 @@ suite('Specs', () => {
const spec = HashSpec.parse(`mem:${testHash}`);
invariant(spec);
assert.equal(spec.hash.toString(), testHash.toString());
assert.equal(spec.store.scheme, 'mem');
assert.equal(spec.store.path, '');
assert.equal(spec.database.scheme, 'mem');
assert.equal(spec.database.path, '');
});
test('ObjectSpec', () => {
@@ -99,8 +99,8 @@ suite('Specs', () => {
assert.isNotNull(spec.value());
invariant(spec instanceof DatasetSpec);
assert.equal(spec.name, 'monkey');
assert.equal(spec.store.scheme, 'http');
assert.equal(spec.store.path, '//foo:8000/test');
assert.equal(spec.database.scheme, 'http');
assert.equal(spec.database.path, '//foo:8000/test');
const testHash = new Hash('sha1-0000000000000000000000000000000000000000');
spec = parseObjectSpec(`http://foo:8000/test:${testHash}`);
+20 -20
View File
@@ -47,7 +47,7 @@ export class DatabaseSpec {
}
// Constructs a new Database based on the parsed spec.
store(): Database {
database(): Database {
if (this.scheme === 'mem') {
return new Database(new BatchStoreAdaptor(new MemoryStore()));
}
@@ -64,7 +64,7 @@ export class DatabaseSpec {
// See "spelling datasets" for details on supported syntaxes:
// https://docs.google.com/document/d/1QgKcRS304llwU0ECahKtn8lGBFmT5zXzWr-5tah1S_4/edit
export class DatasetSpec {
store: DatabaseSpec;
database: DatabaseSpec;
name: string;
// Returns a parsed spec, or null if the spec was invalid.
@@ -73,28 +73,28 @@ export class DatasetSpec {
if (!match) {
return null;
}
const store = DatabaseSpec.parse(match[1]);
if (!store) {
const database = DatabaseSpec.parse(match[1]);
if (!database) {
return null;
}
return new this(store, match[2]);
return new this(database, match[2]);
}
constructor(store: DatabaseSpec, name: string) {
this.store = store;
constructor(database: DatabaseSpec, name: string) {
this.database = database;
this.name = name;
}
// Returns a new DataSet based on the parsed spec.
set(): Dataset {
return new Dataset(this.store.store(), this.name);
dataset(): Dataset {
return new Dataset(this.database.database(), this.name);
}
// Returns the value at the HEAD of this dataset, if any, or null otherwise.
value(): Promise<any> {
// Hm. Calling set() creates a Database that we then toss into the ether, which means we can't
// call close() on it. Ideally, we'd fix that.
return this.set().head()
// Hm. Calling dataset() creates a Database that we then toss into the ether, which means we
// can't call close() on it. Ideally, we'd fix that.
return this.dataset().head()
.then(commit => commit && commit.value);
}
}
@@ -107,7 +107,7 @@ export class DatasetSpec {
// See "spelling objects" for details on supported syntaxes:
// https://docs.google.com/document/d/1QgKcRS304llwU0ECahKtn8lGBFmT5zXzWr-5tah1S_4/edit
export class HashSpec {
store: DatabaseSpec;
database: DatabaseSpec;
hash: Hash;
// Returns a parsed spec, or null if the spec was invalid.
@@ -122,23 +122,23 @@ export class HashSpec {
return null;
}
const store = DatabaseSpec.parse(match[1]);
if (!store) {
const database = DatabaseSpec.parse(match[1]);
if (!database) {
return null;
}
return new this(store, hash);
return new this(database, hash);
}
constructor(store: DatabaseSpec, hash: Hash) {
this.store = store;
constructor(database: DatabaseSpec, hash: Hash) {
this.database = database;
this.hash = hash;
}
// Returns the value for the spec'd reference, if any, or null otherwise.
value(): Promise<any> {
const store = this.store.store();
return store.readValue(this.hash).then(v => store.close().then(() => v));
const database = this.database.database();
return database.readValue(this.hash).then(v => database.close().then(() => v));
}
}