Best way to map Object ID References? - actionscript-3

I'm writing an application where I have many objects (data models) that are identified by a unique String ID that every such object possesses and these objects can refer to each other by their IDs.
So far so good but now I need to keep track of which object keeps a reference to another object and of course there are cases where an object references (or is referenced by) more than one other object and I was wondering what would be the best method to store these references? In a simple map data structure I could just map one object's ID to another but as mentioned there are cases where an object can hold a ref to an arbitrary amount of other objects. Or I could map another map or an Array that hold more than one reference but I'd like to prevent iteration and maybe somebody knows a much better solution for this.

I guess it depends a lot in the particular use, but I think a Dictionary is the way to go, unless you need the order of the references, in which case I think an Array (or Vector) should work...
//Array:
object1.references.push(object2);
for each(var o in object1.references) trace(o);
object1.references.pop();
//Dictionary:
object1.references[object2] = 1;
for (var o in object1.references) trace(o);
delete object1.references[object2];
Whenever you have a dynamic arbitrary set of objects, I think you will necessarily need to iterate through them, whether its an array, object or dictionary.

Related

Feature request? Ability to create an object via an array of key names in jq

I'm an intermediate-to-advanced user of jq 1.5 and 1.6. I'm iterating over an array of objects (let's say, keyed by the names of the first ten digits). I have an array of strings stored in variable $odd_fields (["one","three",...,"nine"]). Without using a convoluted iterative process (since I can do this statically with only map({one,three,...,nine}), I'd like to be able to re-cast my array of objects into an array of objects with reference to my array of keys, and in that order, like map({$odd_fields[]}).
After hundreds of attempts, I can't seem to do it in one throw without a static list of members. I have been able to approximate this by creating an array of UNwanted keys and mapping del($unwanted_keys[]), but I don't care what keys I'm throwing away (and I have no confidence that the 50th object doesn't have one extra key that I don't want), only what keys I want to keep (or create, if missing), so that every object in the array has the same arbitrary keys (my keys) in the same order (suitable for #csv).
This is a general need not directly related to a specific JSON glob or specific set of keys, hence no sample JSON.
Most attempts that don't generate syntax or reference errors give me an array of objects [{"odd_fields":["one","three",...,"nine"]},...], or, some iterative processes give me just the values of the keys I want. I want whole consistent subset objects as if any key not in my list was never present.
Additionally, any wizard out there who can tell me -- while iterating through an object, is there any way to determine what is/was the key to "."? Similar to: {"one":1,"two":2,"three":3}|map(.|my_key) (where function my_key reveals what .'s key is). I realize there's an easier way to do the above example, but I would like to be able to determine .'s key in other, less direct, code. I don't need to use it to rip all keys from an object, just one key from an arbitrary ".".
To recast an object using the keys and key ordering in an array, $array:
def recast($array):
. as $in
| reduce $array[] as $k ({}; .[$k] = $in[$k]);
You could use this with your array as input as follows:
map(recast($odd_fields))
You may wish to tweak this with respect to missing and/or extra keys.
As a one-liner
A compact, reduce-free version:
map([$array[] as $p|{($p):.[$p]}]|add)

fetch geometries of a composite object

How can one fetch geometries of composite objects?
Steps :
Load a model in viewer.
Select an composite-object(icon: Composite Object, dbid: 1234) of viewer.
Now, How can I get geometries(icon: Geometry) of this composite objects?
Note: The current solution I have is to iterate the following ids incrementally until I find a next composite-object, which is generally the case.
json-file for properties
It seems you want to search for elements where displayName=="Icon" and displayValue=="Composite Object". If so, I would suggest:
viewer.search('"Composite Object"', onSuccessCB, onFailCB, ['Icon']);
This will search and the onSuccessCB function will receive an array of dbIds that matches the criteria.

Store multiple authors in to couchbase database

I am a newbie to "couchbase server". What i am looking for is to store 10 author names to couchbase document one after another. Someone please help me whether the structure is like a single document "author" and multiple values
{ id : 1, name : Auther 1}, { id : 2, name : Author 2}
OR store Author 1 to a document and Author 2 to another document.
If so, how can i increment the id automatically before "insert" command.
you can store all authors in a single document
{ doctype : "Authors",
AuthorNames:[
{
id: 1,
Name : "author1"
}
{
id: 2,
Name : "author2"
}
so on
]
IF you want to increase the ID, one is to enter one author name at a time in new document, but ID will be randomly generated and it would not in incremental order.
In Couchbase think more about how your application will be using the data more than how you are want to store it. For example, will your application need to get all of the 10 authors all of the time? If so, then one document might be worthwhile. Perhaps your application needs to only ever read/write one of the authors at a time. Then you might want to put each in their own, but have an object key pattern that makes it so you can get the object really fast. Objects that are used often are kept in the managed cache, other objects that are not used often may fall out of the managed cache...and that is ok.
The other factor is what your reads to writes ratio is on this data.
So like I said, it depends on how your application will be reading and writing your data. Use this as the guidance for how your data should be stored.
The single JSON document is pretty straight forward. The more advanced schema design where each author is in its own document and you access them via object key, might be a bit more complicated, but ultimately faster and more scalable depending on what I already pointed out. I will lay out an example schema and some possibilities.
For the authors, I might create each author JSON document with an object key like this:
authors::ID
Where ID is a value I keep in a special incrementer object that I will called authors::incrementer. Think of that object as a key value pair only holding an integer that happens to be the upper bound of an array. Couchbase SDKs include a special function to increment just such an integer object. With this, my application can put together that object key very quickly. If I want to go after the 5th author, I do a read by object key for "authors::5". If I need to get 10, I do a parallelized BulkGet function and get authors::1 through authors::10. If I want to get all the authors, I get the incrementer object, and get that integer and then to a parallelized bulk get. This way i can get them in order or in whatever order I feel like and I am accessing them by object key which is VERY fast in Couchbase.
All this being said, I could use a view to query this data or the upcoming "SQL for Documents" in Couchbase 4.0 or I can mix and match when I query and when I get objects by their key. Key access will ALWAYS be faster. It is the difference between asking a question then going and getting the object and simply knowing the answer and getting it immediately.

what's the difference with Dictionary, Object and dynamic?

AS3 provide several way to handle something like java Map, but what's the difference with Dictionary, Object and dynamic?
An Object can be used as a map, where the key is a simple string. An objects purpose though is not solely to work as a map, it just happens, that you can use it as such.
A Dictionary is a key/value map, where the key can be an object, too. This is useful, if you want to find values based on a specific object, that you have. The purpose of a Dictionary really is, to be used as a map.
The 'dynamic' keyword is used to make your own class be extendable in the way, that you can put things into an instance of that class at run-time. Something, which you cannot do, if you do not use the keyword 'dynamic'.

Storing Factory Pattern Products

Right now, I've got a switch statement which is being used to create objects based on a string. There are three types of objects which extend an abstract generic object. I should really be using a factory pattern, which I'm figuring out right now. My issue is thus: I appreciate the flexibility of the factory pattern, but right now I'm storing the would-be products in special dictionaries dedicated to their type.
_type1[location] = ArrayOfType1s
_type2[location] = ArrayOfType2s
_type3[location] = ArrayOfType3s
That works if I only have three types, but if I decide to add more with the flexibility of the factory pattern, then that presents the problem of how to store them, as I'd have to make a special dictionary each time I add one...
The only answer that I can think of is to nest my dictionaries, which sounds pretty slow.
_factoryOutput[type] = type[location] = ArrayOfTypes
That's probably a workable solution, but can anyone suggest a cleaner one? I'm working in AS3, but feel free to provide a more generic solution.
One possible solution is to have your products implement a getType() method. This could just return a string, or int that is unique to that product type. You could dynamically create unique arrays for product types as they come up (ie: check if array exists for type, create if needed before storing) or alternatively, you could just store all product types in one array, and have filter functions for retrieving them. An example of this would be:
function getProductsByType(type:String):Array {
var matched:Array = [];
for(//loop over all products) {
//if type is what your looking for, push into matched array
}
return matched;
}