Coverage Report - com.thindeck.dynamo.DyEvents
 
Classes in this File Line Coverage Branch Coverage Complexity
DyEvents
0%
0/14
0%
0/22
2
DyEvents$1
0%
0/4
N/A
2
 
 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.AttributeValue;
 33  
 import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
 34  
 import com.amazonaws.services.dynamodbv2.model.Condition;
 35  
 import com.google.common.base.Function;
 36  
 import com.google.common.collect.Iterables;
 37  
 import com.jcabi.aspects.Immutable;
 38  
 import com.jcabi.dynamo.Attributes;
 39  
 import com.jcabi.dynamo.Item;
 40  
 import com.jcabi.dynamo.QueryValve;
 41  
 import com.jcabi.dynamo.Region;
 42  
 import com.jcabi.manifests.Manifests;
 43  
 import com.thindeck.api.Events;
 44  
 import java.io.IOException;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import lombok.ToString;
 47  
 
 48  
 /**
 49  
  * Dynamo implementation of {@link com.thindeck.api.Events}.
 50  
  *
 51  
  * @author Krzysztof Krason (Krzysztof.Krason@gmail.com)
 52  
  * @author Yegor Bugayenko (yegor@teamed.io)
 53  
  * @version $Id$
 54  
  */
 55  0
 @ToString
 56  
 @Immutable
 57  0
 @EqualsAndHashCode(of = { "region", "deck" })
 58  
 final class DyEvents implements Events {
 59  
 
 60  
     /**
 61  
      * Table name.
 62  
      */
 63  
     public static final String TBL = "events";
 64  
 
 65  
     /**
 66  
      * Name of the deck.
 67  
      */
 68  
     public static final String HASH = "deck";
 69  
 
 70  
     /**
 71  
      * Time of event (msec).
 72  
      */
 73  
     public static final String RANGE = "msec";
 74  
 
 75  
     /**
 76  
      * Head.
 77  
      */
 78  
     public static final String ATTR_HEAD = "head";
 79  
 
 80  
     /**
 81  
      * XML.
 82  
      */
 83  
     public static final String ATTR_TEXT = "text";
 84  
 
 85  
     /**
 86  
      * Version of the system, to show in header.
 87  
      */
 88  0
     private static final String VERSION = String.format(
 89  
         "%s %s %s",
 90  
         // @checkstyle MultipleStringLiterals (3 lines)
 91  
         Manifests.read("Thindeck-Version"),
 92  
         Manifests.read("Thindeck-Revision"),
 93  
         Manifests.read("Thindeck-Date")
 94  
     );
 95  
 
 96  
     /**
 97  
      * Region.
 98  
      */
 99  
     private final transient Region region;
 100  
 
 101  
     /**
 102  
      * Name of deck, for example "yegor256/test".
 103  
      */
 104  
     private final transient String deck;
 105  
 
 106  
     /**
 107  
      * Ctor.
 108  
      * @param reg Region
 109  
      * @param dck Deck name
 110  
      */
 111  0
     DyEvents(final Region reg, final String dck) {
 112  0
         this.region = reg;
 113  0
         this.deck = dck;
 114  0
     }
 115  
 
 116  
     @Override
 117  
     public Iterable<String> iterate(final long since) {
 118  0
         return Iterables.transform(
 119  
             this.region.table(DyEvents.TBL)
 120  
                 .frame()
 121  
                 .through(new QueryValve().withScanIndexForward(false))
 122  
                 .where(DyEvents.HASH, this.deck)
 123  
                 .where(
 124  
                     DyEvents.RANGE,
 125  
                     new Condition()
 126  
                         .withComparisonOperator(ComparisonOperator.LE)
 127  
                         .withAttributeValueList(
 128  
                             new AttributeValue().withN(Long.toString(since))
 129  
                         )
 130  
                 ),
 131  0
             new Function<Item, String>() {
 132  
                 @Override
 133  
                 public String apply(final Item input) {
 134  
                     try {
 135  0
                         return String.format(
 136  
                             "%s\n%s\n%s",
 137  
                             input.get(DyEvents.ATTR_HEAD).getS(),
 138  
                             input.get(DyEvents.RANGE).getN(),
 139  
                             input.get(DyEvents.ATTR_TEXT).getS()
 140  
                         );
 141  0
                     } catch (final IOException ex) {
 142  0
                         throw new IllegalStateException(ex);
 143  
                     }
 144  
                 }
 145  
             }
 146  
         );
 147  
     }
 148  
 
 149  
     @Override
 150  
     public void create(final String text) throws IOException {
 151  0
         final StringBuilder log = new StringBuilder(Drain.INSTANCE.fetch());
 152  0
         if (log.length() > 0) {
 153  0
             log.append('\n').append(DyEvents.VERSION);
 154  0
             this.region.table(DyEvents.TBL).put(
 155  
                 new Attributes()
 156  
                     .with(DyEvents.HASH, this.deck)
 157  
                     .with(
 158  
                         DyEvents.RANGE,
 159  
                         new AttributeValue().withN(
 160  
                             Long.toString(System.currentTimeMillis())
 161  
                         )
 162  
                     )
 163  
                     .with(DyEvents.ATTR_HEAD, text)
 164  
                     .with(DyEvents.ATTR_TEXT, log)
 165  
             );
 166  
         }
 167  0
     }
 168  
 }