# Class FileSnap

java.lang.Object
org.apache.zookeeper.server.persistence.FileSnap

All Implemented Interfaces:
  `SnapShot`

public class FileSnap extends Object implements SnapShot
This class implements the snapshot interface. it is responsible for storing, serializing and deserializing the right snapshot. and provides access to the snapshots.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`SNAP_MAGIC`
`static final String`
`SNAPSHOT_FILE_PREFIX`

## Constructor Summary

Constructors
Constructor
Description
`FileSnap(File snapDir)`

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
synchronized close just so that if serialize is in place the close operation will block and will wait till serialize is done and will set the close flag
`long`
`deserialize(DataTree dt, Map<Long,Integer> sessions)`
deserialize a data tree from the most recent snapshot
`static void`
`deserialize(DataTree dt, Map<Long,Integer> sessions, InputArchive ia)`
deserialize the datatree from an inputarchive
`File`
`findMostRecentSnapshot()`
find the most recent snapshot in the database.
`List<File>`
`findNRecentSnapshots(int n)`
find the last n snapshots.
`protected List<File>`
`findNValidSnapshots(int n)`
find the last (maybe) valid n snapshots.
`SnapshotInfo`
`getLastSnapshotInfo()`
get information of the last saved/restored snapshot
`void`
`serialize(DataTree dt, Map<Long,Integer> sessions, File snapShot, boolean fsync)`
serialize the datatree and session into the file snapshot
`protected void`
`serialize(DataTree dt, Map<Long,Integer> sessions, OutputArchive oa, FileHeader header)`
serialize the datatree and sessions

### Methods inherited from class Object

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

## Field Details

### SNAP_MAGIC

public static final int SNAP_MAGIC

### SNAPSHOT_FILE_PREFIX

public static final String SNAPSHOT_FILE_PREFIX

See Also:
  - Constant Field Values

## Constructor Details

### FileSnap

public FileSnap(@Nonnull File snapDir)

## Method Details

### getLastSnapshotInfo

public SnapshotInfo getLastSnapshotInfo()
get information of the last saved/restored snapshot

Specified by:
  `getLastSnapshotInfo` in interface `SnapShot`
Returns:
  info of last snapshot

### deserialize

public long deserialize(DataTree dt, Map<Long,Integer> sessions) throws IOException
deserialize a data tree from the most recent snapshot

Specified by:
  `deserialize` in interface `SnapShot`
Parameters:
  `dt` - the datatree to be deserialized into
  `sessions` - the sessions to be deserialized into
Returns:
  the zxid of the snapshot
Throws:
  `IOException`

### deserialize

public static void deserialize(DataTree dt, Map<Long,Integer> sessions, InputArchive ia) throws IOException
deserialize the datatree from an inputarchive

Parameters:
  `dt` - the datatree to be serialized into
  `sessions` - the sessions to be filled up
  `ia` - the input archive to restore from
Throws:
  `IOException`

### findMostRecentSnapshot

public File findMostRecentSnapshot()
find the most recent snapshot in the database.

Specified by:
  `findMostRecentSnapshot` in interface `SnapShot`
Returns:
  the file containing the most recent snapshot

### findNValidSnapshots

protected List<File> findNValidSnapshots(int n)
find the last (maybe) valid n snapshots. this does some minor checks on the validity of the snapshots. It just checks for / at the end of the snapshot. This does not mean that the snapshot is truly valid but is valid with a high probability. also, the most recent will be first on the list.

Parameters:
  `n` - the number of most recent snapshots
Returns:
  the last n snapshots (the number might be less than n in case enough snapshots are not available).

### findNRecentSnapshots

public List<File> findNRecentSnapshots(int n) throws IOException
find the last n snapshots. this does not have any checks if the snapshot might be valid or not

Parameters:
  `n` - the number of most recent snapshots
Returns:
  the last n snapshots
Throws:
  `IOException`

### serialize

protected void serialize(DataTree dt, Map<Long,Integer> sessions, OutputArchive oa, FileHeader header) throws IOException
serialize the datatree and sessions

Parameters:
  `dt` - the datatree to be serialized
  `sessions` - the sessions to be serialized
  `oa` - the output archive to serialize into
  `header` - the header of this snapshot
Throws:
  `IOException`

### serialize

public void serialize(DataTree dt, Map<Long,Integer> sessions, File snapShot, boolean fsync) throws IOException
serialize the datatree and session into the file snapshot

Specified by:
  `serialize` in interface `SnapShot`
Parameters:
  `dt` - the datatree to be serialized
  `sessions` - the sessions to be serialized
  `snapShot` - the file to store snapshot into
  `fsync` - sync the file immediately after write
Throws:
  `IOException`

### close

public void close() throws IOException
synchronized close just so that if serialize is in place the close operation will block and will wait till serialize is done and will set the close flag

Specified by:
  `close` in interface `SnapShot`
Throws:
  `IOException`