* Naming services, addressbook, susidns:

- Fix search capability
    - Fix result count and view within results
    - Fix published address book
    - Fix ngettext
    - Cache size
    - Fix 0-9 filter
    - Addressbook updates via API, except for published
This commit is contained in:
zzz
2011-03-15 21:52:48 +00:00
parent 8b737b4adb
commit 12c5b9c21c
7 changed files with 338 additions and 76 deletions

View File

@ -476,13 +476,17 @@ public class BlockfileNamingService extends DummyNamingService {
* from that list (default "hosts.txt", NOT all lists)
* Key "skip": skip that many entries
* Key "limit": max number to return
* Key "search": return only those matching substring
* Key "startsWith": return only those starting with
* ("[0-9]" allowed)
* Key "beginWith": start here in the iteration
* Don't use both
* Don't use both startsWith and beginWith.
* Search, startsWith, and beginWith values must be lower case.
*/
@Override
public Map<String, Destination> getEntries(Properties options) {
String listname = FALLBACK_LIST;
String search = null;
String startsWith = null;
String beginWith = null;
int limit = Integer.MAX_VALUE;
@ -491,10 +495,15 @@ public class BlockfileNamingService extends DummyNamingService {
String ln = options.getProperty("list");
if (ln != null)
listname = ln;
search = options.getProperty("search");
startsWith = options.getProperty("startsWith");
beginWith = options.getProperty("beginWith");
if (beginWith == null)
beginWith = startsWith;
if (beginWith == null && startsWith != null) {
if (startsWith.equals("[0-9]"))
beginWith = "0";
else
beginWith = startsWith;
}
String lim = options.getProperty("limit");
try {
limit = Integer.parseInt(lim);
@ -505,7 +514,9 @@ public class BlockfileNamingService extends DummyNamingService {
} catch (NumberFormatException nfe) {}
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Searching " + listname + " beginning with " + beginWith + " starting with " + startsWith + " limit=" + limit + " skip=" + skip);
_log.debug("Searching " + listname + " beginning with " + beginWith +
" starting with " + startsWith + " search string " + search +
" limit=" + limit + " skip=" + skip);
synchronized(_bf) {
try {
SkipList sl = _bf.getIndex(listname, _stringSerializer, _destSerializer);
@ -523,12 +534,21 @@ public class BlockfileNamingService extends DummyNamingService {
for (int i = 0; i < skip && iter.hasNext(); i++) {
iter.next();
}
for (int i = 0; i < limit && iter.hasNext(); i++) {
String key = (String) iter.nextKey();
if (startsWith != null && !key.startsWith(startsWith))
break;
DestEntry de = (DestEntry) iter.next();
rv.put(key, de.dest);
for (int i = 0; i < limit && iter.hasNext(); ) {
String key = (String) iter.nextKey();
if (startsWith != null) {
if (startsWith.equals("[0-9]")) {
if (key.charAt(0) > '9')
break;
} else if (!key.startsWith(startsWith)) {
break;
}
}
DestEntry de = (DestEntry) iter.next();
if (search != null && key.indexOf(search) < 0)
continue;
rv.put(key, de.dest);
i++;
}
return rv;
} catch (IOException ioe) {

View File

@ -53,6 +53,10 @@ public class SingleFileNamingService extends NamingService {
private final static Log _log = new Log(SingleFileNamingService.class);
private final File _file;
private final ReentrantReadWriteLock _fileLock;
/** cached number of entries */
private int _size;
/** last write time */
private long _lastWrite;
public SingleFileNamingService(I2PAppContext context, String filename) {
super(context);
@ -292,14 +296,18 @@ public class SingleFileNamingService extends NamingService {
/**
* @param options As follows:
* Key "search": return only those matching substring
* Key "startsWith": return only those starting with
* ("[0-9]" allowed)
*/
@Override
public Map<String, Destination> getEntries(Properties options) {
if (!_file.exists())
return Collections.EMPTY_MAP;
String startsWith = "";
String searchOpt = null;
String startsWith = null;
if (options != null) {
searchOpt = options.getProperty("search");
startsWith = options.getProperty("startsWith");
}
BufferedReader in = null;
@ -307,11 +315,19 @@ public class SingleFileNamingService extends NamingService {
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16*1024);
String line = null;
String search = startsWith + '=';
String search = startsWith == null ? null : startsWith + '=';
Map<String, Destination> rv = new HashMap();
while ( (line = in.readLine()) != null) {
if ((!startsWith.equals("")) && !line.startsWith(search))
if (line.length() <= 0)
continue;
if (search != null) {
if (startsWith.equals("[0-9]")) {
if (line.charAt(0) < '0' || line.charAt(0) > '9')
continue;
} else if (!line.startsWith(search)) {
continue;
}
}
if (line.startsWith("#"))
continue;
if (line.indexOf('#') > 0) // trim off any end of line comment
@ -320,12 +336,18 @@ public class SingleFileNamingService extends NamingService {
if (split <= 0)
continue;
String key = line.substring(split);
if (searchOpt != null && key.indexOf(searchOpt) < 0)
continue;
String b64 = line.substring(split+1); //.trim() ??????????????
try {
Destination dest = new Destination(b64);
rv.put(key, dest);
} catch (DataFormatException dfe) {}
}
if (searchOpt == null && startsWith == null) {
_lastWrite = _file.lastModified();
_size = rv.size();
}
return rv;
} catch (IOException ioe) {
_log.error("getEntries error", ioe);
@ -346,14 +368,18 @@ public class SingleFileNamingService extends NamingService {
BufferedReader in = null;
getReadLock();
try {
if (_file.lastModified() <= _lastWrite)
return _size;
in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16*1024);
String line = null;
int rv = 0;
while ( (line = in.readLine()) != null) {
if (line.startsWith("#"))
if (line.startsWith("#") || line.length() <= 0)
continue;
rv++;
}
_lastWrite = _file.lastModified();
_size = rv;
return rv;
} catch (IOException ioe) {
_log.error("size() error", ioe);