logo

Support for cursor in an Entity for very wide rows

Support for cursor in an Entity for very wide rows

This project source code is available on github at https://github.com/deanhiller/playorm.

For a well-known example of Student and Course for ManytoMany relationship, most of the implementations works like this:

public class Student {
@NoSqlId
private String id;
private String firstName;
private String lastName;
@NoSqlManyToMany
private List<Course> courses = new ArrayList(); //constructing avoids nullpointers
}

public class Course {
@NoSqlId
private String id;
private String name;
private String description
@NoSqlManyToOne
private Lecturer lecturer;
@NoSqlManyToMany
private List<Student> students = new List<Student>();
}

However, in big-data cases, the above POJO method could get OutOfMemoryExceptions if there are too many in the list. Therefore, Playorm provide a cursor in this case to fetch and browse the results

@NoSqlManyToMany
private CursorToMany<Course> courseCursor = new CursorToManyImpl<Course>();

and can get and use the cursor like below:

CursorToMany<Course> cursor = theStudent.getCourseCursor ();
while(cursor.next()) {
Course current = cursor.getCurrent();
....
}

Note: For any *ToMany(OneToMany, ManyToMany) Playorm store a LIST of foreign keys in the row itself. In the example above, there are ONLY two tables. A row is going to look like this

studentrowkey -> firstName="harry", lastName="potter", courses:courseFk56=null, courses:courseFk78=null, courses:courseFk98=null, etc. etc.

Notice that Playorm did not have it this way

studentrowkey -> firstName="harry", lastName="potter", courses=courseFk56,courseFk78, courseFk98

The reason Playorm use the former is column operations are independent so if two servers read in the same student. One server may add courseFk200 and another may delete courseFk56 from the student and both actions will take affect. If one follow the latter method instead one of the server’s changes would be lost.

 

 << Back to Documentation Index