Skip to content

Commit

Permalink
naming fixes: use collection, store, handle appropriate (and get rid …
Browse files Browse the repository at this point in the history
…of views) (PolymerLabs#1389)

* rename SetView to Collection (and some more view->handle renames)
* delete unused manifest-view
* remove deprecated "view" from manifest-parser.peg
* Consistently use "store" and "handle" in arc and manifest classes and their apis.
  • Loading branch information
mariakleiner authored and shans committed May 29, 2018
1 parent f288ce5 commit eb88482
Show file tree
Hide file tree
Showing 55 changed files with 612 additions and 721 deletions.
136 changes: 59 additions & 77 deletions runtime/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class Arc {
this._loader = loader;
this._scheduler = scheduler || new Scheduler();

// All the handles, mapped by handle ID
this._handlesById = new Map();
// All the stores, mapped by store ID
this._storesById = new Map();

// storage keys for referenced handles
this._storageKeys = {};
Expand All @@ -58,12 +58,10 @@ export class Arc {
}
this._storageProviderFactory = storageProviderFactory || new StorageProviderFactory(this.id);

// Dictionary from each tag string to a list of handles
this._tags = {};
// Map from each handle to a list of tags.
this._handleTags = new Map();
// Map from each handle to its description (originating in the manifest).
this._handleDescriptions = new Map();
// Map from each store to a set of tags.
this._storeTags = new Map();
// Map from each store to its description (originating in the manifest).
this._storeDescriptions = new Map();

this._search = null;
this._description = new Description(this);
Expand Down Expand Up @@ -144,7 +142,7 @@ export class Arc {
for (let url of importSet.values())
resources += `import '${url}'\n`;

for (let handle of this._handles) {
for (let handle of this._stores) {
if (!handleSet.has(handle.id))
continue;
let type = handle.type;
Expand Down Expand Up @@ -225,7 +223,7 @@ ${this.activeRecipe.toString()}`;
context
});
// TODO: pass tags through too
manifest.handles.forEach(handle => arc._registerHandle(handle, []));
manifest.stores.forEach(store => arc._registerStore(store, []));
let recipe = manifest.activeRecipe.clone();
let options = {errors: new Map()};
assert(recipe.normalize(options), `Couldn't normalize recipe ${recipe.toString()}:\n${[...options.errors.values()].join('\n')}`);
Expand Down Expand Up @@ -254,7 +252,7 @@ ${this.activeRecipe.toString()}`;
assert(connection.isOptional);
continue;
}
let handle = this.findHandleById(connection.handle.id);
let handle = this.findStoreById(connection.handle.id);
assert(handle, `can't find handle of id ${connection.handle.id}`);
this._connectParticleToHandle(id, recipeParticle, name, handle);
}
Expand All @@ -275,20 +273,20 @@ ${this.activeRecipe.toString()}`;
return {base: this.id, component: () => this._nextLocalID++};
}

get _handles() {
return [...this._handlesById.values()];
get _stores() {
return [...this._storesById.values()];
}

// Makes a copy of the arc used for speculative execution.
async cloneForSpeculativeExecution() {
let arc = new Arc({id: this.generateID().toString(), pecFactory: this._pecFactory, context: this.context, loader: this._loader, speculative: true});
let handleMap = new Map();
for (let handle of this._handles) {
for (let handle of this._stores) {
let clone = await arc._storageProviderFactory.construct(handle.id, handle.type, 'in-memory');
await clone.cloneFrom(handle);
handleMap.set(handle, clone);
if (this._handleDescriptions.has(handle)) {
arc._handleDescriptions.set(clone, this._handleDescriptions.get(handle));
if (this._storeDescriptions.has(handle)) {
arc._storeDescriptions.set(clone, this._storeDescriptions.get(handle));
}
}
this.particleHandleMaps.forEach((value, key) => {
Expand Down Expand Up @@ -341,7 +339,7 @@ ${this.activeRecipe.toString()}`;

for (let v of handleMap.values()) {
// FIXME: Tags
arc._registerHandle(v, []);
arc._registerStore(v, []);
}
return arc;
}
Expand Down Expand Up @@ -370,28 +368,28 @@ ${this.activeRecipe.toString()}`;
type = type.resolvedType();
assert(type.isResolved(), `Can't create handle for unresolved type ${type}`);

let handle = await this.createHandle(type, /* name= */ null, this.generateID(), recipeHandle.tags);
let newStore = await this.createStore(type, /* name= */ null, this.generateID(), recipeHandle.tags);
if (recipeHandle.fate === 'copy') {
let copiedHandle = this.findHandleById(recipeHandle.id);
assert(copiedHandle._version !== null);
await handle.cloneFrom(copiedHandle);
let copiedHandleDesc = this.getHandleDescription(copiedHandle);
if (copiedHandleDesc) {
this._handleDescriptions.set(handle, copiedHandleDesc);
let copiedStore = this.findStoreById(recipeHandle.id);
assert(copiedStore._version !== null);
await newStore.cloneFrom(copiedStore);
let copiedStoreDesc = this.getStoreDescription(copiedStore);
if (copiedStoreDesc) {
this._storeDescriptions.set(newStore, copiedStoreDesc);
}
}
recipeHandle.id = handle.id;
recipeHandle.id = newStore.id;
recipeHandle.fate = 'use';
recipeHandle.storageKey = handle.storageKey;
recipeHandle.storageKey = newStore.storageKey;
// TODO: move the call to OuterPEC's DefineHandle to here
}

let storageKey = recipeHandle.storageKey;
if (!storageKey)
storageKey = this.keyForId(recipeHandle.id);
assert(storageKey, `couldn't find storage key for handle '${recipeHandle}'`);
let handle = await this._storageProviderFactory.connect(recipeHandle.id, recipeHandle.type, storageKey);
assert(handle, `handle '${recipeHandle.id}' was not found`);
let store = await this._storageProviderFactory.connect(recipeHandle.id, recipeHandle.type, storageKey);
assert(store, `store '${recipeHandle.id}' was not found`);
}

particles.forEach(recipeParticle => this._instantiateParticle(recipeParticle));
Expand All @@ -414,8 +412,8 @@ ${this.activeRecipe.toString()}`;
handleMap.handles.set(name, targetHandle);
}

async createHandle(type, name, id, tags, storageKey) {
assert(type instanceof Type, `can't createHandle with type ${type} that isn't a Type`);
async createStore(type, name, id, tags, storageKey) {
assert(type instanceof Type, `can't createStore with type ${type} that isn't a Type`);

if (type.isRelation) {
type = Type.newCollection(type);
Expand All @@ -430,37 +428,30 @@ ${this.activeRecipe.toString()}`;
if (storageKey == undefined)
storageKey = 'in-memory';

let handle = await this._storageProviderFactory.construct(id, type, storageKey);
assert(handle, 'handle with id ${id} already exists');
handle.name = name;
let store = await this._storageProviderFactory.construct(id, type, storageKey);
assert(store, 'stopre with id ${id} already exists');
store.name = name;

this._registerHandle(handle, tags);
return handle;
this._registerStore(store, tags);
return store;
}

_registerHandle(handle, tags) {
_registerStore(store, tags) {
tags = tags || [];
tags = Array.isArray(tags) ? tags : [tags];

this._handlesById.set(handle.id, handle);
this._storesById.set(store.id, store);

if (tags.length) {
for (let tag of tags) {
if (this._tags[tag] == undefined)
this._tags[tag] = [];
this._tags[tag].push(handle);
}
}
this._handleTags.set(handle, new Set(tags));
this._storeTags.set(store, new Set(tags));

this._storageKeys[handle.id] = handle.storageKey;
this._storageKeys[store.id] = store.storageKey;
}

// Convert a type to a normalized key that we can use for
// equality testing.
//
// TODO: we should be testing the schemas for compatiblity instead of using just the name.
// TODO: now that this is only used to implement findHandlesByType we can probably replace
// TODO: now that this is only used to implement findStoresByType we can probably replace
// the check there with a type system equality check or similar.
static _typeToKey(type) {
if (type.isCollection) {
Expand All @@ -479,10 +470,10 @@ ${this.activeRecipe.toString()}`;
}
}

findHandlesByType(type, options) {
findStoresByType(type, options) {
// TODO: dstockwell to rewrite this to use constraints and more
let typeKey = Arc._typeToKey(type);
let handles = [...this._handlesById.values()].filter(handle => {
let stores = [...this._storesById.values()].filter(handle => {
if (typeKey) {
let handleKey = Arc._typeToKey(handle.type);
if (typeKey === handleKey) {
Expand All @@ -499,33 +490,33 @@ ${this.activeRecipe.toString()}`;
});

if (options && options.tags && options.tags.length > 0) {
handles = handles.filter(handle => options.tags.filter(tag => !this._handleTags.get(handle).has(tag)).length == 0);
stores = stores.filter(store => options.tags.filter(tag => !this._storeTags.get(store).has(tag)).length == 0);
}
return handles;
return stores;
}

findHandleById(id) {
let handle = this._handlesById.get(id);
if (handle == null) {
handle = this._context.findStorageById(id);
findStoreById(id) {
let store = this._storesById.get(id);
if (store == null) {
store = this._context.findStoreById(id);
}
return handle;
return store;
}

getHandleDescription(handle) {
assert(handle, 'Cannot fetch description for nonexistent handle');
return this._handleDescriptions.get(handle) || handle.description;
getStoreDescription(store) {
assert(store, 'Cannot fetch description for nonexistent store');
return this._storeDescriptions.get(store) || store.description;
}

getHandlesState() {
getStoresState() {
let versionById = new Map();
this._handlesById.forEach((handle, id) => versionById.set(id, handle._version));
this._storesById.forEach((handle, id) => versionById.set(id, handle._version));
return versionById;
}

isSameState(handlesState) {
for (let [id, version] of handlesState ) {
if (!this._handlesById.has(id) || this._handlesById.get(id)._version != version) {
isSameState(storesState) {
for (let [id, version] of storesState ) {
if (!this._storesById.has(id) || this._storesById.get(id)._version != version) {
return false;
}
}
Expand All @@ -536,27 +527,18 @@ ${this.activeRecipe.toString()}`;
return this._storageKeys[id];
}

newCommit(entityMap) {
for (let [entity, handle] of entityMap.entries()) {
entity.identify(this.generateID());
}
for (let [entity, handle] of entityMap.entries()) {
new handleFor(handle).store(entity);
}
}

stop() {
this.pec.stop();
}

toContextString(options) {
let results = [];
let handles = [...this._handlesById.values()].sort(util.compareComparables);
handles.forEach(v => {
results.push(v.toString(this._handleTags.get(v)));
let stores = [...this._storesById.values()].sort(util.compareComparables);
stores.forEach(store => {
results.push(store.toString(this._storeTags.get(store)));
});

// TODO: include handles entities
// TODO: include stores entities
// TODO: include (remote) slots?

if (!this._activeRecipe.isEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/debug/outer-port-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class OuterPortAttachment {
switch (handleType.constructor.name) {
case 'Type':
switch (handleType.tag) {
case 'SetView': return `[${this._describeHandleType(handleType.data)}]`;
case 'Collection': return `[${this._describeHandleType(handleType.data)}]`;
case 'Entity': return this._describeHandleType(handleType.data);
default: return `${handleType.tag} ${this._describeHandleType(handleType.data)}`;
}
Expand Down
16 changes: 8 additions & 8 deletions runtime/description.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class DescriptionFormatter {
await this._updateDescriptionHandles(this._description);

let handleConnection = this._selectHandleConnection(recipeHandle) || recipeHandle.connections[0];
let handle = this._arc.findHandleById(recipeHandle.id);
let handle = this._arc.findStoreById(recipeHandle.id);
return this._formatDescription(handleConnection, handle);
}

Expand Down Expand Up @@ -141,7 +141,7 @@ export class DescriptionFormatter {
let specConn = particle.spec.connectionMap.get(handleConn.name);
let pattern = descByName[handleConn.name] || specConn.pattern;
if (pattern) {
let handleDescription = {pattern: pattern, _handleConn: handleConn, _handle: this._arc.findHandleById(handleConn.handle.id)};
let handleDescription = {pattern: pattern, _handleConn: handleConn, _handle: this._arc.findStoreById(handleConn.handle.id)};
pDesc._connections[handleConn.name] = handleDescription;
}
});
Expand All @@ -151,7 +151,7 @@ export class DescriptionFormatter {
async _getPatternByNameFromDescriptionHandle(particle) {
let descriptionConn = particle.connections['descriptions'];
if (descriptionConn && descriptionConn.handle && descriptionConn.handle.id) {
let descHandle = this._arc.findHandleById(descriptionConn.handle.id);
let descHandle = this._arc.findStoreById(descriptionConn.handle.id);
if (descHandle) {
let descList = await descHandle.toList();
let descByName = {};
Expand Down Expand Up @@ -268,7 +268,7 @@ export class DescriptionFormatter {
properties: handleNames.splice(1),
extra,
_handleConn: handleConn,
_handle: this._arc.findHandleById(handleConn.handle.id)};
_handle: this._arc.findStoreById(handleConn.handle.id)};
}

// slot connection
Expand Down Expand Up @@ -319,7 +319,7 @@ export class DescriptionFormatter {
let handleValue = await this._formatHandleValue(token.handleName, token._handle);
if (!description) {
// For singleton handle, if there is no real description (the type was used), use the plain value for description.
if (handleValue && !token._handle.type.isSetView && !this.excludeValues) {
if (handleValue && !token._handle.type.isCollection && !this.excludeValues) {
return handleValue;
}
}
Expand Down Expand Up @@ -358,7 +358,7 @@ export class DescriptionFormatter {
}

async _propertyTokenToString(handleName, handle, properties) {
assert(!handle.type.isSetView, `Cannot return property ${properties.join(',')} for set-view`);
assert(!handle.type.isCollection, `Cannot return property ${properties.join(',')} for collection`);
// Use singleton value's property (eg. "09/15" for person's birthday)
let handleVar = await handle.get();
if (handleVar) {
Expand All @@ -382,7 +382,7 @@ export class DescriptionFormatter {
if (!handle) {
return;
}
if (handle.type.isSetView) {
if (handle.type.isCollection) {
let handleList = await handle.toList();
if (handleList && handleList.length > 0) {
return this._formatSetHandle(handleName, handleList);
Expand Down Expand Up @@ -448,7 +448,7 @@ export class DescriptionFormatter {
}
_formatHandleDescription(handleConn, handle) {
if (handle) {
let handleDescription = this._arc.getHandleDescription(handle);
let handleDescription = this._arc.getStoreDescription(handle);
let handleType = this._formatHandleType(handleConn);
// Use the handle description available in the arc (if it is different than type name).
if (!!handleDescription && handleDescription != handleType) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Variable extends Handle {
}

export function handleFor(proxy, isSet, name, particleId, canRead = true, canWrite = true) {
return (isSet || (isSet == undefined && proxy.type.isSetView))
return (isSet || (isSet == undefined && proxy.type.isCollection))
? new Collection(proxy, name, particleId, canRead, canWrite)
: new Variable(proxy, name, particleId, canRead, canWrite);
}
8 changes: 4 additions & 4 deletions runtime/inner-PEC.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class InnerPEC {
createHandle: function(type, name) {
return new Promise((resolve, reject) =>
pec._apiPort.ArcCreateHandle({arc: arcId, type, name, callback: proxy => {
let h = handleFor(proxy, proxy.type.isSetView, name, particleId);
h.entityClass = (proxy.type.isSetView ? proxy.type.primitiveType() : proxy.type).entitySchema.entityClass();
let h = handleFor(proxy, proxy.type.isCollection, name, particleId);
h.entityClass = (proxy.type.isCollection ? proxy.type.primitiveType() : proxy.type).entitySchema.entityClass();
resolve(h);
}}));
},
Expand Down Expand Up @@ -201,8 +201,8 @@ export class InnerPEC {
let registerList = [];
proxies.forEach((proxy, name) => {
let connSpec = spec.connectionMap.get(name);
let handle = handleFor(proxy, proxy.type.isSetView, name, id, connSpec.isInput, connSpec.isOutput);
let type = proxy.type.isSetView ? proxy.type.primitiveType() : proxy.type;
let handle = handleFor(proxy, proxy.type.isCollection, name, id, connSpec.isInput, connSpec.isOutput);
let type = proxy.type.isCollection ? proxy.type.primitiveType() : proxy.type;
if (type.isEntity) {
handle.entityClass = type.entitySchema.entityClass();
}
Expand Down
Loading

0 comments on commit eb88482

Please sign in to comment.