OP Links (v0.3)

From Native Big Data Documentation
Jump to: navigation, search


Links allows join different objects.

A link is a proxy object witch allows to share an object in several storage objects. The use of a link is similar to a symbolic link in UNIX systems or the use of a "LEFT JOIN" in SQL.

The best way to understand links is with an example:

var customers = new( "urp:obj:/people", {
	"custommers": {
		"big custommers": [ 
			{ "name": "Jonh"  , "age" : 41, "gender": "M" },
			{ "name": "Maria" , "age" : 25, "gender": "W" },
			{ "name": "Jack"  , "age" : 18, "gender": "M" }
		],
		"small custommers": [
			{ "name": "Peter" , "age" : 21, "gender": "M" },
			{ "name": "Lisa"  , "age" : 19, "gender": "W" },
			{ "name": "Sofia" , "age" : 27, "gender": "W" }
		]
	},
	"supliers": [
		{ "name": "The iron company"  , "product" : "iron"           },
		{ "name": "Sawmills Inc"      , "product" : "wood"           },
		{ "name": "Acme Inc"          , "product" : "portable holes" }
	]	
});

var sales = new( "urp:obj:/sales", [
	{ "custommer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 0 ]) , "amount": 35 , "date": Date.new( [ 2018, 1 , 1 , 12, 0, 0 ] )  },
	{ "custommer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 2 ]) , "amount": 12 , "date": Date.new( [ 2018, 1 , 1 , 13, 0, 0 ] )  },
	{ "custommer": Link("urp:obj:/people", [ "custommers", "small custommers" , 0 ]) , "amount": 45 , "date": Date.new( [ 2018, 1 , 1 , 16, 0, 0 ] )  },
	{ "custommer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 1 ]) , "amount": 12 , "date": Date.new( [ 2018, 1 , 2 , 09, 0, 0 ] )  },
	{ "custommer": Link("urp:obj:/people", [ "custommers", "small custommers" , 2 ]) , "amount":  5 , "date": Date.new( [ 2018, 1 , 3 , 12, 0, 0 ] )  },
	{ "custommer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 2 ]) , "amount": 34 , "date": Date.new( [ 2018, 1 , 4 , 18, 0, 0 ] )  }
] );


sales[3].custommer.value().age

In this example we have a resource "urp:obj:/people" that have information about customers and supliers. At this point is important to design our data models in some similar to a 3st normal form. This is good because we must prevent from having duplicated information. Link objects allow us to link every sale with its custommer data. The last example returns 25 because is the age of Maria.

Using links has these advantages:

  • A master object can be referenced by several client objects. Links can be created anywhere.
  • If the master object is modified, all objects that are linked will be automatically updated.
  • If a client object modifies the content of a link, then the master will be modified too.
  • Links are transparent to all op function. The only function that knows the existence of the link is the contructor. This is specially usefull with map-reduce functions:
  • Links works with i2rr cached objects in RAM. This means that if a object is loaded in RAN with the i2rr command, then Links that points to this object will get the data from RAM.
i2o("urp:obj:/sales").reduce( (a,b) => {a+b} , (v) => { [ [1, v.amount], v.customer.gender ] }  )
  • Links can be bidirectional:
var customers = new( "urp:obj:/people", {
	"custommers": {
		"big custommers": [ 
			{ "name": "Jonh"  , "age" : 41, "gender": "M" , "sales" : [ Link ("urp:obj:/sales" ,[0] ) ]  },
			{ "name": "Maria" , "age" : 25, "gender": "W" , "sales" : [ Link ("urp:obj:/sales" ,[3] ) ]  },
			{ "name": "Jack"  , "age" : 18, "gender": "M" , "sales" : [ Link ("urp:obj:/sales" ,[1] ) , Link ("urp:obj:/sales" ,[5] ) ]  },
		],
		"small custommers": [
			{ "name": "Peter" , "age" : 21, "gender": "M" , "sales" : [ Link ("urp:obj:/sales" ,[2] )                                 ]  },
			{ "name": "Lisa"  , "age" : 19, "gender": "W" , "sales" : [ ]  },
			{ "name": "Sofia" , "age" : 27, "gender": "W" , "sales" : [ Link ("urp:obj:/sales" ,[4] )                                 ]  },
		]
	},
	"supliers": [
		{ "name": "The iron company"  , "product" : "iron"           },
		{ "name": "Sawmills Inc"      , "product" : "wood"           },
		{ "name": "Acme Inc"          , "product" : "portable holes" }
	]	
});

var sales = new( "urp:obj:/sales", [
	{ "customer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 0 ]) , "amount": 35 , "date": Date.new( [ 2018, 1 , 1 , 12, 0, 0 ] )  },
	{ "customer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 2 ]) , "amount": 12 , "date": Date.new( [ 2018, 1 , 1 , 13, 0, 0 ] )  },
	{ "customer": Link("urp:obj:/people", [ "custommers", "small custommers" , 0 ]) , "amount": 45 , "date": Date.new( [ 2018, 1 , 1 , 16, 0, 0 ] )  },
	{ "customer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 1 ]) , "amount": 12 , "date": Date.new( [ 2018, 1 , 2 , 09, 0, 0 ] )  },
	{ "customer": Link("urp:obj:/people", [ "custommers", "small custommers" , 2 ]) , "amount":  5 , "date": Date.new( [ 2018, 1 , 3 , 12, 0, 0 ] )  },
	{ "customer": Link("urp:obj:/people", [ "custommers", "big custommers"   , 2 ]) , "amount": 34 , "date": Date.new( [ 2018, 1 , 4 , 18, 0, 0 ] )  }
] );
100 * sales[3].amount / sales[3].custommer.value().sales.reduce( (a,b)=>{a+b}, (v)=>{ v.amout } )

The last example returns the percentage of a sale in relation with all the sales on the same custommer.