-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PageCache.java
111 lines (102 loc) · 5.28 KB
/
PageCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io.pagecache;
import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
/**
* A page caching mechanism that allows caching multiple files and accessing their data
* in pages via a re-usable cursor.
* <p>
* This interface does not specify the cache eviction and allocation behavior, it may be
* backed by implementations that map entire files into RAM, or implementations with smart
* eviction strategies, trying to keep "hot" pages in RAM.
*/
public interface PageCache extends AutoCloseable
{
/**
* Ask for a handle to a paged file, backed by this page cache.
* <p>
* Note that this currently asks for the pageSize to use, which is an artifact or records being
* of varying size in the stores. This should be consolidated to use a standard page size for the
* whole cache, with records aligning on those page boundaries.
*
* @param file The file to map.
* @param pageSize The file page size to use for this mapping. If the file is already mapped with a different page
* size, an exception will be thrown.
* @param openOptions The set of open options to use for mapping this file.
* The {@link StandardOpenOption#READ} and {@link StandardOpenOption#WRITE} options always implicitly specified.
* The {@link StandardOpenOption#CREATE} open option will create the given file if it does not already exist, and
* the {@link StandardOpenOption#TRUNCATE_EXISTING} will truncate any existing file <em>iff</em> it has not already
* been mapped.
* The {@link StandardOpenOption#DELETE_ON_CLOSE} will cause the file to be deleted after the last unmapping.
* All other options are either silently ignored, or will cause an exception to be thrown.
* @throws java.nio.file.NoSuchFileException if the given file does not exist, and the
* {@link StandardOpenOption#CREATE} option was not specified.
* @throws IOException if the file could otherwise not be mapped. Causes include the file being locked.
*/
PagedFile map( File file, int pageSize, OpenOption... openOptions ) throws IOException;
/**
* Ask for an already mapped paged file, backed by this page cache.
* <p>
* If mapping exist, the returned {@link Optional} will report {@link Optional#isPresent()} true and
* {@link Optional#get()} will return the same {@link PagedFile} instance that was initially returned my
* {@link #map(File, int, OpenOption...)}.
* If no mapping exist for this file, then returned {@link Optional} will report {@link Optional#isPresent()}
* false.
* <p>
* NOTE! User is responsible for closing the returned paged file.
*
* @param file The file to try to get the mapped paged file for.
* @return {@link Optional} containing the {@link PagedFile} mapped by this {@link PageCache} for given file, or an
* empty {@link Optional} if no mapping exist.
* @throws IOException if page cache has been closed or page eviction problems occur.
*/
Optional<PagedFile> getExistingMapping( File file ) throws IOException;
/** Flush all dirty pages */
void flushAndForce() throws IOException;
/**
* Flush all dirty pages, but limit the rate of IO as advised by the given IOPSLimiter.
* @param limiter The {@link IOLimiter} that determines if pauses or sleeps should be injected into the flushing
* process to keep the IO rate down.
*/
void flushAndForce( IOLimiter limiter ) throws IOException;
/** Flush all dirty pages and close the page cache. */
void close() throws IOException;
/** The size in bytes of the pages managed by this cache. */
int pageSize();
/** The max number of cached pages. */
int maxCachedPages();
/**
* Move the file from the given source path, to the given target path.
*
* Both files have to be unmapped when performing the move, otherwise an exception will be thrown.
*
* @param sourceFile The name of the file to move.
* @param targetFile The new name of the file after the move.
* @param copyOptions Options to modify the behaviour of the move in possibly platform specific ways. In particular,
* {@link java.nio.file.StandardCopyOption#REPLACE_EXISTING} may be used to overwrite any existing file at the
* target path name, instead of throwing an exception.
*/
void moveFile( File sourceFile, File targetFile, CopyOption... copyOptions ) throws IOException;
}