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