Skip to content

Getting Started

João Sousa edited this page May 12, 2017 · 5 revisions

Welcome to the Getting Started page!

First things first!

Check out the Prerequisites page, which gives you a glimpse of the few things you need to use Smof.

Add the project to your classpath and let's get started.

Smof

This framework uses an object to perform all the main operations, in order to make it simple and straightforward. So, after starting a mongo server and copying the project to the classpath, use Smof to create a connection to the mongo server:

import org.smof.collection.Smof;

...
...

final Smof smof = Smof.create("localhost", 27017, "myDB");

Now, with this connection you can load and create collections, insert, update or query elements.

Load collections

The main goal of this project is to (automatically) turn java objects in BSON documents, stored by mongoDB. To achieve this goal efficiently, but still give the user some control over the serialized data, all java object that are to be serialized to BSON documents must have some specifications.

Element

All java objects that are to be mapped to a mongo collection must have some special properties (e.g. the _id property). However, all this "special properties" are already taken care by the Element class.

Let's talk practical then. For instance, consider this simple Bottle class:

public static class Bottle {

	private String liquid;
	private double amount;
	private double capacity;

	public Bottle(String liquid, double capacity) {
		this(liquid, capacity, 0.0);
	}

	public boolean isFull() {
		return capacity == amount;
	}

	public double fill(Double amount) {
		final double left = capacity-amount;
		if(left < amount) {
			this.amount = capacity;
			return amount-left;
		}
		this.amount += amount;
		return left-amount;
	}
}

In order to map it to a fully functional mongoDB, this object must extend AbstractElement (an abstract implementation of Element). But that's not all. Bottle has 3 fields, but you might not want to store all of them. You might also want to stored them in specific formats (e.g. for cross-platform support). So, each field must specify its name and type in the database (but only for the fields you want to store):

public static class Bottle extends AbstractElement {

	private static final String CAPACITY = "capacity";
	private static final String AMOUNT = "liquid_amount";
	private static final String LIQUID = "liquid";

	@SmofString(name = LIQUID)
	private String liquid;

	@SmofNumber(name = AMOUNT)
	private double amount;

	@SmofNumber(name = CAPACITY)
	private double capacity;

	public Bottle(String liquid, double capacity) {
		this(liquid, capacity, 0.0);
	}

	public boolean isFull() {
		return capacity == amount;
	}

	public double fill(Double amount) {
		final double left = capacity-amount;
		if(left < amount) {
			this.amount = capacity;
			return amount-left;
		}
		this.amount += amount;
		return left-amount;
	}
}

This specification is made through Java Annotations. The basic smofTypes are:

  • @SmofString - a general purpose string
  • @SmofNumber- a BsonNumber. Assumes different Bson types, according to the field's java type.
  • @SmofDate - Used for dates. Supports Java 8 Time and JodaTime.
  • @SmofByte
  • @SmofBoolean
  • @SmofArray
  • @SmofObject
  • @SmofObjectId

Table of Contents

Clone this wiki locally