org/sonews/storage/AggregatedGroup.java
changeset 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/storage/AggregatedGroup.java	Wed Jul 22 14:04:05 2009 +0200
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.storage;
    1.23 +
    1.24 +import java.util.ArrayList;
    1.25 +import java.util.Collections;
    1.26 +import java.util.List;
    1.27 +import org.sonews.util.Pair;
    1.28 +
    1.29 +/**
    1.30 + * An aggregated group is a group consisting of several "real" group.
    1.31 + * @author Christian Lins
    1.32 + * @since sonews/1.0
    1.33 + */
    1.34 +class AggregatedGroup extends Channel
    1.35 +{
    1.36 +
    1.37 +  static class GroupElement
    1.38 +  {
    1.39 +    private Group group;
    1.40 +    private long  offsetStart, offsetEnd;
    1.41 +    
    1.42 +    public GroupElement(Group group, long offsetStart, long offsetEnd)
    1.43 +    {
    1.44 +      this.group       = group;
    1.45 +      this.offsetEnd   = offsetEnd;
    1.46 +      this.offsetStart = offsetStart;
    1.47 +    }
    1.48 +  }
    1.49 +
    1.50 +  public static List<Channel> getAll()
    1.51 +  {
    1.52 +    List<Channel> all = new ArrayList<Channel>();
    1.53 +    all.add(getByName("agg.test"));
    1.54 +    return all;
    1.55 +  }
    1.56 +
    1.57 +  public static AggregatedGroup getByName(String name)
    1.58 +  {
    1.59 +    if("agg.test".equals(name))
    1.60 +    {
    1.61 +      AggregatedGroup agroup = new AggregatedGroup(name);
    1.62 +      agroup.addGroup(Group.getByName("agg.test0"), 0, 1000);
    1.63 +      agroup.addGroup(Group.getByName("agg.test1"), 2000, 4000);
    1.64 +      return agroup;
    1.65 +    }
    1.66 +    else
    1.67 +      return null;
    1.68 +  }
    1.69 +
    1.70 +  private GroupElement[] groups = new GroupElement[2];
    1.71 +  private String         name;
    1.72 +
    1.73 +  public AggregatedGroup(String name)
    1.74 +  {
    1.75 +    this.name = name;
    1.76 +  }
    1.77 +
    1.78 +  private long aggIdxToIdx(long aggIdx)
    1.79 +    throws StorageBackendException
    1.80 +  {
    1.81 +    assert groups != null && groups.length == 2;
    1.82 +    assert groups[0] != null;
    1.83 +    assert groups[1] != null;
    1.84 +
    1.85 +    // Search in indices of group one
    1.86 +    List<Long> idxs0 = groups[0].group.getArticleNumbers();
    1.87 +    Collections.sort(idxs0);
    1.88 +    for(long idx : idxs0)
    1.89 +    {
    1.90 +      if(idx == aggIdx)
    1.91 +      {
    1.92 +        return idx;
    1.93 +      }
    1.94 +    }
    1.95 +
    1.96 +    // Given aggIdx must be an index of group two
    1.97 +    List<Long> idxs1 = groups[1].group.getArticleNumbers();
    1.98 +    return 0;
    1.99 +  }
   1.100 +  
   1.101 +  private long idxToAggIdx(long idx)
   1.102 +  {
   1.103 +    return 0;
   1.104 +  }
   1.105 +
   1.106 +  /**
   1.107 +   * Adds the given group to this aggregated set.
   1.108 +   * @param group
   1.109 +   * @param offsetStart Lower limit for the article ids range
   1.110 +   */
   1.111 +  public void addGroup(Group group, long offsetStart, long offsetEnd)
   1.112 +  {
   1.113 +    this.groups[groups[0] == null ? 0 : 1]
   1.114 +      = new GroupElement(group, offsetStart, offsetEnd);
   1.115 +  }
   1.116 +
   1.117 +  @Override
   1.118 +  public Article getArticle(long idx)
   1.119 +    throws StorageBackendException
   1.120 +  {
   1.121 +    Article article = null;
   1.122 +
   1.123 +    for(GroupElement groupEl : groups)
   1.124 +    {
   1.125 +      if(groupEl.offsetStart <= idx && groupEl.offsetEnd >= idx)
   1.126 +      {
   1.127 +        article = groupEl.group.getArticle(idx - groupEl.offsetStart);
   1.128 +        break;
   1.129 +      }
   1.130 +    }
   1.131 +
   1.132 +    return article;
   1.133 +  }
   1.134 +
   1.135 +  @Override
   1.136 +  public List<Pair<Long, ArticleHead>> getArticleHeads(
   1.137 +    final long first, final long last)
   1.138 +    throws StorageBackendException
   1.139 +  {
   1.140 +    List<Pair<Long, ArticleHead>> heads = new ArrayList<Pair<Long, ArticleHead>>();
   1.141 +    
   1.142 +    for(GroupElement groupEl : groups)
   1.143 +    {
   1.144 +      List<Pair<Long, ArticleHead>> partHeads = new ArrayList<Pair<Long, ArticleHead>>();
   1.145 +      if(groupEl.offsetStart <= first && groupEl.offsetEnd >= first)
   1.146 +      {
   1.147 +        long end = Math.min(groupEl.offsetEnd, last);
   1.148 +        partHeads = groupEl.group.getArticleHeads
   1.149 +          (first - groupEl.offsetStart, end - groupEl.offsetStart);
   1.150 +      }
   1.151 +      else if(groupEl.offsetStart <= last && groupEl.offsetEnd >= last)
   1.152 +      {
   1.153 +        long start = Math.max(groupEl.offsetStart, first);
   1.154 +        partHeads = groupEl.group.getArticleHeads
   1.155 +          (start - groupEl.offsetStart, last - groupEl.offsetStart);
   1.156 +      }
   1.157 +
   1.158 +      for(Pair<Long, ArticleHead> partHead : partHeads)
   1.159 +      {
   1.160 +        heads.add(new Pair<Long, ArticleHead>(
   1.161 +          partHead.getA() + groupEl.offsetStart, partHead.getB()));
   1.162 +      }
   1.163 +    }
   1.164 +
   1.165 +    return heads;
   1.166 +  }
   1.167 +
   1.168 +  @Override
   1.169 +  public List<Long> getArticleNumbers()
   1.170 +    throws StorageBackendException
   1.171 +  {
   1.172 +    List<Long> articleNumbers = new ArrayList<Long>();
   1.173 +    
   1.174 +    for(GroupElement groupEl : groups)
   1.175 +    {
   1.176 +      List<Long> partNums = groupEl.group.getArticleNumbers();
   1.177 +      for(Long partNum : partNums)
   1.178 +      {
   1.179 +        articleNumbers.add(partNum + groupEl.offsetStart);
   1.180 +      }
   1.181 +    }
   1.182 +
   1.183 +    return articleNumbers;
   1.184 +  }
   1.185 +
   1.186 +  @Override
   1.187 +  public long getIndexOf(Article art)
   1.188 +    throws StorageBackendException
   1.189 +  {
   1.190 +    for(GroupElement groupEl : groups)
   1.191 +    {
   1.192 +      long idx = groupEl.group.getIndexOf(art);
   1.193 +      if(idx > 0)
   1.194 +      {
   1.195 +        return idx;
   1.196 +      }
   1.197 +    }
   1.198 +    return -1;
   1.199 +  }
   1.200 +
   1.201 +  public long getInternalID()
   1.202 +  {
   1.203 +    return -1;
   1.204 +  }
   1.205 +
   1.206 +  @Override
   1.207 +  public String getName()
   1.208 +  {
   1.209 +    return this.name;
   1.210 +  }
   1.211 +
   1.212 +  @Override
   1.213 +  public long getFirstArticleNumber()
   1.214 +    throws StorageBackendException
   1.215 +  {
   1.216 +    long first = Long.MAX_VALUE;
   1.217 +
   1.218 +    for(GroupElement groupEl : groups)
   1.219 +    {
   1.220 +      first = Math.min(first, groupEl.group.getFirstArticleNumber() + groupEl.offsetStart);
   1.221 +    }
   1.222 +
   1.223 +    return first;
   1.224 +  }
   1.225 +
   1.226 +  @Override
   1.227 +  public long getLastArticleNumber()
   1.228 +    throws StorageBackendException
   1.229 +  {
   1.230 +    long last = 1;
   1.231 +
   1.232 +    for(GroupElement groupEl : groups)
   1.233 +    {
   1.234 +      last = Math.max(last, groupEl.group.getLastArticleNumber() + groupEl.offsetStart);
   1.235 +    }
   1.236 +
   1.237 +    return last + getPostingsCount(); // This is a hack
   1.238 +  }
   1.239 +  
   1.240 +  public long getPostingsCount()
   1.241 +    throws StorageBackendException
   1.242 +  {
   1.243 +    long postings = 0;
   1.244 +
   1.245 +    for(GroupElement groupEl : groups)
   1.246 +    {
   1.247 +      postings += groupEl.group.getPostingsCount();
   1.248 +    }
   1.249 +
   1.250 +    return postings;
   1.251 +  }
   1.252 +
   1.253 +  public boolean isDeleted()
   1.254 +  {
   1.255 +    return false;
   1.256 +  }
   1.257 +
   1.258 +  public boolean isWriteable()
   1.259 +  {
   1.260 +    return false;
   1.261 +  }
   1.262 +
   1.263 +}