Skip to content

Commit

Permalink
minibase project: init
Browse files Browse the repository at this point in the history
  • Loading branch information
openinx committed Sep 6, 2017
0 parents commit 1835d9b
Show file tree
Hide file tree
Showing 14 changed files with 802 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/.externalToolBuilders
.project
*.settings/
.classpath
/build
/.idea/
/logs
*target/
*.orig
*~
hbase-*/test
*.iws
*.iml
*.ipr
patchprocess/
dependency-reduced-pom.xml
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.minibase</groupId>
<artifactId>minibase</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
53 changes: 53 additions & 0 deletions src/main/java/org/apache/minibase/BloomFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.apache.minibase;

public class BloomFilter {
private int k;
private int bitsPerKey;
private int bitLen;
private byte[] result;

public BloomFilter(int k, int bitsPerKey) {
this.k = k;
this.bitsPerKey = bitsPerKey;
}

public BloomFilter(int k, int bitsPerKey, byte[] result) {
this.k = k;
this.bitsPerKey = bitsPerKey;
this.bitLen = result.length;
this.result = result;
}

public byte[] generate(byte[][] keys) {
assert keys != null;
bitLen = keys.length * bitsPerKey;
bitLen = ((bitLen + 7) / 8) << 3;
bitLen = bitLen < 64 ? 64 : bitLen;
result = new byte[bitLen >> 3];
for (int i = 0; i < keys.length; i++) {
assert keys[i] != null;
int h = Bytes.hash(keys[i]);
for (int t = 0; t < k; t++) {
int idx = (h % bitLen + bitLen) % bitLen;
result[idx / 8] |= (1 << (idx % 8));
int delta = (h >> 17) | (h << 15);
h += delta;
}
}
return result;
}

public boolean contains(byte[] key) {
assert result != null;
int h = Bytes.hash(key);
for (int t = 0; t < k; t++) {
int idx = (h % bitLen + bitLen) % bitLen;
if ((result[idx / 8] & (1 << (idx % 8))) == 0) {
return false;
}
int delta = (h >> 17) | (h << 15);
h += delta;
}
return true;
}
}
100 changes: 100 additions & 0 deletions src/main/java/org/apache/minibase/Bytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.apache.minibase;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Bytes {
public static byte[] toBytes(byte b) {
return new byte[] { b };
}

public static byte[] toBytes(String s) throws IOException {
if (s == null) return new byte[0];
return s.getBytes("UTF-8");
}

public static byte[] toBytes(int x) {
byte[] b = new byte[4];
b[3] = (byte) (x & 0xFF);
b[2] = (byte) ((x >> 8) & 0xFF);
b[1] = (byte) ((x >> 16) & 0xFF);
b[0] = (byte) ((x >> 24) & 0xFF);
return b;
}

public static byte[] toBytes(long x) {
byte[] b = new byte[8];
b[7] = (byte) (x & 0xFF);
b[6] = (byte) ((x >> 8) & 0xFF);
b[5] = (byte) ((x >> 16) & 0xFF);
b[4] = (byte) ((x >> 24) & 0xFF);
b[3] = (byte) ((x >> 32) & 0xFF);
b[2] = (byte) ((x >> 40) & 0xFF);
b[1] = (byte) ((x >> 48) & 0xFF);
b[0] = (byte) ((x >> 56) & 0xFF);
return b;
}

public static byte[] toBytes(byte[] a, byte[] b) {
if (a == null) return b;
if (b == null) return a;
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}

public static int toInt(byte[] a) {
return (a[0] << 24) & 0xFF000000 | (a[1] << 16) & 0x00FF0000 | (a[2] << 8) & 0x0000FF00
| (a[3] << 0) & 0x000000FF;
}

public static long toLong(byte[] a) {
long x = 0;
for (int i = 0; i < 8; i++) {
int j = (7 - i) << 3;
x |= ((0xFFL << j) & ((long) a[i] << j));
}
return x;
}

public static byte[] slice(byte[] buf, int offset, int len) throws IOException {
if (buf == null) {
throw new IOException("buffer is null");
}
if (offset < 0 || len < 0) {
throw new IOException("Invalid offset: " + offset + " or len: " + len);
}
if (offset + len > buf.length) {
throw new IOException("buffer overflow, offset: " + offset + ", len: " + len);
}
byte[] result = new byte[len];
System.arraycopy(buf, offset, result, 0, len);
return result;
}

public static int hash(byte[] key) {
if (key == null) return 0;
int h = 1;
for (int i = 0; i < key.length; i++) {
h = (h << 5) + h + key[i];
}
return h;
}

public static int compare(byte[] a, byte[] b) {
if (a == b) return 0;
if (a == null) return -1;
if (b == null) return 1;
for (int i = 0, j = 0; i < a.length && j < b.length; i++, j++) {
int x = a[i] & 0xFF;
int y = b[i] & 0xFF;
if (x != y) {
return x - y;
}
}
return a.length - b.length;
}

}
Loading

0 comments on commit 1835d9b

Please sign in to comment.