Skip to content

Commit

Permalink
Merge pull request #68 from benalexau/agrona
Browse files Browse the repository at this point in the history
Replace DirectBuffer with Agrona DirectBuffer
  • Loading branch information
krisskross committed May 31, 2016
2 parents 9a29e34 + e980a67 commit 14b264a
Show file tree
Hide file tree
Showing 20 changed files with 732 additions and 760 deletions.
19 changes: 19 additions & 0 deletions lmdbjni-android/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ limitations under the License.
</configureArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../lmdbjni/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
19 changes: 19 additions & 0 deletions lmdbjni-linux64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ limitations under the License.
</configureArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../lmdbjni/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions lmdbjni-osx64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ limitations under the License.
</configureArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../lmdbjni/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
19 changes: 19 additions & 0 deletions lmdbjni-win64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@
<packageDirectory>${basedir}/../lmdbjni/target/generated-sources/hawtjni/native-package</packageDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../lmdbjni/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
69 changes: 69 additions & 0 deletions lmdbjni/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>hawtjni-runtime</artifactId>
<version>${hawtjni-version}</version>
</dependency>
<dependency>
<groupId>org.agrona</groupId>
<artifactId>Agrona</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -116,6 +121,11 @@
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<artifactSet>
<excludes>
<exclude>org.agrona:Agrona:*:*</exclude>
</excludes>
</artifactSet>
<createSourcesJar>true</createSourcesJar>
<shadeSourcesContent>true</shadeSourcesContent>
</configuration>
Expand All @@ -128,6 +138,65 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>uk.co.real-logic</groupId>
<artifactId>sbe-all</artifactId>
</executableDependency>
<mainClass>uk.co.real_logic.sbe.SbeTool</mainClass>
<classpathScope>test</classpathScope>
<systemProperties>
<systemProperty>
<key>sbe.output.dir</key>
<value>${project.build.directory}/generated-sources/java</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>${project.build.testResources[0].directory}/car.xml</argument>
</arguments>
<workingDirectory>${project.build.directory}/generated-sources/java</workingDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>uk.co.real-logic</groupId>
<artifactId>sbe-all</artifactId>
<version>1.4.0-RC4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
57 changes: 26 additions & 31 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/BufferCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.agrona.MutableDirectBuffer;

import static org.fusesource.lmdbjni.JNI.mdb_strerror;

Expand Down Expand Up @@ -120,15 +121,15 @@ public class BufferCursor implements AutoCloseable {
private final ByteBuffer keyByteBuffer;
private ByteBuffer valueByteBuffer;
private final boolean isReadOnly;
private DirectBuffer key;
private DirectBuffer value;
private MutableDirectBuffer key;
private MutableDirectBuffer value;
private boolean keyDatbaseMemoryLocation = false;
private boolean valDatbaseMemoryLocation = false;
private int keyWriteIndex = 0;
private int valWriteIndex = 0;
private boolean validPosition = false;

BufferCursor(Cursor cursor, DirectBuffer key, DirectBuffer value) {
BufferCursor(Cursor cursor, MutableDirectBuffer key, MutableDirectBuffer value) {
this.cursor = cursor;
this.isReadOnly = cursor.isReadOnly();
if (key.byteBuffer() == null) {
Expand All @@ -150,7 +151,7 @@ public class BufferCursor implements AutoCloseable {
}

BufferCursor(Cursor cursor, int maxValueSize) {
this(cursor, new DirectBuffer(), new DirectBuffer(ByteBuffer.allocateDirect(maxValueSize)));
this(cursor, Buffers.buffer(), Buffers.buffer(maxValueSize));
}

/**
Expand Down Expand Up @@ -333,10 +334,8 @@ public void close() {
* previously existing key.
*/
public boolean put() {
DirectBuffer k = (keyWriteIndex != 0) ?
new DirectBuffer(key.addressOffset(), keyWriteIndex) : key;
DirectBuffer v = (valWriteIndex != 0) ?
new DirectBuffer(value.addressOffset(), valWriteIndex) : value;
MutableDirectBuffer k = (keyWriteIndex != 0) ? Buffers.bufferSlice(key, keyWriteIndex) : key;
MutableDirectBuffer v = (valWriteIndex != 0) ? Buffers.bufferSlice(value, valWriteIndex) : value;
keyWriteIndex = 0;
valWriteIndex = 0;
int rc = cursor.put(k, v, Constants.NOOVERWRITE);
Expand All @@ -355,10 +354,8 @@ public boolean put() {
* previously existing key. Also used for adding duplicates.
*/
public boolean overwrite() {
DirectBuffer k = (keyWriteIndex != 0) ?
new DirectBuffer(key.addressOffset(), keyWriteIndex) : key;
DirectBuffer v = (valWriteIndex != 0) ?
new DirectBuffer(value.addressOffset(), valWriteIndex) : value;
MutableDirectBuffer k = (keyWriteIndex != 0) ? Buffers.bufferSlice(key, keyWriteIndex) : key;
MutableDirectBuffer v = (valWriteIndex != 0) ? Buffers.bufferSlice(value, valWriteIndex) : value;
keyWriteIndex = 0;
valWriteIndex = 0;
int rc = cursor.put(k, v, 0);
Expand All @@ -380,10 +377,8 @@ public boolean overwrite() {
* data corruption.
*/
public void append() {
DirectBuffer k = (keyWriteIndex != 0) ?
new DirectBuffer(key.addressOffset(), keyWriteIndex) : key;
DirectBuffer v = (valWriteIndex != 0) ?
new DirectBuffer(value.addressOffset(), valWriteIndex) : value;
MutableDirectBuffer k = (keyWriteIndex != 0) ? Buffers.bufferSlice(key, keyWriteIndex) : key;
MutableDirectBuffer v = (valWriteIndex != 0) ? Buffers.bufferSlice(value, valWriteIndex) : value;
keyWriteIndex = 0;
valWriteIndex = 0;
int rc = cursor.put(k, v, Constants.APPEND);
Expand Down Expand Up @@ -500,7 +495,7 @@ public BufferCursor keyWriteDouble(double data) {
*/
public BufferCursor keyWriteUtf8(ByteString data) {
setSafeKeyMemoryLocation();
this.key.putString(keyWriteIndex, data);
Util.putString(this.key, keyWriteIndex, data);
keyWriteIndex += data.size() + 1;
return this;
}
Expand All @@ -515,7 +510,7 @@ public BufferCursor keyWriteUtf8(ByteString data) {
public BufferCursor keyWriteUtf8(String data) {
setSafeKeyMemoryLocation();
ByteString bytes = new ByteString(data);
this.key.putString(keyWriteIndex, bytes);
Util.putString(this.key, keyWriteIndex, bytes);
keyWriteIndex += bytes.size() + 1;
return this;
}
Expand Down Expand Up @@ -558,7 +553,7 @@ public BufferCursor keyWriteBytes(byte[] data, int offset, int length) {
* @param capacity capacity
* @return this
*/
public BufferCursor keyWrite(DirectBuffer buffer, int capacity) {
public BufferCursor keyWrite(MutableDirectBuffer buffer, int capacity) {
setSafeKeyMemoryLocation();
this.key.putBytes(keyWriteIndex, buffer, 0, capacity);
keyWriteIndex += capacity;
Expand All @@ -568,7 +563,7 @@ public BufferCursor keyWrite(DirectBuffer buffer, int capacity) {
/**
* @see org.fusesource.lmdbjni.BufferCursor#keyWrite(DirectBuffer, int)
*/
public BufferCursor keyWrite(DirectBuffer buffer) {
public BufferCursor keyWrite(MutableDirectBuffer buffer) {
keyWrite(buffer, buffer.capacity());
return this;
}
Expand Down Expand Up @@ -672,7 +667,7 @@ public double keyDouble(int pos) {
*/
public ByteString keyUtf8(int pos) {
checkForValidPosition();
return this.key.getString(pos);
return Util.getString(key, pos);
}

/**
Expand Down Expand Up @@ -703,14 +698,14 @@ public byte[] keyBytes() {
*
* @return underlying buffer
*/
public DirectBuffer keyBuffer() {
public MutableDirectBuffer keyBuffer() {
return key;
}

/**
* @return the key direct buffer at current position.
*/
public DirectBuffer keyDirectBuffer() {
public MutableDirectBuffer keyDirectBuffer() {
checkForValidPosition();
return key;
}
Expand Down Expand Up @@ -855,7 +850,7 @@ public BufferCursor valWriteUtf8(String data) {
setSafeValMemoryLocation();
ByteString bytes = new ByteString(data);
ensureValueWritableBytes(bytes.size() + 1);
this.value.putString(valWriteIndex, bytes);
Util.putString(this.value, valWriteIndex, bytes);
valWriteIndex += bytes.size() + 1;
return this;
}
Expand All @@ -873,7 +868,7 @@ public BufferCursor valWriteUtf8(ByteString data) {
}
setSafeValMemoryLocation();
ensureValueWritableBytes(data.size() + 1);
this.value.putString(valWriteIndex, data);
Util.putString(this.value, valWriteIndex, data);
valWriteIndex += data.size() + 1;
return this;
}
Expand Down Expand Up @@ -927,7 +922,7 @@ public BufferCursor valWriteBytes(byte[] data, int offset, int length) {
* @param length how many bytes to write
* @return this
*/
public BufferCursor valWrite(DirectBuffer buffer, int srcIndex, int length) {
public BufferCursor valWrite(MutableDirectBuffer buffer, int srcIndex, int length) {
if (isReadOnly) {
throw new LMDBException("Read only transaction", LMDBException.EACCES);
}
Expand All @@ -941,14 +936,14 @@ public BufferCursor valWrite(DirectBuffer buffer, int srcIndex, int length) {
/**
* @see org.fusesource.lmdbjni.BufferCursor#valWrite(DirectBuffer, int, int)
*/
public BufferCursor valWrite(DirectBuffer buffer, int length) {
public BufferCursor valWrite(MutableDirectBuffer buffer, int length) {
return valWrite(buffer, 0, length);
}

/**
* @see org.fusesource.lmdbjni.BufferCursor#valWrite(DirectBuffer, int)
*/
public BufferCursor valWrite(DirectBuffer buffer) {
public BufferCursor valWrite(MutableDirectBuffer buffer) {
valWrite(buffer, 0, buffer.capacity());
return this;
}
Expand Down Expand Up @@ -1056,7 +1051,7 @@ public byte[] valBytes() {
*
* @return underlying buffer
*/
public DirectBuffer valBuffer() {
public MutableDirectBuffer valBuffer() {
return value;
}

Expand Down Expand Up @@ -1091,13 +1086,13 @@ public double valDouble(int pos) {
*/
public ByteString valUtf8(int pos) {
checkForValidPosition();
return this.value.getString(pos);
return Util.getString(this.value, pos);
}

/**
* @return the direct buffer at the current position.
*/
public DirectBuffer valDirectBuffer() {
public MutableDirectBuffer valDirectBuffer() {
checkForValidPosition();
return this.value;
}
Expand Down
Loading

0 comments on commit 14b264a

Please sign in to comment.