mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Full proof read and edit
This commit is contained in:
+119
-117
@@ -74,15 +74,15 @@ this JVM process, rather than creating a new Chronicle Map data store. That mean
|
||||
|
||||
In this tutorial, the term `ChronicleMap` instance (or simply `ChronicleMap`) is used to refer to an on-heap object providing access to a Chronicle Map data store (or Chronicle Map key-value store, or Chronicle Map store, or simply Chronicle Map, with space between two words in contrast to `ChronicleMap`), which could be purely in-memory, or persisted to disk.
|
||||
|
||||
Currently the Java implementation doesn't allow creation of multiple accessor ChronicleMap` objects for a single *in-memory Chronicle Map store*; there is always a one-to-one correspondence.
|
||||
Currently the Java implementation doesn't allow creation of multiple accessor ChronicleMap` objects for a single *in-memory Chronicle Map store*; there is always a one-to-one relationship.
|
||||
|
||||
*Persisted Chronicle Map store*, however, does allow creation of multiple accessor `ChronicleMap` instances; either within a single JVM process (although it is not recommended), or from concurrent JVM processes.
|
||||
A *persisted Chronicle Map store*, however, does allow creation of multiple accessor `ChronicleMap` instances; either within a single JVM process (although it is not recommended), or from concurrent JVM processes.
|
||||
|
||||
When no processes access the file, it could be freely moved to another location in the system, or
|
||||
When no processes access the file, it could be moved to another location in the system, or
|
||||
even to another server. It could even run on different operating systems. When opened from another location you will observe the same data.
|
||||
|
||||
If you don't need the Chronicle Map instance to survive the server restart, i. e. you don't need
|
||||
persistence to disk, only multi-process access, choose the file to be mounted on http://en.wikipedia.org/wiki/Tmpfs[tmpfs]. For example, on `Linux` it is as easy as placing you file in `/dev/shm` directory.
|
||||
If you don't need the Chronicle Map instance to survive the server restart (that is, you don't need
|
||||
persistence to disk; only multi-process access), mount the file on http://en.wikipedia.org/wiki/Tmpfs[tmpfs]. For example, on `Linux` it is as easy as placing you file in `/dev/shm` directory.
|
||||
|
||||
|
||||
== Configure entries
|
||||
@@ -94,28 +94,34 @@ You should not put additional margin over the actual target number of entries. T
|
||||
See `ChronicleMapBuilder#entries()` in http://www.javadoc.io/doc/net.openhft/chronicle-map/[Javadocs] for more information.
|
||||
|
||||
|
||||
Once `ChronicleMap` instance is created, its configurations are sealed and cannot be changed
|
||||
though the `ChronicleMapBuilder` instance.
|
||||
Once a `ChronicleMap` instance is created, its configurations are sealed and cannot be changed
|
||||
using the `ChronicleMapBuilder` instance.
|
||||
|
||||
|
||||
== Single `ChronicleMap` instance per JVM
|
||||
If you want to access a Chronicle Map data store
|
||||
concurrently within a Java process, you should not create a separate `ChronicleMap` instance per thread. Within the JVM environment, `ChronicleMap` instance is a `ConcurrentMap`, and could be accessed concurrently the same way as, for example, `ConcurrentHashMap`.
|
||||
concurrently within a Java process, you should not create a separate `ChronicleMap` instance for each thread. Within the JVM environment, a `ChronicleMap` instance is a `ConcurrentMap`, and could be accessed concurrently the same way as, for example, a `ConcurrentHashMap`.
|
||||
|
||||
== Recovery
|
||||
|
||||
If a process, accessing a persisted Chronicle Map, terminated abnormally: crashed, `SIGKILL`ed, or
|
||||
terminated because the host operating system crashed, or the machine lost power, the Chronicle Map
|
||||
might remain in an inaccessible or corrupted state. When the Chronicle Map is opened next time from
|
||||
another process, it should be done via `.recoverPersistedTo()` method in `ChronicleMapBuilder`.
|
||||
Unlike `createPersistedTo()`, this method scans all memory of Chronicle Map store for
|
||||
inconsistencies, if some found, it cleans them up.
|
||||
If a process, accessing a persisted Chronicle Map, terminated abnormally, for example:
|
||||
|
||||
- crashed
|
||||
- `SIGKILL`ed
|
||||
- terminated because the host operating system crashed
|
||||
- terminated because the host machine lost power
|
||||
|
||||
then the Chronicle Map may remain in an inaccessible or corrupted state.
|
||||
|
||||
When the Chronicle Map is opened next time from another process, it should be done using `.recoverPersistedTo()` method in `ChronicleMapBuilder`.
|
||||
|
||||
Unlike `createPersistedTo()`, this method scans all the memory of the Chronicle Map store for
|
||||
inconsistencies and, if any are found, it cleans them up.
|
||||
|
||||
`.recoverPersistedTo()` needs to access the Chronicle Map exclusively. If a concurrent process is
|
||||
accessing the Chronicle Map while another process is attempting to perform recovery, result of
|
||||
operations on the accessing process side, and results of recovery are unspecified. The data could be
|
||||
corrupted further. You must ensure no other process is accessing the Chronicle Map store when
|
||||
calling for `.recoverPersistedTo()` on this store.
|
||||
accessing the Chronicle Map while another process is attempting to perform recovery, the results of
|
||||
operations on the accessing process side, and results of recovery are unspecified; the data could be corrupted further. You must ensure thst no other process is accessing the Chronicle Map store when
|
||||
calling `.recoverPersistedTo()`.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -129,14 +135,17 @@ ChronicleMap<Integer, PostalCodeRange> cityPostalCodes = ChronicleMap
|
||||
```
|
||||
|
||||
The second parameter in `recoverPersistedTo()` method is called
|
||||
`sameBuilderConfigAndLibraryVersion`, it could be `true` only if `ChronicleMapBuilder` is configured
|
||||
`sameBuilderConfigAndLibraryVersion`. It has two possible values:
|
||||
|
||||
- `true` - if `ChronicleMapBuilder` is configured
|
||||
in exactly the same way, as when the Chronicle Map (persisted to the given file) was created, and
|
||||
using the same version of the Chronicle Map library, or `false`, if initial configurations are not
|
||||
known, or current version of Chronicle Map library could be different from the version, used to
|
||||
create this Chronicle Map initially.
|
||||
using the same version of the Chronicle Map library
|
||||
- `false` - if the initial configuration is not
|
||||
known, or the current version of Chronicle Map library could be different from the version originally used to
|
||||
create this Chronicle Map.
|
||||
|
||||
If `sameBuilderConfigAndLibraryVersion` is `true`, `recoverPersistedTo()` "knows" all the right
|
||||
configurations and what should be written to the header. It checks if the recovered Chronicle Map's
|
||||
configurations, and what should be written to the header. It checks if the recovered Chronicle Map's
|
||||
header memory (containing serialized configurations) is corrupted or not. If the header is
|
||||
corrupted, it is overridden, and the recovery process continues.
|
||||
|
||||
@@ -146,16 +155,14 @@ corrupted, `ChronicleHashRecoveryFailedException` is thrown.
|
||||
|
||||
However, the subject header memory is never updated on ordinary operations with Chronicle Map, so it
|
||||
couldn't be corrupted if an accessing process crashed, or the operating system crashed, or even the
|
||||
machine lost power. Only hardware memory or disk corruption or a bug in the file system could lead
|
||||
machine lost power. Only hardware, memory, or disk corruption, or a bug in the file system, could lead
|
||||
to Chronicle Map header memory corruption.
|
||||
|
||||
`.recoverPersistedTo()` is harmless if the previous process accessing the Chronicle Map terminated
|
||||
normally, however this is a computationally expensive procedure that should generally be avoided.
|
||||
normally; however this is a computationally expensive procedure that should generally be avoided.
|
||||
|
||||
Chronicle Map creation and recovery could be conveniently merged using a single call:
|
||||
`.createOrRecoverPersistedTo(persistenceFile, sameLibraryVersion)` in `ChronicleMapBuilder`, which
|
||||
acts like `createPersistedTo(persistenceFile)`, if the persistence file doesn't yet exist, and like
|
||||
`recoverPersistedTo(persistenceFile, sameLibraryVersion)`, if the file already exists, e. g.:
|
||||
Chronicle Map creation and recovery could be conveniently merged using a single call, `.createOrRecoverPersistedTo(persistenceFile, sameLibraryVersion)` in `ChronicleMapBuilder`. This acts like `createPersistedTo(persistenceFile)` if the persistence file doesn't yet exist, and like
|
||||
`recoverPersistedTo(persistenceFile, sameLibraryVersion)`, if the file already exists. For example:
|
||||
|
||||
```java
|
||||
ChronicleMap<Integer, PostalCodeRange> cityPostalCodes = ChronicleMap
|
||||
@@ -165,46 +172,42 @@ ChronicleMap<Integer, PostalCodeRange> cityPostalCodes = ChronicleMap
|
||||
.createOrRecoverPersistedTo(cityPostalCodesFile, false);
|
||||
```
|
||||
|
||||
If the Chronicle Map is configured to store entry checksums along with entries, recovery procedure
|
||||
checks for each entry that the checksums is correct, otherwise it assumes the entry is corrupted and
|
||||
deletes it from the Chronicle Map. If checksums are to stored, recovery procedure cannot guarantee
|
||||
correctness of entry data. See [Entry checksums](#entry-checksums) section for more information.
|
||||
If the Chronicle Map is configured to store entry checksums along with entries, then the recovery procedure checks that the checksum is correct for each entry.
|
||||
|
||||
Otherwise, it assumes the entry is corrupted and deletes it from the Chronicle Map. If checksums are to be stored, the recovery procedure cannot guarantee
|
||||
correctness of the entry data. See [Entry checksums](#entry-checksums) section for more information.
|
||||
|
||||
== Key and Value Types
|
||||
|
||||
Either key or value type of `ChronicleMap<K, V>` could be:
|
||||
The key, or value type, of `ChronicleMap<K, V>` could be:
|
||||
|
||||
- Types with best possible out of the box support:
|
||||
- Any [value interface](https://github.com/OpenHFT/Chronicle-Values)
|
||||
- Any class implementing [`Byteable`](
|
||||
http://openhft.github.io/Chronicle-Bytes/apidocs/net/openhft/chronicle/bytes/Byteable.html)
|
||||
interface from [Chronicle Bytes](https://github.com/OpenHFT/Chronicle-Bytes)
|
||||
- Any class implementing [`BytesMarshallable`](
|
||||
http://openhft.github.io/Chronicle-Bytes/apidocs/net/openhft/chronicle/bytes/BytesMarshallable.html)
|
||||
- Types with best possible out-of-the-box support:
|
||||
** Any https://github.com/OpenHFT/Chronicle-Values[value interface]
|
||||
** Any class implementing http://openhft.github.io/Chronicle-Bytes/apidocs/net/openhft/chronicle/bytes/Byteable.html[`Byteable`] interface from https://github.com/OpenHFT/Chronicle-Bytes[Chronicle Bytes].
|
||||
** Any class implementing http://openhft.github.io/Chronicle-Bytes/apidocs/net/openhft/chronicle/bytes/BytesMarshallable.html[`BytesMarshallable`].
|
||||
interface from Chronicle Bytes. The implementation class should have a public no-arg constructor.
|
||||
- `byte[]` and `ByteBuffer`
|
||||
- `CharSequence`, `String` and `StringBuilder`. Note that these char sequence types are
|
||||
** `byte[]` and `ByteBuffer`
|
||||
** `CharSequence`, `String` and `StringBuilder`. Note that these char sequence types are
|
||||
serialized using UTF-8 encoding by default. If you need a different encoding, refer to the
|
||||
example in the [custom `CharSequence` encoding](#custom-charsequence-encoding) section.
|
||||
- `Integer`, `Long` and `Double`
|
||||
example in the <<CM_Tutorial.adoc#custom-charsequence-encoding,custom `CharSequence` encoding>> section.
|
||||
** `Integer`, `Long` and `Double`
|
||||
|
||||
- Types supported out of the box, but not particularly efficiently. You might want to implement
|
||||
more efficient [custom serializers](#custom-serializers) for them:
|
||||
- Any class implementing `java.io.Externalizable`. The implementation class should have a public
|
||||
no-arg constructor.
|
||||
- Any type implementing `java.io.Serializable`, including boxed primitive types (except listed
|
||||
above) and array types
|
||||
- Types supported out-of-the-box, but that are not particularly efficiently. You may want to implement
|
||||
more efficient <<CM_Tutorial.adoc#custom-serializers,custom serializers>> for them:
|
||||
** Any class implementing `java.io.Externalizable`. The implementation class should have a public `no-arg` constructor.
|
||||
** Any type implementing `java.io.Serializable`, including boxed primitive types (except those listed
|
||||
above) and array types.
|
||||
|
||||
- Any other type, if [custom serializers](#custom-serializers) are provided.
|
||||
- Any other type, if <<CM_Tutorial.adoc#custom-serializers,custom serializers>> are provided.
|
||||
|
||||
https://github.com/OpenHFT/Chronicle-Values)[value interfaces] are preferred as they don't generate garbage, and have close to zero serialization/deserialization costs. They are preferable even to boxed primitives. For example, try to use `net.openhft.chronicle.core.values.IntValue` instead of `Integer`.
|
||||
https://github.com/OpenHFT/Chronicle-Values)[Value interfaces] are preferred as they do not generate garbage, and have close to zero serialization/deserialization costs. They are preferable even to boxed primitives. For example, try to use `net.openhft.chronicle.core.values.IntValue` instead of `Integer`.
|
||||
|
||||
Generally, you must hint the `ChronicleMapBuilder` with the average sizes of the keys and values, which are going to be inserted into the `ChronicleMap`. This is needed to allocate the proper volume of the shared memory. Do this using `averageKey()` (preferred) or `averageKeySize()`, and
|
||||
Generally, you must provide hints for the `ChronicleMapBuilder` with the average sizes of the keys and the values, which are going to be inserted into the `ChronicleMap`. This is required in order to allocate the proper amount of shared memory. Do this using `averageKey()` (preferred) or `averageKeySize()`, and
|
||||
`averageValue()` or `averageValueSize()` respectively.
|
||||
|
||||
In the example above, `averageKey("Amsterdam")` is called, because it is assumed that "Amsterdam" (9 bytes in UTF-8 encoding) is the average length for city names; some names are shorter (Tokyo, 5 bytes), some names are longer (San Francisco, 13 bytes).
|
||||
In the example above, `averageKey("Amsterdam")` is called, because it is assumed that "Amsterdam" (9 bytes in UTF-8 encoding) is the average length for city names. Some names are shorter (Tokyo, 5 bytes), some names are longer (San Francisco, 13 bytes).
|
||||
|
||||
Another example could be if values in your `ChronicleMap` are adjacency lists of some social graph, where nodes are represented as `long` ids, and adjacency lists are `long[]` arrays. The average number of friends is 150. You could configure the `ChronicleMap` as follows:
|
||||
Another example could be if values in your `ChronicleMap` are adjacency lists of some social graph, where nodes are represented as `long` identifiers, and adjacency lists are `long[]` arrays. For example, if the average number of friends is 150, you could configure the `ChronicleMap` as follows:
|
||||
|
||||
```java
|
||||
Map<Long, long[]> socialGraph = ChronicleMap
|
||||
@@ -215,9 +218,9 @@ Map<Long, long[]> socialGraph = ChronicleMap
|
||||
.create();
|
||||
```
|
||||
|
||||
You could omit specifying key or value average sizes, if their types are boxed Java primitives or value interfaces. They are constantly-sized and Chronicle Map knows about that.
|
||||
You could omit specifying key, or value, average sizes, if their types are boxed Java primitives or value interfaces. They are constantly-sized and Chronicle Map knows about that.
|
||||
|
||||
If the key or value type is constantly sized, or keys or values only of a certain size appear in your Chronicle Map domain, you should prefer to configure `constantKeySizeBySample()` or
|
||||
If the key or value type is constantly sized, or keys or values only of a certain size appear in your Chronicle Map domain, then preferably you should configure `constantKeySizeBySample()` or
|
||||
`constantValueSizeBySample()`, instead of `averageKey()` or `averageValue()`. For example:
|
||||
|
||||
```java
|
||||
@@ -232,9 +235,9 @@ ChronicleSet<UUID> uuids =
|
||||
|
||||
== Custom serializers
|
||||
|
||||
Chronicle Map allows you to configure custom marshallers for key or value types which are not supported out of the box. You can also serialize supported types like `String` in some custom way (encoded other than UTF-8), or serialize supported types more efficiently than by default.
|
||||
Chronicle Map allows you to configure custom marshallers for key or value types which are not supported out-of-the-box. You can also serialize supported types like `String` in some custom way (encoded other than UTF-8), or serialize supported types more efficiently than by default.
|
||||
|
||||
There are three pairs of serialization interfaces, only one of them should be chosen in a single implementation, and supplied to the `ChronicleMapBuilder` for the key or value type. These are:
|
||||
There are three pairs of serialization interfaces. Only one of them should be chosen in a single implementation, and supplied to the `ChronicleMapBuilder` for the key or value type. These are:
|
||||
|
||||
- link:CM_Tutorial_Bytes.adoc[BytesWriter and BytesReader]
|
||||
- link:CM_Tutorial_Sized.adoc[SizedWriter and SizedReader]
|
||||
@@ -292,14 +295,14 @@ There are three pairs of serialization interfaces, only one of them should be ch
|
||||
|
||||
=== Single-key queries
|
||||
|
||||
First of all, `ChronicleMap` supports all operations from [`Map`](
|
||||
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html): `get()`, `put()`, etc, including
|
||||
methods added in Java 8, like `compute()` and `merge()`, and [`ConcurrentMap`](
|
||||
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html) interfaces:
|
||||
`putIfAbsent()`, `replace()`. All operations, including those which include "two steps", e. g.
|
||||
`compute()`, are correctly synchronized in terms of `ConcurrentMap` interface.
|
||||
`ChronicleMap` supports all operations from:
|
||||
|
||||
This means, you could use `ChronicleMap` instance just like a `HashMap` or `ConcurrentHashMap`:
|
||||
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html[`Map`] interfaces; `get()`, `put()`, etc, including
|
||||
methods added in Java 8, like `compute()` and `merge()`, and
|
||||
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html[`ConcurrentMap`] interfaces;
|
||||
`putIfAbsent()`, `replace()`.
|
||||
|
||||
All operations, including those which include "two-steps", for example, `compute()`, are correctly synchronized in terms of the `ConcurrentMap` interface. This means that you could use a `ChronicleMap` instance just like a `HashMap` or `ConcurrentHashMap`.
|
||||
|
||||
```java
|
||||
PostalCodeRange amsterdamCodes = Values.newHeapInstance(PostalCodeRange.class);
|
||||
@@ -313,7 +316,7 @@ PostalCodeRange amsterdamCodes = cityPostalCodes.get("Amsterdam");
|
||||
```
|
||||
|
||||
However, this approach often generates garbage, because the values should be deserialized from
|
||||
off-heap memory to on-heap, the new value object are allocated. There are several possibilities to
|
||||
off-heap memory to on-heap memory when the new value objects are allocated. There are several possibilities to
|
||||
reuse objects efficiently:
|
||||
|
||||
==== Value interfaces instead of boxed primitives
|
||||
@@ -349,10 +352,8 @@ for (long id : orderIds) {
|
||||
|
||||
==== `chronicleMap.getUsing()`
|
||||
|
||||
Use `ChronicleMap#getUsing(K key, V using)` to reuse the value object. It works if:
|
||||
Use `ChronicleMap#getUsing(K key, V using)` to reuse the value object. It works if the value type is `CharSequence`. Pass `StringBuilder` as the `using` argument. For example:
|
||||
|
||||
- The value type is `CharSequence`, pass `StringBuilder` as the `using` argument.
|
||||
For example:
|
||||
```java
|
||||
ChronicleMap<LongValue, CharSequence> names = ...
|
||||
StringBuilder name = new StringBuilder();
|
||||
@@ -363,15 +364,15 @@ Use `ChronicleMap#getUsing(K key, V using)` to reuse the value object. It works
|
||||
}
|
||||
```
|
||||
|
||||
In this case, calling `names.getUsing(key, name)` is equivalent to
|
||||
In this case, calling `names.getUsing(key, name)` is equivalent to:
|
||||
|
||||
```java
|
||||
name.setLength(0);
|
||||
name.append(names.get(key));
|
||||
```
|
||||
|
||||
with the difference that it doesn't generate garbage.
|
||||
- The value type is value interface, pass heap instance to read the data into it without new object
|
||||
allocation:
|
||||
The difference is that it doesn't generate garbage. The value type is the value interface. Pass the heap instance to read the data into it without new object allocation:
|
||||
|
||||
```java
|
||||
ThreadLocal<PostalCodeRange> cachedPostalCodeRange =
|
||||
ThreadLocal.withInitial(() -> Values.newHeapInstance(PostalCodeRange.class));
|
||||
@@ -382,15 +383,18 @@ Use `ChronicleMap#getUsing(K key, V using)` to reuse the value object. It works
|
||||
cityPostalCodes.getUsing(city, range);
|
||||
// process the range...
|
||||
```
|
||||
- If the value type implements `BytesMarshallable`, or `Externalizable`, `ChronicleMap` attempts to
|
||||
|
||||
- If the value type implements `BytesMarshallable`, or `Externalizable`, then `ChronicleMap` attempts to
|
||||
reuse the given `using` object by deserializing the value into the given object.
|
||||
|
||||
- If custom marshaller is configured in the `ChronicleMapBuilder` via `.valueMarshaller()`,
|
||||
`ChronicleMap` attempts to reuse the given object by calling `readUsing()` method from the
|
||||
then `ChronicleMap` attempts to reuse the given object by calling the `readUsing()` method from the
|
||||
marshaller interface.
|
||||
|
||||
If `ChronicleMap` fails to reuse the object in `getUsing()`, it makes no harm, it falls back to
|
||||
object creation, like in `get()` method. In particular, even `null` is allowed to be passed as
|
||||
`using` object. It allows "lazy" using object initialization pattern:
|
||||
If `ChronicleMap` fails to reuse the object in `getUsing()`, it does no harm. It falls back to
|
||||
object creation, as in the `get()` method. In particular, even `null` is allowed to be passed as
|
||||
`using` object. It allows a "lazy" using object initialization pattern:
|
||||
|
||||
```java
|
||||
// a field
|
||||
PostalCodeRange cachedRange = null;
|
||||
@@ -401,15 +405,14 @@ PostalCodeRange cachedRange = null;
|
||||
cachedRange = cityPostalCodes.getUsing(city, cachedRange);
|
||||
// process the range...
|
||||
```
|
||||
In this example, `cachedRange` is `null` initially, on the first `getUsing()` call the heap value
|
||||
is allocated, and saved in a `cachedRange` field for later reuse.
|
||||
In this example, `cachedRange` is `null` initially. On the first `getUsing()` call, the heap value
|
||||
is allocated and saved in a `cachedRange` field for later reuse.
|
||||
|
||||
<i>If the value type is a value interface, <b>don't</b> use flyweight implementation as `getUsing()`
|
||||
argument.</i> This is dangerous, because on reusing flyweight points to the `ChronicleMap` memory
|
||||
directly, but the access is not synchronized. At least you could read inconsistent value state,
|
||||
at most - corrupt the `ChronicleMap` memory.
|
||||
NOTE: If the value type is a value interface, **do not** use flyweight implementation as the `getUsing()` argument. This is dangerous, because on reusing flyweight points to the `ChronicleMap` memory
|
||||
directly, but the access is not synchronized. At best you could read inconsistent value state;
|
||||
at worst you could corrupt the `ChronicleMap` memory.
|
||||
|
||||
For accessing the `ChronicleMap` value memory directly use the following technique:
|
||||
For accessing the `ChronicleMap` value memory directly use the following techniques.
|
||||
|
||||
==== Working with an entry within a context
|
||||
|
||||
@@ -432,7 +435,8 @@ try (ExternalMapQueryContext<CharSequence, PostalCodeRange, ?> c =
|
||||
|
||||
=== Multi-key queries
|
||||
|
||||
In this example, consistent graph edge addition and removals are implemented via multi-key queries:
|
||||
In this example, consistent graph edge addition and removals are implemented using multi-key queries:
|
||||
|
||||
```java
|
||||
public static boolean addEdge(
|
||||
ChronicleMap<Integer, Set<Integer>> graph, int source, int target) {
|
||||
@@ -562,6 +566,7 @@ private static boolean innerRemoveEdge(
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```java
|
||||
HashSet<Integer> averageValue = new HashSet<>();
|
||||
for (int i = 0; i < AVERAGE_CONNECTIVITY; i++) {
|
||||
@@ -579,21 +584,22 @@ removeEdge(graph, 1, 2);
|
||||
```
|
||||
|
||||
== Close `ChronicleMap`
|
||||
Unlike ConcurrentHashMap, ChronicleMap stores its data off heap, often in a memory mapped file.
|
||||
Its recommended that you call close() once you have finished working with a ChronicleMap.
|
||||
Unlike `ConcurrentHashMap`, `ChronicleMap` stores its data off-heap; often in a memory mapped file.
|
||||
It is recommended that you call `close()` when you have finished working with a `ChronicleMap`.
|
||||
|
||||
``` java
|
||||
map.close()
|
||||
```
|
||||
|
||||
This is especially important when working with ChronicleMap replication, as failure to call close may prevent
|
||||
you from restarting a replicated map on the same port. In the event that your application crashes it may not
|
||||
be possible to call close(). Your operating system will usually close dangling ports automatically,
|
||||
so although it is recommended that you close() when you have finished with the map,
|
||||
its not something that you must do, it's just something that we recommend you should do.
|
||||
This is especially important when working with Chronicle Map replication, as failure to call close may prevent
|
||||
you from restarting a replicated map on the same port.
|
||||
|
||||
WARNING: *WARNING*. If you call `close()` too early before you have finished working with the map, this can cause
|
||||
your JVM to crash. Close MUST BE the last thing that you do with the map.
|
||||
In the event that your application crashes, it may not
|
||||
be possible to call `close()`. Your operating system will usually close dangling ports automatically. So, although it is recommended that you `close()` when you have finished with the map,
|
||||
it is not something that you must do; it's just something that we recommend you should do.
|
||||
|
||||
WARNING: If you call `close()` too early before you have finished working with the map, this can cause
|
||||
your JVM to crash. Close **MUST** be the last thing that you do with the map.
|
||||
|
||||
== Behaviour Customization
|
||||
|
||||
@@ -604,31 +610,27 @@ See <<CM_Tutorial_Behaviour.adoc#,CM_Tutorial_Behaviour.adoc>> for more details.
|
||||
|
||||
== Entry checksums
|
||||
|
||||
Chronicle Map 3 is able to store entry checksums along with entries. With entry checksums it is
|
||||
possible to identify partially written entries (in case of operating system or power failure while)
|
||||
and corrupted entries (in case of hardware memory or disk corruption) and clean them up during the <<CM_Tutorial.adoc#recovery,recovery>> procedure.
|
||||
Chronicle Map is able to store entry checksums along with entries. With entry checksums it is
|
||||
possible to identify partially written entries (in the case of operating system, or power failure),
|
||||
and corrupted entries (in the case of hardware, memory, or disk corruption) and clean them up during the <<CM_Tutorial.adoc#recovery,recovery>> procedure.
|
||||
|
||||
Entry checksums are 32-bit numbers, computed by a hash function with good avalanche effect.
|
||||
Theoretically there is still about a one-billionth chance that after entry corruption it passes the
|
||||
sum check.
|
||||
Entry checksums are `32-bit` numbers, computed by a hash function with good avalanche effect.
|
||||
Theoretically, there is still about a one-in-a-billion chance that after entry corruption, it passes the sum check.
|
||||
|
||||
By default, entry checksums are ON if the Chronicle Map is persisted to disk (i. e. created via
|
||||
`createPersistedTo()` method), and OFF if the Chronicle Map is purely in-memory. Storing checksums
|
||||
for a purely in-memory Chronicle Map hardly makes any practical sense, but you might want to disable
|
||||
storing checksums for a persisted Chronicle Map by calling `.checksumEntries(false)` on the
|
||||
`ChronicleMapBuilder` used to create a map. It makes sense if you don't need extra safety checksums
|
||||
provide.
|
||||
By default, entry checksums are:
|
||||
|
||||
- **`ON`** if the Chronicle Map is persisted to disk (i. e. created via
|
||||
`createPersistedTo()` method)
|
||||
- **`OFF`** if the Chronicle Map is purely in-memory.
|
||||
|
||||
Storing checksums for a purely in-memory Chronicle Map hardly makes any practical sense, but you might want to disable storing checksums for a persisted Chronicle Map by calling `.checksumEntries(false)` on the `ChronicleMapBuilder` used to create a map. It makes sense if you don't need extra safety that checksums provide.
|
||||
|
||||
Entry checksums are computed automatically when an entry is inserted into a Chronicle Map, and
|
||||
re-computed automatically on operations which update the whole value, e. g. `map.put()`,
|
||||
`map.replace()`, `map.compute()`, `mapEntry.doReplaceValue()` (See `MapEntry` interface in
|
||||
[Javadocs](http://www.javadoc.io/doc/net.openhft/chronicle-map/). But if you update values directly,
|
||||
bypassing Chronicle Map logic, keeping entry checksum up-to-date is also your responsibility.
|
||||
re-computed automatically on operations which update the whole value. For example, `map.put()`,
|
||||
`map.replace()`, `map.compute()`, `mapEntry.doReplaceValue()`. See the `MapEntry` interface in
|
||||
http://www.javadoc.io/doc/net.openhft/chronicle-map/[Javadocs]. If you update values directly, bypassing Chronicle Map logic, keeping the entry checksum up-to-date is also your responsibility.
|
||||
|
||||
It is strongly recommended to update off-heap memory of values directly only within a
|
||||
[context](#working-with-an-entry-within-a-context-section) and update or write lock held. Within a
|
||||
context, you are provided with an entry object of `MapEntry` type. To re-compute entry checksum
|
||||
manually, cast that object to `ChecksumEntry` type and call `.updateChecksum()` method on it:
|
||||
It is strongly recommended to update off-heap memory of values directly only within a context, and update or write lock held. Within a context, you are provided with an entry object of `MapEntry` type. To re-compute entry checksum manually, cast that object to `ChecksumEntry` type and call the `.updateChecksum()` method:
|
||||
|
||||
``` java
|
||||
try (ChronicleMap<Integer, LongValue> map = ChronicleMap
|
||||
|
||||
Reference in New Issue
Block a user