Git Product home page Git Product logo

jsync's Introduction

JSync

JSync is a simple open-source library that analyses two sets of POJOs, that may be linked to form graphs, and returns the operations that are necessary to go from the former graph to the latter.

It may seem to be an easy task, but it's not so. Detection of changes require a nesting batch algorithm involving all objects on both graphs. And, the resulting list of operations should be ordered based on the level of dependency of each object.

There are many applications for this kind of synchronization, but the most common is to record changes in a database. For that, the current objects on the database can be provided as the "old" graph and the "new" graph can be filled with the desired outcome. JSync will create a list of operations that may be added to the database to update the graph. In this situation, operations will be executed only if differences are detected.

Example / Usage

Melhods addOld and addNew should be used to populate both graphs with objects. Then, sync method can be called to produce a list of operations.

Some annotations may be added to POJOs in order to ignore fields both for the similarity check and for the computing of dependency levels.

A complete example can be found among the unit tests:

public class Foo implements Synchronizable {
	@IgnoreForSimilarity
	Long id;
	@IgnoreForDependencyLevel
	List<Bar> bars;
	boolean a;
	boolean b;
}

public class Bar implements Synchronizable {
	@IgnoreForSimilarity
	Long id;
	Foo foo;
	boolean changed;
}

@Test
public void test23FooChanged() {
	Foo oldFoo1 = new Foo(1L, null, false);
	Bar oldBar1 = new Bar(1L, oldFoo1, false);
	Foo newFoo1 = new Foo(1L, null, true);
	Bar newBar1 = new Bar(1L, newFoo1, false);
	Bar newBar2 = new Bar(2L, newFoo1, false);
	
	Synchronizer sync = new Synchronizer();
	sync.addOld(oldFoo1);
	sync.addOld(oldBar1);
	sync.addNew(newFoo1);
	sync.addNew(newBar1);
	sync.addNew(newBar2);
	
	List<Operation> l = sync.sync();
	
	assertEquals(3, l.size());
	assertEquals(Operator.UPDATE, l.get(0).getOperator());
	assertEquals(oldFoo1, l.get(0).getOld());
	assertEquals(newFoo1, l.get(0).getNew());
	assertEquals(0, l.get(0).getDependencyLevel());
	assertEquals(Operator.INSERT, l.get(1).getOperator());
	assertEquals(newBar2, l.get(1).getNew());
	assertEquals(newBar2.foo, newFoo1);
	assertEquals(1, l.get(1).getDependencyLevel());
	assertEquals(Operator.UPDATE, l.get(2).getOperator());
	assertEquals(oldBar1, l.get(2).getOld());
	assertEquals(newBar1, l.get(2).getNew());
	assertEquals(newBar1.foo, newFoo1);
	assertEquals(1, l.get(2).getDependencyLevel());
}

jsync's People

Contributors

crivano avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.