@anidotnet - ok no worries Anindya I'll try to create something. But have a rookie question. The in-memory instance doesn't seem to store anything in the heap. I'm always getting null or actually nothing is returned. what am I doing wrong here?
// In Memory instance
Nitrite db = Nitrite.builder()
.openOrCreate();
// Creating repository
ObjectRepository<Employee> repository = db.getRepository(Employee.class);
// Inserting Employee
Employee emp = new Employee();
emp.setEmpId(12564);
emp.setAddress("12 Some Street");
repository.insert(employees);
// Listing all the employees
List<Employee> employees = repository.find().toList();
for (Employee employee: employees) {
System.out.println(employee);
}
Output - No records are being returned.
Any clue what am I doing wrong here?
public final class DatabaseConnection {
private static final DatabaseConnection DB_CONN = new DatabaseConnection();
private Nitrite db;
private ObjectRepository<Employee> repository;
private DatabaseConnection(){
db = Nitrite.builder().openOrCreate();
repository = db.getRepository(Employee.class);
}
public static DatabaseConnection getInstance() {
return DB_CONN;
}
/**
* Inserting into inmemory
* @param employee
* @return
*/
public List<Employee> insertIntoMemory(Employee employee) {
return insert(employee);
}
/**
* Find from memory
* @param offset
* @param size
* @return
*/
public List<Employee> findFromMemory(int offset, int size){
return find(offset, size);
}
private List<Employee> insert(Employee employee){
List<Employee> employees = new ArrayList<>();
WriteResult wr = null;
try {
wr = repository.insert(employee);
System.out.println("\n Affected Rows : " + wr.getAffectedCount());
return repository.find().toList();
} catch (Exception e) {
System.out.println("Unable to insert transaction. ");
e.printStackTrace();
}
return employees;
}
private List<Employee> find(int offset, int size) {
List<Employee> employees = new ArrayList<>();
try {
employees = repository.find(limit(offset, size)).toList();
} catch (Exception e) {
System.out.println("Unable to find transactions. ");
e.printStackTrace();
}
return employees;
}
}
HTTP/1.1 200 OK
Date: Thu, 05 Dec 2019 18:30:39 GMT
Content-Type: application/json
Content-Length: 365
Server: Jetty(9.4.22.v20191022)
[
{
"empId": "f843e184-46f0-4967-b3e1-fecbb4dfcf23",
"address": "From Memory. 123, North Pole."
},
{
"empId": "6a75a311-5cb7-44fd-9ca8-1b7a86ea3b22",
"address": "From Memory. 123, North Pole."
},
{
"empId": "c2e3c339-26b3-4bb9-8823-41bc94e90065",
"address": "From Memory. 123, North Pole."
},
{
"empId": "1d40b65a-4ef7-4acf-973e-2b327f591629",
"address": "From Memory. 123, North Pole."
}
]
Response code: 200 (OK); Time: 35ms; Content length: 365 bytes
Nitrite.builder().openOrCreate()
before every insert call, which is actually resetting the memory store before every insert.
Hi, I am not sure I understand the find API for a specific use case.. say I have some JSON as follows...
"tags": [
{
"type": "example"
}
],
How do I write a find query that returns documents that have a tag where the key is type
or documents that have a type field with the value example?
elemMatch
filter here.public void testCollectionField() {
Document document = createDocument("name", "John")
.put("tags", new Document[] {
createDocument("type", "example").put("other", "value"),
createDocument("type", "another-example").put("other", "some-other-value")
});
NitriteCollection example = db.getCollection("example");
example.insert(document);
document = createDocument("name", "Jane")
.put("tags", new Document[] {
createDocument("type", "example2").put("other", "value2"),
createDocument("type", "another-example2").put("other", "some-other-value2")
});
example.insert(document);
DocumentCursor cursor = example.find(elemMatch("tags", eq("type", "example")));
for (Document doc : cursor) {
System.out.println(doc);
}
}
NitriteDataGate.java
from datagate.