@ -11,12 +11,12 @@
* 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 ( at your option ) any later version .
*
*
* This library 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 this program in the file lgpl21 . txt
* If not , see < http : //www.gnu.org/licenses/>.
@ -46,13 +46,13 @@ import net.yacy.cora.document.UTF8;
public class KeyList {
private static final Object _obj = new Object ( ) ;
private Map < String , Object > keys ;
private RandomAccessFile raf ;
public KeyList ( File file ) throws IOException {
private final Map < String , Object > keys ;
private final RandomAccessFile raf ;
public KeyList ( final File file ) throws IOException {
this . keys = new ConcurrentHashMap < String , Object > ( ) ;
if ( file . exists ( ) ) {
InputStream is = new FileInputStream ( file ) ;
if ( file . getName ( ) . endsWith ( ".gz" ) ) {
@ -66,34 +66,47 @@ public class KeyList {
l = l . trim ( ) . toLowerCase ( ) ;
this . keys . put ( l , _obj ) ;
}
} catch ( IOException e ) {
} catch ( final IOException e ) {
// finish
}
}
this . raf = new RandomAccessFile ( file , "rw" ) ;
}
public boolean contains ( String key ) {
return this . keys . containsKey ( key );
public boolean contains ( final String key ) {
return this . keys . containsKey ( key .trim ( ) . toLowerCase ( ) );
}
public void add ( String key ) throws IOException {
if ( keys. containsKey ( key ) ) return ;
public void add ( final String key ) throws IOException {
if ( this . keys. containsKey ( key ) ) return ;
synchronized ( this . raf ) {
if ( keys. containsKey ( key ) ) return ; // check again for those threads who come late (after another has written this)
if ( this . keys. containsKey ( key ) ) return ; // check again for those threads who come late (after another has written this)
this . keys . put ( key , _obj ) ;
this . raf . seek ( raf. length ( ) ) ;
this . raf . seek ( this . raf. length ( ) ) ;
this . raf . write ( UTF8 . getBytes ( key ) ) ;
this . raf . writeByte ( '\n' ) ;
}
}
public void close ( ) throws IOException {
synchronized ( this . raf ) {
raf. close ( ) ;
this . raf. close ( ) ;
}
}
public static void main ( final String [ ] args ) {
try {
final KeyList kl = new KeyList ( new File ( "/tmp/test" ) ) ;
kl . add ( "eins" ) ;
kl . add ( "zwei" ) ;
kl . add ( "drei" ) ;
System . out . println ( kl . contains ( "eins" ) ? "drin" : "nicht" ) ;
} catch ( final IOException e ) {
e . printStackTrace ( ) ;
}
}
}