org.apache.ignite.cache.eviction.sorted

## Class SortedEvictionPolicy<K,V>

- java.lang.Object
- - org.apache.ignite.cache.eviction.AbstractEvictionPolicy<K,V>
  - - org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicy<K,V>

All Implemented Interfaces:
  Externalizable, Serializable, EvictionPolicy<K,V>, IgniteMBeanAware

    public class SortedEvictionPolicy<K,V>
    extends AbstractEvictionPolicy<K,V>
    implements IgniteMBeanAware

Cache eviction policy which will select the minimum cache entry for eviction.

The eviction starts in the following cases:

- The cache size becomes `batchSize` elements greater than the maximum size.
- The size of cache entries in bytes becomes greater than the maximum memory size. The size of cache entry calculates as sum of key size and value size.

Note:Batch eviction is enabled only if maximum memory limit isn't set (`maxMemSize == 0`). `batchSize` elements will be evicted in this case. The default `batchSize` value is `1`.

Entries comparison based on `Comparator` instance if provided. Default `Comparator` behaviour is use cache entries keys for comparison that imposes a requirement for keys to implement `Comparable` interface.

User defined comparator should implement `Serializable` interface.

See Also:
  Serialized Form

### Field Summary

### Fields inherited from class org.apache.ignite.cache.eviction.AbstractEvictionPolicy

`memSize`

### Constructor Summary

Constructors
Constructor and Description
`SortedEvictionPolicy()` Constructs sorted eviction policy with all defaults.
`SortedEvictionPolicy(Comparator<EvictableEntry<K,V>> comp)` Constructs sorted eviction policy with given maximum size and given entry comparator.
`SortedEvictionPolicy(int max)` Constructs sorted eviction policy with maximum size.
`SortedEvictionPolicy(int max, Comparator<EvictableEntry<K,V>> comp)` Constructs sorted eviction policy with given maximum size and given entry comparator.
`SortedEvictionPolicy(int max, int batchSize, Comparator<EvictableEntry<K,V>> comp)` Constructs sorted eviction policy with given maximum size, eviction batch size and entries comparator.

### Method Summary

All Methods Instance Methods Concrete Methods
`int` `getCurrentSize()`
`Object` `getMBean()`
`Collection<EvictableEntry<K,V>>` `queue()` Gets read-only view of backed queue in proper order.
`void` `readExternal(ObjectInput in)`
`protected boolean` `removeMeta(Object meta)` Removes holder from backed queue and marks holder as removed.
`SortedEvictionPolicy<K,V>` `setBatchSize(int batchSize)` Sets batch size.
`SortedEvictionPolicy<K,V>` `setMaxMemorySize(long maxMemSize)` Sets maximum allowed cache size in bytes.
`SortedEvictionPolicy<K,V>` `setMaxSize(int max)` Sets maximum allowed size of cache before entry will start getting evicted.
`protected int` `shrink0()` Tries to remove one item from queue.
`protected boolean` `touch(EvictableEntry<K,V> entry)`
`void` `writeExternal(ObjectOutput out)`

### Methods inherited from class org.apache.ignite.cache.eviction.AbstractEvictionPolicy

`getBatchSize, getCurrentMemorySize, getMaxMemorySize, getMaxSize, onEntryAccessed, shrink`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### SortedEvictionPolicy

    public SortedEvictionPolicy()

Constructs sorted eviction policy with all defaults.

#### SortedEvictionPolicy

    public SortedEvictionPolicy(int max)

Constructs sorted eviction policy with maximum size.

Parameters:
  `max` - Maximum allowed size of cache before entry will start getting evicted.

#### SortedEvictionPolicy

    public SortedEvictionPolicy(int max,
                                @Nullable
                                Comparator<EvictableEntry<K,V>> comp)

Constructs sorted eviction policy with given maximum size and given entry comparator.

Parameters:
  `max` - Maximum allowed size of cache before entry will start getting evicted.
  `comp` - Entries comparator.

#### SortedEvictionPolicy

    public SortedEvictionPolicy(int max,
                                int batchSize,
                                @Nullable
                                Comparator<EvictableEntry<K,V>> comp)

Constructs sorted eviction policy with given maximum size, eviction batch size and entries comparator.

Parameters:
  `max` - Maximum allowed size of cache before entry will start getting evicted.
  `batchSize` - Batch size.
  `comp` - Entries comparator.

#### SortedEvictionPolicy

    public SortedEvictionPolicy(@Nullable
                                Comparator<EvictableEntry<K,V>> comp)

Constructs sorted eviction policy with given maximum size and given entry comparator.

Parameters:
  `comp` - Entries comparator.

### Method Detail

#### setMaxMemorySize

    public SortedEvictionPolicy<K,V> setMaxMemorySize(long maxMemSize)

Sets maximum allowed cache size in bytes.

Overrides:
  `setMaxMemorySize` in class `AbstractEvictionPolicy<K,V>`
Returns:
  `this` for chaining.

#### setMaxSize

    public SortedEvictionPolicy<K,V> setMaxSize(int max)

Sets maximum allowed size of cache before entry will start getting evicted.

Overrides:
  `setMaxSize` in class `AbstractEvictionPolicy<K,V>`
Parameters:
  `max` - Maximum allowed size of cache before entry will start getting evicted.
Returns:
  `this` for chaining.

#### setBatchSize

    public SortedEvictionPolicy<K,V> setBatchSize(int batchSize)

Sets batch size.

Overrides:
  `setBatchSize` in class `AbstractEvictionPolicy<K,V>`
Parameters:
  `batchSize` - Batch size.
Returns:
  `this` for chaining.

#### queue

    public Collection<EvictableEntry<K,V>> queue()

Gets read-only view of backed queue in proper order.

Returns:
  Read-only view of backed queue.

#### touch

    protected boolean touch(EvictableEntry<K,V> entry)

Specified by:
  `touch` in class `AbstractEvictionPolicy<K,V>`
Parameters:
  `entry` - Entry to touch.
Returns:
  `True` if backed queue has been changed by this call.

#### getCurrentSize

    public int getCurrentSize()

Specified by:
  `getCurrentSize` in class `AbstractEvictionPolicy<K,V>`
Returns:
  Size of the container with trackable entries.

#### shrink0

    protected int shrink0()

Tries to remove one item from queue.

Specified by:
  `shrink0` in class `AbstractEvictionPolicy<K,V>`
Returns:
  number of bytes that was free. `-1` if queue is empty.

#### getMBean

    public Object getMBean()

Specified by:
  `getMBean` in interface `IgniteMBeanAware`
Returns:
  MBean for this object.

#### writeExternal

    public void writeExternal(ObjectOutput out)
                       throws IOException

Specified by:
  `writeExternal` in interface `Externalizable`
Overrides:
  `writeExternal` in class `AbstractEvictionPolicy<K,V>`
Throws:
  `IOException`

#### readExternal

    public void readExternal(ObjectInput in)
                      throws IOException,
                             ClassNotFoundException

Specified by:
  `readExternal` in interface `Externalizable`
Overrides:
  `readExternal` in class `AbstractEvictionPolicy<K,V>`
Throws:
  `IOException`
  `ClassNotFoundException`

#### removeMeta

    protected boolean removeMeta(Object meta)

Removes holder from backed queue and marks holder as removed.

Specified by:
  `removeMeta` in class `AbstractEvictionPolicy<K,V>`
Parameters:
  `meta` - Holder.
Returns:
  `True` if meta was successfully removed from the container.

2018 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.7.5
Release Date | : June 4 2019