Skip to content

Commit

Permalink
(issue #29) multi-node cluster voting
Browse files Browse the repository at this point in the history
  • Loading branch information
bluestreak01 committed Jan 28, 2015
1 parent 18a6a9e commit 6415aba
Show file tree
Hide file tree
Showing 15 changed files with 709 additions and 577 deletions.
93 changes: 49 additions & 44 deletions nfsdb-core/src/main/java/com/nfsdb/collections/IntObjHashMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2015. Vlad Ilyushchenko
* Copyright (c) 2014. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@ public class IntObjHashMap<V> {
private V[] values;
private int[] keys;
private int free;
private int capacity;

public IntObjHashMap() {
this(11);
Expand All @@ -44,28 +45,12 @@ public IntObjHashMap(int initialCapacity, double loadFactor) {
this.loadFactor = loadFactor;
values = (V[]) new Object[capacity];
keys = new int[capacity];
free = initialCapacity;
free = this.capacity = initialCapacity;
clear();
}

@SuppressWarnings({"unchecked"})
protected void rehash() {

int newCapacity = Primes.next(values.length << 1);

free = (int) (newCapacity * loadFactor);

V[] oldValues = values;
int[] oldKeys = keys;
this.keys = new int[newCapacity];
this.values = (V[]) new Object[newCapacity];
Arrays.fill(values, 0, values.length, FREE);

for (int i = oldKeys.length; i-- > 0; ) {
if (oldValues[i] != FREE) {
insertKey(oldKeys[i], oldValues[i]);
}
}
public void clear() {
Arrays.fill(values, FREE);
}

public V get(int key) {
Expand All @@ -81,18 +66,6 @@ public V get(int key) {
return probe(key, index);
}

private V probe(int key, int index) {
do {
index = (index + 1) % keys.length;
if (values[index] == FREE) {
return null;
}
if (key == keys[index]) {
return values[index];
}
} while (true);
}

public V put(int key, V value) {
V old = insertKey(key, value);
if (free == 0) {
Expand All @@ -101,6 +74,15 @@ public V put(int key, V value) {
return old;
}

public int size() {
return capacity - free;
}

public ValuesIterator values() {
valuesIterator.index = 0;
return valuesIterator;
}

private V insertKey(int key, V value) {
int index = (key & 0x7fffffff) % keys.length;
if (values[index] == FREE) {
Expand All @@ -119,6 +101,18 @@ private V insertKey(int key, V value) {
return probeInsert(key, index, value);
}

private V probe(int key, int index) {
do {
index = (index + 1) % keys.length;
if (values[index] == FREE) {
return null;
}
if (key == keys[index]) {
return values[index];
}
} while (true);
}

private V probeInsert(int key, int index, V value) {
do {
index = (index + 1) % keys.length;
Expand All @@ -137,13 +131,24 @@ private V probeInsert(int key, int index, V value) {
} while (true);
}

public ValuesIterator values() {
valuesIterator.index = 0;
return valuesIterator;
}
@SuppressWarnings({"unchecked"})
protected void rehash() {

public void clear() {
Arrays.fill(values, FREE);
int newCapacity = Primes.next(values.length << 1);

free = this.capacity = (int) (newCapacity * loadFactor);

V[] oldValues = values;
int[] oldKeys = keys;
this.keys = new int[newCapacity];
this.values = (V[]) new Object[newCapacity];
Arrays.fill(values, 0, values.length, FREE);

for (int i = oldKeys.length; i-- > 0; ) {
if (oldValues[i] != FREE) {
insertKey(oldKeys[i], oldValues[i]);
}
}
}

public class ValuesIterator extends AbstractImmutableIterator<V> {
Expand All @@ -154,17 +159,17 @@ public boolean hasNext() {
return index < values.length && (values[index] != FREE || scan());
}

@SuppressFBWarnings({"IT_NO_SUCH_ELEMENT"})
@Override
public V next() {
return values[index++];
}

private boolean scan() {
while (index < values.length && values[index] == FREE) {
index++;
}
return index < values.length;
}

@SuppressFBWarnings({"IT_NO_SUCH_ELEMENT"})
@Override
public V next() {
return values[index++];
}
}
}
92 changes: 48 additions & 44 deletions nfsdb-core/src/main/java/com/nfsdb/collections/ObjIntHashMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2015. Vlad Ilyushchenko
* Copyright (c) 2014. Vlad Ilyushchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,22 +56,8 @@ public ObjIntHashMap(int initialCapacity, double loadFactor, int noKeyValue) {
clear();
}

@SuppressWarnings({"unchecked"})
protected void rehash() {

int newCapacity = Primes.next(values.length << 1);
free = capacity = (int) (newCapacity * loadFactor);
int[] oldValues = values;
V[] oldKeys = keys;
this.keys = (V[]) new Object[newCapacity];
this.values = new int[newCapacity];
public void clear() {
Arrays.fill(keys, FREE);

for (int i = oldKeys.length; i-- > 0; ) {
if (oldKeys[i] != FREE) {
insertKey(oldKeys[i], oldValues[i]);
}
}
}

public int get(V key) {
Expand All @@ -88,16 +74,14 @@ public int get(V key) {
return probe(key, index);
}

private int probe(V key, int index) {
do {
index = (index + 1) % keys.length;
if (keys[index] == FREE) {
return noKeyValue;
}
if (keys[index] == key || key.equals(keys[index])) {
return values[index];
}
} while (true);
public Iterable<Entry<V>> immutableIterator() {
return new EntryIterator();
}

@Override
public Iterator<Entry<V>> iterator() {
iterator.index = 0;
return iterator;
}

public int put(V key, int value) {
Expand All @@ -119,6 +103,10 @@ public boolean putIfAbsent(V key, int value) {
return !(keys[index] == key || key.equals(keys[index])) && probeInsertIfAbsent(key, index, value);
}

public int size() {
return capacity - free;
}

private int insertKey(V key, int value) {
int index = (key.hashCode() & 0x7fffffff) % keys.length;
if (keys[index] == FREE) {
Expand All @@ -140,6 +128,18 @@ private int insertKey(V key, int value) {
return probeInsert(key, index, value);
}

private int probe(V key, int index) {
do {
index = (index + 1) % keys.length;
if (keys[index] == FREE) {
return noKeyValue;
}
if (keys[index] == key || key.equals(keys[index])) {
return values[index];
}
} while (true);
}

private int probeInsert(V key, int index, int value) {
do {
index = (index + 1) % keys.length;
Expand Down Expand Up @@ -180,18 +180,22 @@ private boolean probeInsertIfAbsent(V key, int index, int value) {
} while (true);
}

public void clear() {
Arrays.fill(keys, FREE);
}
@SuppressWarnings({"unchecked"})
protected void rehash() {

public int size() {
return capacity - free;
}
int newCapacity = Primes.next(values.length << 1);
free = capacity = (int) (newCapacity * loadFactor);
int[] oldValues = values;
V[] oldKeys = keys;
this.keys = (V[]) new Object[newCapacity];
this.values = new int[newCapacity];
Arrays.fill(keys, FREE);

@Override
public Iterator<Entry<V>> iterator() {
iterator.index = 0;
return iterator;
for (int i = oldKeys.length; i-- > 0; ) {
if (oldKeys[i] != FREE) {
insertKey(oldKeys[i], oldValues[i]);
}
}
}

public static class Entry<V> {
Expand All @@ -202,26 +206,26 @@ public static class Entry<V> {
public class EntryIterator extends AbstractImmutableIterator<Entry<V>> {

private final Entry<V> entry = new Entry<>();
private int index;
private int index = 0;

@Override
public boolean hasNext() {
return index < values.length && (keys[index] != FREE || scan());
}

private boolean scan() {
while (index < values.length && keys[index] == FREE) {
index++;
}
return index < values.length;
}

@SuppressFBWarnings({"IT_NO_SUCH_ELEMENT"})
@Override
public Entry<V> next() {
entry.key = keys[index];
entry.value = values[index++];
return entry;
}

private boolean scan() {
while (index < values.length && keys[index] == FREE) {
index++;
}
return index < values.length;
}
}
}

0 comments on commit 6415aba

Please sign in to comment.