Skip to content

Commit

Permalink
close to fixing #13
Browse files Browse the repository at this point in the history
I want to do a couple unit tests, which means I need to abstract out the engine creation in the database.  I was planning to do this anyways.

Signed-off-by: gburgett <[email protected]>
  • Loading branch information
gburgett committed Jan 8, 2013
1 parent c3fbe33 commit 4d50003
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 246 deletions.
30 changes: 30 additions & 0 deletions java/XFlat/src/org/gburgett/xflat/EngineStateException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.gburgett.xflat;

import org.gburgett.xflat.db.EngineBase.EngineState;

/**
*
* @author gordon
*/
public class EngineStateException extends XflatException {

private final EngineState state;
public EngineState getEngineState(){
return state;
}

/**
* Constructs an instance of
* <code>EngineStateException</code> with the specified detail message.
*
* @param msg the detail message.
*/
public EngineStateException(String msg, EngineState state) {
super(msg);
this.state = state;
}
}
137 changes: 107 additions & 30 deletions java/XFlat/src/org/gburgett/xflat/db/ConvertingTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,36 @@ private String generateId(T rowData){

@Override
public void insert(T row) throws DuplicateKeyException {
Element e = convert(row);
final Element e = convert(row);
String id = getId(e);
if(id == null){
//generate new ID
id = generateId(row);
setId(e, id);
}

this.getEngine().insertRow(id, e);
final String sId = id;

this.doWithEngine(new EngineAction(){
@Override
public Object act(Engine engine) {
engine.insertRow(sId, e);
return null;
}
});
}

@Override
public T find(Object id) {
String sId = this.getIdGenerator().idToString(id);
Element data = this.getEngine().readRow(sId);
final String sId = this.getIdGenerator().idToString(id);

Element data = this.doWithEngine(new EngineAction<Element>(){
@Override
public Element act(Engine engine) {
return engine.readRow(sId);
}
});

if(data == null){
return null;
}
Expand All @@ -180,8 +195,9 @@ public T find(Object id) {
}

@Override
public T findOne(XpathQuery query) {
public T findOne(final XpathQuery query) {
Element e = findOneElement(query);

if(e == null){
return null;
}
Expand All @@ -190,7 +206,7 @@ public T findOne(XpathQuery query) {
}

private Element findOneElement(XpathQuery query){
try(Cursor<Element> elements = this.getEngine().queryTable(query)){
try(Cursor<Element> elements = this.queryTable(query)){
Iterator<Element> it = elements.iterator();
if(!it.hasNext()){
return null;
Expand All @@ -203,16 +219,30 @@ private Element findOneElement(XpathQuery query){
}
}

private Cursor<Element> queryTable(final XpathQuery query){
return this.doWithEngine(new EngineAction<Cursor<Element>>(){
@Override
public Cursor<Element> act(Engine engine) {
return engine.queryTable(query);
}
});
}

@Override
public Cursor<T> find(XpathQuery query) {
return new ConvertingCursor(this.getEngine().queryTable(query));
public Cursor<T> find(final XpathQuery query) {
return this.doWithEngine(new EngineAction<Cursor<T>>(){
@Override
public Cursor<T> act(Engine engine) {
return new ConvertingCursor(engine.queryTable(query));
}
});
}

@Override
public List<T> findAll(XpathQuery query) {
List<T> ret = new ArrayList<>();

try(Cursor<Element> data = this.getEngine().queryTable(query)){
try(Cursor<Element> data = this.queryTable(query)){
for(Element e : data){
ret.add(convert(e));
}
Expand All @@ -226,13 +256,20 @@ public List<T> findAll(XpathQuery query) {

@Override
public void replace(T newValue) throws KeyNotFoundException {
String id = getId(newValue);
final String id = getId(newValue);
if(id == null){
throw new KeyNotFoundException("Object has no ID");
}

Element data = convert(newValue, id);
this.getEngine().replaceRow(id, data);
final Element data = convert(newValue, id);
this.doWithEngine(new EngineAction(){
@Override
public Object act(Engine engine) {
engine.replaceRow(id, data);
return null;
}
});

}

@Override
Expand All @@ -258,15 +295,21 @@ public boolean replaceOne(XpathQuery query, T newValue) {
return true;
}

private String recursiveReplaceOne(XpathQuery query, Element data, Element existing){
private String recursiveReplaceOne(XpathQuery query, final Element data, Element existing){
if(existing == null){
return null;
}

String id = getId(existing);
final String id = getId(existing);
setId(data, id);
try{
this.getEngine().replaceRow(id, data);
this.doWithEngine(new EngineAction(){
@Override
public Object act(Engine engine) {
engine.replaceRow(id, data);
return null;
}
});

return id;
}
Expand All @@ -279,49 +322,83 @@ private String recursiveReplaceOne(XpathQuery query, Element data, Element exist

@Override
public boolean upsert(T newValue) {
Element data = convert(newValue);
String id = getId(data);
final Element data = convert(newValue);
final String id = getId(data);

if(id == null){
//insert
id = generateId(newValue);
setId(data, id);
this.getEngine().insertRow(id, data);
final String nId = generateId(newValue);
setId(data, nId);

this.doWithEngine(new EngineAction(){
@Override
public Object act(Engine engine) {
engine.insertRow(nId, data);
return null;
}
});

return true; //inserted
}
else{
return this.getEngine().upsertRow(id, data);
return this.doWithEngine(new EngineAction<Boolean>(){
@Override
public Boolean act(Engine engine) {
return engine.upsertRow(id, data);
}
});
}
}

@Override
public boolean update(Object id, XpathUpdate update) throws KeyNotFoundException {
public boolean update(Object id, final XpathUpdate update) throws KeyNotFoundException {
if(id == null){
throw new IllegalArgumentException("Id cannot be null");
}
String sId = this.getIdGenerator().idToString(id);
final String sId = this.getIdGenerator().idToString(id);

return this.getEngine().update(sId, update);
return this.doWithEngine(new EngineAction<Boolean>(){
@Override
public Boolean act(Engine engine) {
return engine.update(sId, update);
}
});
}

@Override
public int update(XpathQuery query, XpathUpdate update) {
return this.getEngine().update(query, update);
public int update(final XpathQuery query, final XpathUpdate update) {
return this.doWithEngine(new EngineAction<Integer>(){
@Override
public Integer act(Engine engine) {
return engine.update(query, update);
}
});
}

@Override
public void delete(Object id) throws KeyNotFoundException {
if(id == null){
throw new IllegalArgumentException("id cannot be null");
}
String sId = this.getIdGenerator().idToString(id);
final String sId = this.getIdGenerator().idToString(id);

this.getEngine().deleteRow(sId);
this.doWithEngine(new EngineAction(){
@Override
public Object act(Engine engine) {
engine.deleteRow(sId);
return null;
}
});
}

@Override
public int deleteAll(XpathQuery query) {
return this.getEngine().deleteAll(query);
public int deleteAll(final XpathQuery query) {
return this.doWithEngine(new EngineAction<Integer>(){
@Override
public Integer act(Engine engine) {
return engine.deleteAll(query);
}
});
}

private class ConvertingCursor implements Cursor<T>{
Expand Down
Loading

0 comments on commit 4d50003

Please sign in to comment.