mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
CE-14 stateless wire map - toString() is now restricted to 100 entries
This commit is contained in:
@@ -199,7 +199,12 @@ class ClientWiredStatelessChronicleMap<K, V>
|
||||
|
||||
@NotNull
|
||||
public String toString() {
|
||||
return toMap().toString();
|
||||
int maxNumEntries = 100;
|
||||
if (size() > 100) {
|
||||
return toMap(maxNumEntries).toString() + "....";
|
||||
} else {
|
||||
return toMap().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -438,6 +443,53 @@ class ClientWiredStatelessChronicleMap<K, V>
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns a chunk of entries
|
||||
*
|
||||
* @return
|
||||
* @param maxNumEntries
|
||||
*/
|
||||
private Map<K, V> toMap(int maxNumEntries) {
|
||||
final Map<K, V> result = new HashMap<K, V>();
|
||||
final long startTime = System.currentTimeMillis();
|
||||
|
||||
// send
|
||||
final long transactionId = proxySend(entrySetRestricted, maxNumEntries, startTime);
|
||||
assert !hub.outBytesLock().isHeldByCurrentThread();
|
||||
final long timeoutTime = startTime + hub.timeoutMs;
|
||||
|
||||
OUTER:
|
||||
for (; ; ) {
|
||||
|
||||
// receive
|
||||
hub.inBytesLock().lock();
|
||||
try {
|
||||
final Wire wireIn = hub.proxyReply(timeoutTime, transactionId);
|
||||
|
||||
while (wireIn.bytes().remaining() > 0) {
|
||||
boolean bool = wireIn.read(Fields.hasNext).bool();
|
||||
if (bool) {
|
||||
result.put(
|
||||
readK(Fields.resultKey, wireIn, null),
|
||||
readV(Fields.resultValue, wireIn, null));
|
||||
} else
|
||||
break OUTER;
|
||||
|
||||
|
||||
if (wireIn.bytes().remaining() > 0) {
|
||||
// todo process the exception
|
||||
boolean isException = wireIn.read(Fields.isException).bool();
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
hub.inBytesLock().unlock();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callback each entry is passed to the callback
|
||||
*/
|
||||
@@ -819,6 +871,21 @@ class ClientWiredStatelessChronicleMap<K, V>
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
private long proxySend(EventId methodName, long arg1, long startTime) {
|
||||
long transactionId;
|
||||
hub.outBytesLock().lock();
|
||||
try {
|
||||
transactionId = hub.writeHeader(startTime, channelID, hub.outWire());
|
||||
hub.outWire().write(Fields.methodName).text(methodName.toString());
|
||||
hub.outWire().write(Fields.arg1).int64(arg1);
|
||||
hub.writeSocket(hub.outWire());
|
||||
} finally {
|
||||
hub.outBytesLock().unlock();
|
||||
}
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
|
||||
private boolean eventReturnsNull(@NotNull EventId methodName) {
|
||||
|
||||
switch (methodName) {
|
||||
|
||||
@@ -117,12 +117,19 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
this.publishLater = wireHandlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param transactionId the transaction id
|
||||
* @param function provides that returns items bases on a BytesChronicleMap
|
||||
* @param c an iterator that contains items
|
||||
* @param maxEntries the maximum number of items that can be written
|
||||
* @throws StreamCorruptedException
|
||||
*/
|
||||
private void writeChunked(
|
||||
long transactionId, @NotNull final Function<BytesChronicleMap, Iterator> function,
|
||||
@NotNull final Consumer<Iterator> c) throws StreamCorruptedException {
|
||||
@NotNull final Consumer<Iterator> c, long maxEntries) throws StreamCorruptedException {
|
||||
|
||||
final BytesChronicleMap m = bytesMap(channelId);
|
||||
final Iterator<byte[]> iterator = function.apply(m);
|
||||
final Iterator iterator = function.apply(m);
|
||||
|
||||
final WireHandler that = new WireHandler() {
|
||||
|
||||
@@ -132,7 +139,7 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
outWire.write(Fields.transactionId).int64(transactionId);
|
||||
|
||||
// this allows us to write more data than the buffer will allow
|
||||
for (; ; ) {
|
||||
for (int count = 0; count < maxEntries; count++) {
|
||||
|
||||
final boolean hasNext = iterator.hasNext();
|
||||
|
||||
@@ -140,8 +147,10 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
|
||||
outWire.write(Fields.hasNext).bool(hasNext);
|
||||
|
||||
if (hasNext)
|
||||
if (hasNext) {
|
||||
c.accept(iterator);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
if (!hasNext)
|
||||
@@ -162,6 +171,13 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
|
||||
}
|
||||
|
||||
private void writeChunked(
|
||||
long transactionId, @NotNull final Function<BytesChronicleMap, Iterator> function,
|
||||
@NotNull final Consumer<Iterator> c) throws StreamCorruptedException {
|
||||
writeChunked(transactionId, function, c, Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void process(Wire in, Wire out) throws StreamCorruptedException {
|
||||
try {
|
||||
@@ -218,6 +234,12 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (entrySetRestricted.contentEquals(methodName)) {
|
||||
long maxEntries = inWire.read(arg1).int64();
|
||||
writeChunked(transactionId, m -> m.delegate.entrySet().iterator(), writeEntry,maxEntries);
|
||||
return;
|
||||
}
|
||||
|
||||
if (putAll.contentEquals(methodName)) {
|
||||
putAll(transactionId);
|
||||
return;
|
||||
@@ -803,6 +825,7 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
|
||||
keySet,
|
||||
values,
|
||||
entrySet,
|
||||
entrySetRestricted,
|
||||
replace,
|
||||
replaceWithOldAndNewValue,
|
||||
putIfAbsent,
|
||||
|
||||
Reference in New Issue
Block a user