Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #22 from Sqooba/remove-bytebuffer
Browse files Browse the repository at this point in the history
Remove bytebuffer.
  • Loading branch information
Nennya authored Aug 31, 2017
2 parents 4cce019 + 3bd57d3 commit 8430812
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 294 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@
<compilerStartOption>-Wall</compilerStartOption>
<compilerStartOption>-Werror</compilerStartOption>
<compilerStartOption>-fpic</compilerStartOption>
<compilerStartOption>-O0</compilerStartOption>
<compilerStartOption>-g</compilerStartOption>
<compilerStartOption>-Ofast</compilerStartOption>
</compilerStartOptions>

<linkerOutputDirectory>target</linkerOutputDirectory>
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/io/sqooba/traildb/TrailDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class TrailDB implements AutoCloseable {

private final TrailDBNative trailDBj = TrailDBNative.INSTANCE;

/** ByteBuffer holding a pointer to the traildb. */
ByteBuffer db;
/** long holding a pointer to the traildb. */
long db;

private final long numTrails;
private final long numEvents;
Expand All @@ -47,7 +47,7 @@ public TrailDB(String path) {
}

this.db = this.trailDBj.init();
if (this.db == null) {
if (this.db == -1) {
throw new TrailDBException("Failed to allocate memory to init a new TrailDB.");
}

Expand Down Expand Up @@ -286,8 +286,8 @@ public long getTrailID(String uuid) {
* @throws TrailDBException If cursor creation failed in some way.
*/
public TrailDBIterator trail(long trailID) { // Python has more params.
final ByteBuffer cursor = this.trailDBj.cursorNew(this.db);
if (cursor == null) {
final long cursor = this.trailDBj.cursorNew(this.db);
if (cursor == -1) {
throw new TrailDBException("Memory allocation failed for cursor.");
}

Expand Down Expand Up @@ -326,9 +326,9 @@ public Map<String, TrailDBIterator> trails() {

@Override
public void close() {
if (this.db != null) {
if (this.db != -1) {
this.trailDBj.close(this.db);
this.db = null;
this.db = -1;
}
}

Expand All @@ -350,7 +350,7 @@ public static class TrailDBBuilder {
private final String[] ofields;

/** Handle to the TrailDB, returned by init method. */
private ByteBuffer cons;
private long cons;

/** Tells if this Builder build method has already been called. */
private boolean closed = false;
Expand All @@ -373,7 +373,7 @@ public TrailDBBuilder(String path, String[] ofields) {

// Initialisation.
this.cons = this.trailDBj.consInit();
if (this.cons == null) {
if (this.cons == -1) {
throw new TrailDBException("Failed to allocate memory for constructor.");
}
if (this.trailDBj.consOpen(this.cons, path, ofields, ofields.length) != 0) {
Expand Down Expand Up @@ -450,7 +450,8 @@ public TrailDB build() {

this.trailDBj.consClose(this.cons);
LOGGER.info("TrailDBBuilder closed.");
this.cons = null;
// Explicitly say the constructor pointer is no longer valid. TODO Needed?
this.cons = -1;

return new TrailDB(this);
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/io/sqooba/traildb/TrailDBEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.sqooba.traildb;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -65,17 +64,11 @@ public String[] getFieldNames() {
*/
public String getFieldValue(int index) {
// Caching possibility here to avoid going back to JNI to decode items.
final ByteBuffer bb = ByteBuffer.allocate(8);
final String value = TrailDBNative.INSTANCE.eventGetItemValue(this.trailDB.db, index, bb, this);
final String value = TrailDBNative.INSTANCE.eventGetItemValue(this.trailDB.db, index, this);
if (value == null) {
throw new TrailDBException("Value not found.");
}
final long value_length = bb.getLong(0);
if (value_length > Integer.MAX_VALUE) {
throw new TrailDBException(
"Overflow, received a String value that is larger than the java String capacity.");
}
return value.substring(0, (int)value_length);
return value;
}

@Override
Expand Down
56 changes: 28 additions & 28 deletions src/main/java/io/sqooba/traildb/TrailDBInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,78 @@

public interface TrailDBInterface {

public ByteBuffer consInit();
public long consInit();

public int consOpen(ByteBuffer cons, String root, String[] ofieldNames, long numOfields);
public int consOpen(long cons, String root, String[] ofieldNames, long numOfields);

public void consClose(ByteBuffer cons);
public void consClose(long cons);

public int consAdd(ByteBuffer cons, byte[] uuid, long timestamp, String[] values,
public int consAdd(long cons, byte[] uuid, long timestamp, String[] values,
long[] valueLengths);

public int consAppend(ByteBuffer cons, ByteBuffer db);
public int consAppend(long cons, long db);

public int consFinalize(ByteBuffer cons);
public int consFinalize(long cons);

// ========================================================================
// Open a TrailDB and access metadata.
// ========================================================================

public ByteBuffer init();
public long init();

public int open(ByteBuffer db, String root);
public int open(long db, String root);

public void close(ByteBuffer db);
public void close(long db);

public long numTrails(ByteBuffer db);
public long numTrails(long db);

public long numEvents(ByteBuffer db);
public long numEvents(long db);

public long numFields(ByteBuffer db);
public long numFields(long db);

public long minTimestamp(ByteBuffer db);
public long minTimestamp(long db);

public long maxTimestamp(ByteBuffer db);
public long maxTimestamp(long db);

public long version(ByteBuffer db);
public long version(long db);

public String errorStr(int errcode);

// ========================================================================
// Working with items, fields and values.
// ========================================================================

public long lexiconSize(ByteBuffer db, long field);
public long lexiconSize(long db, long field);

public int getField(ByteBuffer db, String fieldName, ByteBuffer field);
public int getField(long db, String fieldName, ByteBuffer field);

public String getFieldName(ByteBuffer db, long field);
public String getFieldName(long db, long field);

public long getItem(ByteBuffer db, long field, String value);
public long getItem(long db, long field, String value);

public String getValue(ByteBuffer db, long field, long val, ByteBuffer value_length);
public String getValue(long db, long field, long val, ByteBuffer value_length);

public String getItemValue(ByteBuffer db, long item, ByteBuffer value_length);
public String getItemValue(long db, long item, ByteBuffer value_length);

// ========================================================================
// Working with UUIDs.
// ========================================================================

public ByteBuffer getUUID(ByteBuffer db, long traildId);
public ByteBuffer getUUID(long db, long traildId);

public int getTrailId(ByteBuffer db, byte[] uuid, ByteBuffer trailId);
public int getTrailId(long db, byte[] uuid, ByteBuffer trailId);

// ========================================================================
// Query events with cursors.
// ========================================================================

public ByteBuffer cursorNew(ByteBuffer db); // A cursor is a void *.
public long cursorNew(long db); // A cursor is a void *.

public void cursorFree(ByteBuffer cursor);
public void cursorFree(long cursor);

public int getTrail(ByteBuffer cursor, long trailID);
public int getTrail(long cursor, long trailID);

public long getTrailLength(ByteBuffer cursor);
public long getTrailLength(long cursor);

public int cursorNext(ByteBuffer cursor, TrailDBEvent event);
public int cursorNext(long cursor, TrailDBEvent event);
}
9 changes: 4 additions & 5 deletions src/main/java/io/sqooba/traildb/TrailDBIterator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.sqooba.traildb;

import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.NoSuchElementException;

Expand All @@ -17,21 +16,21 @@
*/
public class TrailDBIterator implements Iterable<TrailDBEvent>, AutoCloseable {

private ByteBuffer cursor;
private long cursor;
private final TrailDB trailDB;
private final long size;

protected TrailDBIterator(ByteBuffer cursor, TrailDB trailDB, int size) {
protected TrailDBIterator(long cursor, TrailDB trailDB, int size) {
this.cursor = cursor;
this.trailDB = trailDB;
this.size = size;
}

@Override
public void close() {
if (this.cursor != null) {
if (this.cursor != -1) {
TrailDBNative.INSTANCE.cursorFree(this.cursor);
this.cursor = null;
this.cursor = -1;
}
}

Expand Down
Loading

0 comments on commit 8430812

Please sign in to comment.