1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2001,2008 Oracle.  All rights reserved.
5 *
6 * $Id: CacheFile.java,v 12.7 2008/01/17 05:04:53 mjc Exp $
7 */
8
9package com.sleepycat.db;
10
11import com.sleepycat.db.internal.DbConstants;
12import com.sleepycat.db.internal.DbMpoolFile;
13
14/**
15This class allows applications to modify settings for
16a {@link com.sleepycat.db.Database Database} using the {@link com.sleepycat.db.Database#getCacheFile Database.getCacheFile}.
17*/
18public class CacheFile {
19    private DbMpoolFile mpf;
20
21    /* package */
22    CacheFile(final DbMpoolFile mpf) {
23        this.mpf = mpf;
24    }
25
26    /**
27Return the cache priority for pages from the specified file.
28<p>
29This method may be called at any time during the life of the application.
30<p>
31@return
32The cache priority for pages from the specified file.
33<p>
34<p>
35@throws DatabaseException if a failure occurs.
36    */
37    public CacheFilePriority getPriority()
38        throws DatabaseException {
39
40        return CacheFilePriority.fromFlag(mpf.get_priority());
41    }
42
43    /**
44Set the
45cache priority for pages from the specified file.
46<p>The priority of a page biases the replacement algorithm to be more
47    or less likely to discard a page when space is needed in the buffer
48    pool.  The bias is temporary, and pages will eventually be discarded
49    if they are not referenced again.  Setting the priority is only
50    advisory, and does not guarantee pages will be treated in a specific
51    way.
52<p>
53This method may be called at any time during the life of the application.
54<p>
55@param priority
56The cache priority for pages from the specified file.
57<p>
58<p>
59@throws DatabaseException if a failure occurs.
60    */
61    public void setPriority(final CacheFilePriority priority)
62        throws DatabaseException {
63
64        mpf.set_priority(priority.getFlag());
65    }
66
67    /**
68Return the maximum size for the file backing the database, or 0 if
69    no maximum file size has been configured.
70<p>
71This method may be called at any time during the life of the application.
72<p>
73@return
74The maximum size for the file backing the database, or 0 if
75    no maximum file size has been configured.
76<p>
77<p>
78@throws DatabaseException if a failure occurs.
79    */
80    public long getMaximumSize()
81        throws DatabaseException {
82
83        return mpf.get_maxsize();
84    }
85
86    /**
87Set the
88maximum size for the file backing the database.
89<p>Attempts to allocate new pages in the file after the limit has been
90    reached will fail.
91<p>
92This method may be called at any time during the life of the application.
93<p>
94@param bytes
95The maximum size for the file backing the database.
96<p>
97<p>
98@throws DatabaseException if a failure occurs.
99    */
100    public void setMaximumSize(final long bytes)
101        throws DatabaseException {
102
103        mpf.set_maxsize(bytes);
104    }
105
106    /**
107Return true if the opening of backing temporary files for in-memory
108    databases has been disallowed.
109<p>
110This method may be called at any time during the life of the application.
111<p>
112@return
113True if the opening of backing temporary files for in-memory
114    databases has been disallowed.
115<p>
116<p>
117@throws DatabaseException if a failure occurs.
118    */
119    public boolean getNoFile()
120        throws DatabaseException {
121
122        return (mpf.get_flags() & DbConstants.DB_MPOOL_NOFILE) != 0;
123    }
124
125    /**
126Disallow opening backing temporary files for in-memory
127    databases, even if they expand to fill the entire cache.
128<p>Attempts to create new file pages after the cache has been filled
129    will fail.
130<p>
131This method may be called at any time during the life of the application.
132<p>
133@param onoff
134If true,
135disallow opening backing temporary files for in-memory
136    databases, even if they expand to fill the entire cache.
137<p>
138<p>
139@throws DatabaseException if a failure occurs.
140    */
141    public void setNoFile(final boolean onoff)
142        throws DatabaseException {
143
144        mpf.set_flags(DbConstants.DB_MPOOL_NOFILE, onoff);
145    }
146
147    /**
148Return true if the file will be removed when the last reference to it is
149    closed.
150<p>
151This method may be called at any time during the life of the application.
152<p>
153@return
154True if the file will be removed when the last reference to it is
155    closed.
156<p>
157<p>
158@throws DatabaseException if a failure occurs.
159    */
160    public boolean getUnlink()
161        throws DatabaseException {
162
163        return (mpf.get_flags() & DbConstants.DB_MPOOL_UNLINK) != 0;
164    }
165
166    /**
167Remove the file when the last reference to it is closed.
168<p>
169This method may be called at any time during the life of the application.
170<p>
171@param onoff
172If true,
173remove the file when the last reference to it is closed.
174<p>
175<p>
176@throws DatabaseException if a failure occurs.
177    */
178    public void setUnlink(boolean onoff)
179        throws DatabaseException {
180
181        mpf.set_flags(DbConstants.DB_MPOOL_UNLINK, onoff);
182    }
183}
184