Crisp Reading Notes on Latest Technology Trends and Basics

Archive for the ‘Uncategorized’ Category

Scala says “Singletons Instead of Static”



Java uses the static methods and blocks as a primary primitive.
Singleton is built as a common design pattern on top of static methods.

Scala on the other hand builds the singleton as a standard pattern.
Static methods are implemented as methods within a singleton class

Objects versus Classes

Scala uses two keywords –  object and class to distinguish between singletons and non-singletons.

  • Defining an object defines a class and a singleton object with the same name
object Account {
  var count = 0

  def numAccounts(): Unit = {
    return count
  • From the corresponding Account class, the count is added whenever an account is created
class Account(ssn:Int, balance:Float) {

Companion Classes

  • In the above example, the use of the same name for the object and the class is deliberate
  • These are called companion classes. They are present in the same file, and may access each others private methods

Singleton Objects in Other Domains

  • Singleton objects are used heavily used in CMIP, which is a OSI standard for sophisticated modeling
  • Singleton objects are also heavily used in Dependency-Injection Frameworks, such as Spring.

Scala says “Bye-Bye Java Beans”

Scala avoids JavaBeans code

One of the most powerful features of Scala is that it removes the highly repetitive nature of writing Java Beans (something that occupies major space in programs and books)

Constructors and Accessors – Motivating Example

The Scala way

To code a class BankAccount with

  • A immutable string – name
  • A mutable int – balance
class Account( val name: String, var balance: int )
acc = new Account(“scott”, 25)

The Java way

The equivalent code in Java would have

  • A constructor
  • A getter only for the immutable entity
  • A getter and setter for the mutable entities
public class Account {
  private String name;
  private int balance;

  public Account(String name, int balance) { = name;
    this.balance = balance;

  public String getName() {

  public int getBalance() {
    return this.balance;

  public void setBalance(int balance) {
    this.balance = balance;

IDEs do this work – Why bother

IDEs still produce code that you need to maintain. It could be distracting.

Also, if there is a way to modify the generated code, there will be a temptation to modify it. After that, change management becomes hard.

The meaning of the words on the constructors

  • If a constructor argument is specified as val, then only a getter is generated for it
  • If a constructor argument is specified as var, then both a getter and setter are generated for it
  • If a constructor argument is specified as neither, then, the variable is not part of the class. The argument is used in the constructor and discarded.

Multiple and Custom Constructors

Like Java, Scala allows multiple and custom constructors. It also improves by having only one primary constructor and having secondary constructors go through the same

Multiple Constructors

Like Java, scala allows multiple constructors. It calls one constructor as primary, and the other constructors as secondary

If the bank accounts were opened with zero balance, the most common case, a new constructor could be used

class Account( val name: String, var balance: int ) {
  def this( val name: String ) = {
    this( name, 0 );

Custom Code in Primary Constructors

Primary constructors also have custom code in them

Suppose our constructor took a SSN, and did a lookup for the name

class Account(val ssn: int, var balance int) {
  val name = ssn_lookup(ssn)

  def ssn_lookup(val ssn: int): String {
    // To be implemented

B-Tree and Index Locking in Databases

In this short note, we elaborate on a short and crisp algorithm to lock the nodes of a B-Tree and of the Indexes, while executing the standard operations of “Select” and “Insert”

Tree-based Algorithm

Search Operation

The following enhanced algorithm will work well for the Select algorithm, while traversing the B-tree

lock root of B-tree

while ( not at leaf ) {

lock child;

unlock parent;


The algorithm works because

  • During a “Select”, a node is never revisited
  • Also, all Insert/Delete operations must follow the same path during their traversal.

Insert/Delete Operation

The following is a simple algorithm for Insert/Delete

  • For  inserts, changes made to the leaf of the tree cause splits on the parent and ancestors
    • Until a non-full node is reached ( This is called a safe-child )
  • For deletes, changes made to the leaf of the tree cause merges on the parent and ancestors
    • Until a non half-empty node is reached ( This is called a safe-child )
lock root of B-tree

while ( not at leaf ) {

lock child;

if ( child is safe ) {

// The split will stop at the child. So

release all nodes held, except for the child



Optimized Insert/Delete Algorithm

The following algorithm performs very well in practice

lock leaf

if (leaf is not safe ) {

unlock leaf;

restart with normal insert/delete algorithm


Index Locking

Predicate Locks

When a query executes a Predicate, other transactions may add fresh data, and we may see only a portion of their effects. This becomes non-serializable.

  • If there is an Index for the predicate,
    • Predicate locks are achieved by locking the Index on specific pages for entries containing the predicate value
    • Or by locking the pages where the Predicate value would have been placed.
  • If there is no Index for the predicate,
    • The entire table needs to be locked.

Snippet Generation

The best article on this is a Research Report, that gives a pretty good idea of how snippets are being generated, and how they should be generated.


Social Media Landscape

What is NOSQL

Not Only SQL

Four Types

Key-Value Distributed Hash Tables – Dynamo, Riak, Voldermort, memcached, redis
Big-Table based Key-Values, Column Families – HBase, HyperTable, Cassandra
Document based Entities and Attributes. Retrieved as XML or JSON documents –        Lotus Notes,  CouchDB, MongoDB
Graph based Nodes and Relationships – Neo4j, AllegroGraph

Cassandra Integration with HDFS

Tag Cloud