Coverage Report - com.thindeck.dynamo.DyDecks
 
Classes in this File Line Coverage Branch Coverage Complexity
DyDecks
0%
0/25
0%
0/28
2.833
DyDecks$1
0%
0/4
N/A
2.833
 
 1  0
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the thindeck.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.thindeck.dynamo;
 31  
 
 32  
 import com.amazonaws.services.dynamodbv2.model.Select;
 33  
 import com.google.common.base.Function;
 34  
 import com.google.common.collect.Iterables;
 35  
 import com.jcabi.aspects.Immutable;
 36  
 import com.jcabi.dynamo.Attributes;
 37  
 import com.jcabi.dynamo.Item;
 38  
 import com.jcabi.dynamo.QueryValve;
 39  
 import com.jcabi.dynamo.Region;
 40  
 import com.thindeck.api.Deck;
 41  
 import com.thindeck.api.Decks;
 42  
 import java.io.IOException;
 43  
 import java.util.Iterator;
 44  
 import lombok.EqualsAndHashCode;
 45  
 import lombok.ToString;
 46  
 
 47  
 /**
 48  
  * Dynamo implementation of {@link com.thindeck.api.Decks}.
 49  
  *
 50  
  * @author Krzysztof Krason (Krzysztof.Krason@gmail.com)
 51  
  * @author Yegor Bugayenko (yegor@teamed.io)
 52  
  * @version $Id$
 53  
  */
 54  0
 @ToString
 55  
 @Immutable
 56  0
 @EqualsAndHashCode(of = { "region", "user" })
 57  
 final class DyDecks implements Decks {
 58  
 
 59  
     /**
 60  
      * Region.
 61  
      */
 62  
     private final transient Region region;
 63  
 
 64  
     /**
 65  
      * Name of the owner.
 66  
      */
 67  
     private final transient String user;
 68  
 
 69  
     /**
 70  
      * Ctor.
 71  
      * @param reg Region
 72  
      * @param usr URN
 73  
      */
 74  0
     DyDecks(final Region reg, final String usr) {
 75  0
         this.region = reg;
 76  0
         this.user = usr;
 77  0
     }
 78  
 
 79  
     @Override
 80  
     public Deck get(final String name) throws IOException {
 81  0
         final Iterator<Item> items = this.region
 82  
             .table(DyDeck.TBL)
 83  
             .frame()
 84  
             .through(new QueryValve().withLimit(1))
 85  
             .where(DyDeck.HASH, this.user)
 86  
             .where(DyDeck.RANGE, name)
 87  
             .iterator();
 88  0
         if (!items.hasNext()) {
 89  0
             throw new IOException(
 90  
                 String.format("deck '%s' not found", name)
 91  
             );
 92  
         }
 93  0
         return new DyDeck(
 94  
             this.region, this.user, name
 95  
         );
 96  
     }
 97  
 
 98  
     @Override
 99  
     public void add(final String name) throws IOException {
 100  0
         if (!name.matches("[a-z]{3,12}")) {
 101  0
             throw new IllegalStateException(
 102  
                 String.format(
 103  
                     "invalid deck name '%s', must be 3-12 English letters",
 104  
                     name
 105  
                 )
 106  
             );
 107  
         }
 108  0
         final Iterator<Item> items = this.region.table(DyDeck.TBL)
 109  
             .frame()
 110  
             .through(new QueryValve().withLimit(1))
 111  
             .where(DyDeck.HASH, this.user)
 112  
             .where(DyDeck.RANGE, name)
 113  
             .iterator();
 114  0
         if (items.hasNext()) {
 115  0
             throw new IllegalStateException(
 116  
                 String.format(
 117  
                     "deck '%s' already exists, can't add it again",
 118  
                     name
 119  
                 )
 120  
             );
 121  
         }
 122  0
         this.region.table(DyDeck.TBL).put(
 123  
             new Attributes()
 124  
                 .with(DyDeck.HASH, this.user)
 125  
                 .with(DyDeck.RANGE, name)
 126  
                 .with(DyDeck.ATTR_UPDATED, System.currentTimeMillis())
 127  
                 .with(
 128  
                     DyDeck.ATTR_MEMO,
 129  
                     String.format("<deck name='%s/%s'/>", this.user, name)
 130  
                 )
 131  
         );
 132  0
     }
 133  
 
 134  
     @Override
 135  
     public void delete(final String name) {
 136  0
         final Iterator<Item> items = this.region.table(DyDeck.TBL)
 137  
             .frame()
 138  
             .through(new QueryValve().withLimit(1))
 139  
             .where(DyDeck.HASH, this.user)
 140  
             .where(DyDeck.RANGE, name)
 141  
             .iterator();
 142  0
         if (!items.hasNext()) {
 143  0
             throw new IllegalStateException(
 144  
                 String.format(
 145  
                     "deck '%s' not found, can't delete",
 146  
                     name
 147  
                 )
 148  
             );
 149  
         }
 150  0
         items.next();
 151  0
         items.remove();
 152  0
     }
 153  
 
 154  
     @Override
 155  
     public Iterable<Deck> iterate() {
 156  0
         return Iterables.transform(
 157  
             this.region.table(DyDeck.TBL)
 158  
                 .frame()
 159  
                 .through(new QueryValve().withSelect(Select.ALL_ATTRIBUTES))
 160  
                 .where(DyDeck.HASH, this.user),
 161  0
             new Function<Item, Deck>() {
 162  
                 @Override
 163  
                 public Deck apply(final Item input) {
 164  
                     try {
 165  0
                         return DyDecks.this.get(input.get(DyDeck.RANGE).getS());
 166  0
                     } catch (final IOException ex) {
 167  0
                         throw new IllegalStateException(ex);
 168  
                     }
 169  
                 }
 170  
             }
 171  
         );
 172  
     }
 173  
 
 174  
 }
 175