public class K2hashKeyQueue extends Object implements Closeable
The difference of KeyQueue and Queue: KeyQueue stores references to keys in k2hash database whereas Queue holds any kinds of elements on the other.
Usage Examples. Suppose you want to make a new data collection from current keys. KeyQueue is very useful because current keys will not be deleted after removing date from keyQueue. You can use KeyQueue as a temporary data container. You could write this as:
package ax.antpick;
import IOException;
import java.util.*;
import java.util.stream.*;
import java.math.*;
public class App {
public static void main(String[] args) {
// 1. setvalues
HashMap<String, String> data = new HashMap<String, String>() {{ put("k1","v1"); put("k2","v2"); put("k3","v3");}};
try (K2hash db = K2hash.of("App.k2h")) {
for(Map.Entry<String, String> entry : data.entrySet()) {
db.setValue(entry.getKey(), entry.getValue()); // setValue
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
return;
}
// 2. offer to KeyQueue
try (K2hash db = K2hash.of("App.k2h")) {
long handle = db.getHandle();
K2hashKeyQueue queue = K2hashKeyQueue.of(handle);
HashMap<String, String> k1v1 = new HashMap<String, String>() {{ put("k1","v1"); }};
HashMap<String, String> k2v2 = new HashMap<String, String>() {{ put("k2","v3"); }};
HashMap<String, String> k3v3 = new HashMap<String, String>() {{ put("k3","v3"); }};
queue.offer(k1v1);
queue.offer(k2v2);
queue.offer(k3v3);
queue.offer(k3v3);
queue.offer(k2v2);
queue.offer(k1v1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
return;
}
// 3. peek from KeyQueue
try (K2hash db = K2hash.of("App.k2h")) {
long handle = db.getHandle();
K2hashKeyQueue queue = K2hashKeyQueue.of(handle);
Map<String,String> val = null;
do {
if( (val = queue.poll()) != null) {
System.out.println(val.toString());
}
} while (val != null);
} catch (IOException ex) {
System.out.println(ex.getMessage());
return;
}
}
}
| Modifier and Type | Field and Description |
|---|---|
static K2hashAttrPack[] |
DEFAULT_ATTRPACK
Default K2hashAttrPack array is
null. |
static int |
DEFAULT_ATTRPACK_SIZE
Default size of a K2hashAttrPack array is
0. |
static long |
DEFAULT_EXPIRATION_DURATION
Default data expiration duration is
0. |
static boolean |
DEFAULT_FIFO
Default FIFO is
true. |
static String |
DEFAULT_PASS
Default passphrase string is
null. |
static int |
DEFAULT_POSITION
Default position of a peek operation is
0. |
static String |
DEFAULT_PREFIX
Default prefix string is
null. |
static long |
K2H_INVALID_HANDLE
Default k2hash data handle.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(Object obj)
Inserts an element into the tail of this queue.
|
void |
clear()
Removes all of the elements from this collection (optional operation).
|
void |
close()
Free KeyQueueHandle
|
long |
count()
Returns the number of queue.
|
Map<String,String> |
element()
Finds and gets a object from the head of this queue.
|
Map<String,String> |
element(int position)
Finds and gets a object from the head of this queue.
|
long |
getQueueHandle()
Returns a K2hashKeyQueue handle.
|
boolean |
isEmpty()
Returns true if, and only if, queue size is 0.
|
static K2hashKeyQueue |
of(long handle)
Constructs a K2hashKeyQueue instance.
|
static K2hashKeyQueue |
of(long handle,
boolean fifo)
Constructs a K2hashKeyQueue instance.
|
static K2hashKeyQueue |
of(long handle,
boolean fifo,
String prefix)
Constructs a K2hashKeyQueue instance.
|
static K2hashKeyQueue |
of(long handle,
boolean fifo,
String prefix,
String pass,
long expirationDuration)
Constructs a K2hashKeyQueue instance.
|
boolean |
offer(Object obj)
Inserts an element into the tail of this queue.
|
Map<String,String> |
peek()
Finds and gets a object at the head of this queue.
|
Map<String,String> |
peek(int position)
Finds and gets a object from the head of this queue.
|
Map<String,String> |
poll()
Finds and gets a object from the head of this queue.
|
void |
print()
Print the objects in this queue.
|
Map<String,String> |
remove()
Finds and removes the object at the top of this queue and returns the object.
|
boolean |
remove(Object o)
Removes the object(key) at the top of this queue and the object(value) outside of this queue
This remove method is derived from the Collection interface.
|
List<Map<String,String>> |
removeList(long count)
Removes the objects from this queue.
|
String |
toString() |
public static final long K2H_INVALID_HANDLE
public static boolean DEFAULT_FIFO
true.public static String DEFAULT_PREFIX
null.public static int DEFAULT_POSITION
0.public static String DEFAULT_PASS
null.public static long DEFAULT_EXPIRATION_DURATION
0.public static K2hashAttrPack[] DEFAULT_ATTRPACK
null.public static int DEFAULT_ATTRPACK_SIZE
0.public static K2hashKeyQueue of(long handle) throws IOException
handle - a K2hash data handleIOException - if a K2hash data handle is invalidpublic static K2hashKeyQueue of(long handle, boolean fifo) throws IOException
handle - a K2hash data handlefifo - true if elements are in a FIFO (first-in-first-out) mannerIOException - if a K2hash data handle is invalidpublic static K2hashKeyQueue of(long handle, boolean fifo, String prefix) throws IOException
handle - a K2hash data handlefifo - true if elements are in a FIFO (first-in-first-out) mannerprefix - a prefix string of this queueIOException - if a K2hash data handle is invalidpublic static K2hashKeyQueue of(long handle, boolean fifo, String prefix, String pass, long expirationDuration) throws IOException
handle - a K2hash data handlefifo - true if elements are in a FIFO (first-in-first-out) mannerprefix - a prefix string of this queuepass - a passphrase to access dataexpirationDuration - a duration to expire data in secondsIOException - if a K2hash data handle is invalidIllegalArgumentException - if position is less than zeropublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionpublic long getQueueHandle()
public boolean isEmpty()
true if empty false otherwisepublic long count()
public Map<String,String> peek()
public Map<String,String> peek(int position)
position - a position to peek in this queueIllegalArgumentException - if the position is less than zeropublic boolean offer(Object obj)
obj - an object to be inserted to this queuetrue if success false otherwiseIllegalArgumentException - - if arguments are illegal.public Map<String,String> poll()
public boolean add(Object obj)
obj - an object to be inserted into this queuetrue if success false otherwiseNoSuchElementException - - if this queue is emptypublic Map<String,String> remove()
NoSuchElementException - - if this queue is emptypublic boolean remove(Object o)
o - element to be removed from this collection, if presentpublic List<Map<String,String>> removeList(long count)
count - the number of objects to be removedNoSuchElementException - - if this queue is emptyIllegalArgumentException - if an illegal augment existspublic Map<String,String> element()
NoSuchElementException - - if this queue is emptypublic Map<String,String> element(int position)
position - position where the data exists in queueNoSuchElementException - - if this queue is emptyIllegalArgumentException - if an illegal augment existspublic void clear()
public void print()
NoSuchElementException - - if this queue is emptyCopyright © 2021. All rights reserved.