001 /*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2013
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 *
010 * All Rights Reserved
011 *
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.
014 *
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 *
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023 * GNU Lesser Public License for more details.
024 *
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program. If not, see http://www.gnu.org/licenses.
027 */
028 package edu.wisc.ssec.mcidasv.util.trie;
029
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 import edu.wisc.ssec.mcidasv.servermanager.AddeEntry;
034 import edu.wisc.ssec.mcidasv.servermanager.EntryStore;
035 import edu.wisc.ssec.mcidasv.util.trie.PatriciaTrie.KeyAnalyzer;
036
037
038 public class AddeEntryKeyAnalyzer implements KeyAnalyzer<AddeEntry> {
039
040 final static Logger logger = LoggerFactory.getLogger(EntryStore.class);
041
042 // AddeEntrys are organized like so:
043 // server->dataset->type->name
044 // rets->rets3->IMAGE->
045 @Override public int bitIndex(AddeEntry key, int keyStart, int keyLength, AddeEntry found, int foundStart, int foundLength) {
046
047 if (found == null) {
048 logger.debug("bitIndex: key={}; null found: ret={}", key.asStringId(), keyStart);
049 return keyStart;
050 }
051
052 boolean allNull = true;
053 logger.debug("bitIndex: key={} keyStart={} keyLen={} found={} foundStart={} foundLen={}", new Object[] { key.asStringId(),keyStart,keyLength,found.asStringId(),foundStart,foundLength});
054 int length = Math.max(keyLength, foundLength);
055 for (int i = 0; i < length; i++) {
056 String fromKey = valAtIdx(i, key);
057 String fromFound = valAtIdx(i, found);
058 if (!fromKey.equals(fromFound)) {
059 logger.debug(" diff: idx={} key={} found={}", new Object[] { i, key.asStringId(), found.asStringId()});
060 return i;
061 }
062 }
063 logger.debug(" equals!");
064 return KeyAnalyzer.EQUAL_BIT_KEY;
065 }
066
067 //
068 @Override public int bitsPerElement() {
069 return 1;
070 }
071
072 @Override public boolean isBitSet(AddeEntry key, int keyLength, int bitIndex) {
073 return true;
074 }
075
076 @Override public boolean isPrefix(AddeEntry prefix, int offset, int length, AddeEntry key) {
077 logger.debug("isPrefix: prefix={} offset={} len={} key={}", new Object[] { prefix.asStringId(), offset, length, key.asStringId() });
078 for (int i = offset; i < length; i++) {
079 String fromKey = valAtIdx(i, key);
080 String fromPrefix = valAtIdx(i, prefix);
081 if (!fromKey.equals(fromPrefix))
082 return false;
083 }
084 return true;
085 }
086
087 @Override public int length(AddeEntry key) {
088 return 4;
089 }
090
091 @Override public int compare(AddeEntry o1, AddeEntry o2) {
092 return o1.asStringId().compareTo(o2.asStringId());
093 }
094
095 private static String valAtIdx(final int idx, final AddeEntry e) {
096 switch (idx) {
097 case 0: return e.getAddress();
098 case 1: return e.getGroup();
099 case 2: return e.getEntryType().name();
100 case 3: return e.getName();
101 default: throw new AssertionError();
102 }
103 }
104
105 }