📄 villa.java
字号:
/************************************************************************************************* * Java API of Villa, the advanced API of QDBM * Copyright (C) 2000-2003 Mikio Hirabayashi * This file is part of QDBM, Quick Database Manager. * QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU * Lesser General Public License as published by the Free Software Foundation; either version * 2.1 of the License or any later version. QDBM 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 Lesser General Public License for more * details. * You should have received a copy of the GNU Lesser General Public License along with QDBM; if * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA. *************************************************************************************************/package qdbm;/** * The Java API of Villa, the advanced API of QDBM. * This class depends on the native library `jqdbm'. */public class Villa implements ADBM { //---------------------------------------------------------------- // error codes //---------------------------------------------------------------- /** error code: no error */ public static final int ENOERR = 0; /** error code: with fatal error */ public static final int EFATAL = 1; /** error code: invalid mode */ public static final int EMODE = 2; /** error code: broken database file */ public static final int EBROKEN = 3; /** error code: existing record */ public static final int EKEEP = 4; /** error code: no item found */ public static final int ENOITEM = 5; /** error code: memory allocation error */ public static final int EALLOC = 6; /** error code: memory mapping error */ public static final int EMAP = 7; /** error code: open error */ public static final int EOPEN = 8; /** error code: close error */ public static final int ECLOSE = 9; /** error code: trunc error */ public static final int ETRUNC = 10; /** error code: sync error */ public static final int ESYNC = 11; /** error code: stat error */ public static final int ESTAT = 12; /** error code: seek error */ public static final int ESEEK = 13; /** error code: read error */ public static final int EREAD = 14; /** error code: write error */ public static final int EWRITE = 15; /** error code: lock error */ public static final int ELOCK = 16; /** error code: unlink error */ public static final int EUNLINK = 17; /** error code: mkdir error */ public static final int EMKDIR = 18; /** error code: rmdir error */ public static final int ERMDIR = 19; /** error code: miscellaneous error */ public static final int EMISC = 20; //---------------------------------------------------------------- // open modes //---------------------------------------------------------------- /** open mode: open as a reader */ public static final int OREADER = 1 << 0; /** open mode: open as a writer */ public static final int OWRITER = 1 << 1; /** open mode: writer creating */ public static final int OCREAT = 1 << 2; /** open mode: writer truncating */ public static final int OTRUNC = 1 << 3; /** open mode: open without locking */ public static final int ONOLCK = 1 << 4; //---------------------------------------------------------------- // comparing modes //---------------------------------------------------------------- /** comparing mode: compare by lexical order */ public static final int CMPLEX = 0; /** comparing mode: compare as long integers */ public static final int CMPNUM = 1; /** comparing mode: compare as decimal strings */ public static final int CMPDEC = 2; /** comparing mode: compare as comparable objects */ public static final int CMPOBJ = 3; //---------------------------------------------------------------- // write modes //---------------------------------------------------------------- /** write mode: overwrite the existing value */ public static final int DOVER = 0; /** write mode: keep the existing value */ public static final int DKEEP = 1; /** write mode: concatenate values */ public static final int DDUP = 2; //---------------------------------------------------------------- // jump modes //---------------------------------------------------------------- /** jump mode: jump mode: step forward */ public static final int JFORWARD = 0; /** jump mode: jump mode: step backward */ public static final int JBACKWARD = 1; //---------------------------------------------------------------- // static initializer //---------------------------------------------------------------- static { try { System.loadLibrary("jqdbm"); } catch(UnsatisfiedLinkError e){ e.printStackTrace(); } vlinit(); } //---------------------------------------------------------------- // public static methods //---------------------------------------------------------------- /** * Get the version information. * @return the string of the version information. */ public static synchronized String version(){ return vlversion(); } /** * Get an error message. * @param ecode an error code. * @return the message string of the error code. */ public static synchronized String errmsg(int ecode){ return vlerrmsg(ecode); } /** * Remove a database file. * @param name the name of a database file. * @throws VillaException if an error occures. */ public static void remove(String name) throws VillaException { synchronized(ADBM.class){ if(vlremove(name) == 0) throw new VillaException(vlecode()); } } //---------------------------------------------------------------- // instance fields //---------------------------------------------------------------- /** Index of the native table for database handles. */ private int index; /** Whether the handle has the transaction or not. */ private boolean tran; /** Monitor for mutual exclusion control. */ private Object tranmonitor; //---------------------------------------------------------------- // public or protected methods //---------------------------------------------------------------- /** * Get the database handle. * @param name the name of a database file. * @param omode the connection mode: `Villa.OWRITER' as a writer, `Villa.OREADER' as * a reader. If the mode is `Villa.OWRITER', the following may be added by bitwise or: * `Villa.OCREAT', which means it creates a new database if not exist, `Villa.OTRUNC', * which means it creates a new database regardless if one exists. Both of `Villa.OREADER' * and `Villa.OWRITER' can be added to by bitwise or: `Villa.ONOLCK', which means it opens a * database file without file locking. * @param cmode the comparing function: `Villa.CMPLEX' comparing keys in lexical order, * `Villa.CMPNUM' comparing keys as numbers of big endian, `Villa.CMPDEC' comparing keys as * decimal strings, `Villa.CMPOBJ' comparing keys as serialized objects implementing * `java.util.Comparable'. The comparing function should be kept same in the life of a * database. * @note While connecting as a writer, an exclusive lock is invoked to the database file. * While connecting as a reader, a shared lock is invoked to the database file. The thread * blocks until the lock is achieved. If `Villa.ONOLCK' is used, the application is * responsible for exclusion control. */ public Villa(String name, int omode, int cmode) throws VillaException { synchronized(ADBM.class){ if((index = vlopen(name, omode, cmode)) == -1) throw new VillaException(vlecode()); } tran = false; tranmonitor = new Object(); } /** * Get the database handle as a reader. * The same as `Villa(name, Villa.OREADER, Villa.CMPLEX)'. * @see #Villa(java.lang.String, int, int) */ public Villa(String name) throws VillaException { this(name, OREADER, CMPLEX); } /** * Release the resources. * @note If the database handle is not closed yet, it is closed. Every database should be * closed explicitly. Do not cast the duty on the gerbage collection. */ protected void finalize() throws Throwable { try { if(index < 0) return; synchronized(ADBM.class){ vlclose(index); index = -1; } } finally { super.finalize(); } } /** * Close the database handle. * @throws VillaException if an error occures. * @note Updating a database is assured to be written when the handle is closed. If a * writer opens a database but does not close it appropriately, the database will be broken. * If the transaction is activated and not committed, it is aborted. */ public void close() throws VillaException { if(index < 0) throw new VillaException(); synchronized(ADBM.class){ int rv = vlclose(index); index = -1; if(rv == 0) throw new VillaException(vlecode()); } } /** * Store a record. * @param key a byte array of a key. * @param val a byte array of a value. * @param dmode behavior when the key overlaps, by the following values: `Villa.DOVER', * which means the specified value overwrites the existing one, `Villa.DKEEP', which means the * existing value is kept, `Villa.DDUP', which means duplication of keys is allowed. * @throws VillaException if an error occures or replace is cancelled. * @note A duplicated record is stored at the tail of the records of the same key. The cursor * becomes unavailable due to updating database. */ public void put(byte[] key, byte[] val, int dmode) throws VillaException { if(index < 0) throw new VillaException(); synchronized(ADBM.class){ if(vlput(index, key, key.length, val, val.length, dmode) == 0) throw new VillaException(vlecode()); } } /** * Store a record with overwrite. * The same as `put(key, val, Villa.DOVER)'. * @see #put(byte[], byte[], int) */ public void put(byte[] key, byte[] val) throws VillaException { put(key, val, DOVER); } /** * Store a record composed of serializable objects. * The same as `put(qdbm.Util.serialize(key), qdbm.Util.serialize(val), dmode)'. * @see #put(byte[], byte[], int) * @note If serialization is failed, an instance of `VillaException' is thrown. */ public void putobj(Object key, Object val, int dmode) throws VillaException { byte[] kbuf = Util.serialize(key); byte[] vbuf = Util.serialize(val); if(kbuf == null || vbuf == null) throw new VillaException(); put(kbuf, vbuf, dmode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -