Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Mar 2, 2016
1 parent 7270522 commit 8d24c65
Show file tree
Hide file tree
Showing 38 changed files with 2,517 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/jare/Entrance.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package io.jare;

import io.jare.dynamo.DyBase;
import io.jare.tk.TkApp;
import java.io.IOException;
import org.takes.http.Exit;
Expand Down Expand Up @@ -49,7 +50,7 @@ private Entrance() {
* @throws IOException If fails
*/
public static void main(final String... args) throws IOException {
new FtCLI(new TkApp(), args).start(Exit.NEVER);
new FtCLI(new TkApp(new DyBase()), args).start(Exit.NEVER);
}

}
99 changes: 99 additions & 0 deletions src/main/java/io/jare/dynamo/DyBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.dynamo;

import com.amazonaws.services.dynamodbv2.model.Select;
import com.jcabi.dynamo.Conditions;
import com.jcabi.dynamo.QueryValve;
import com.jcabi.dynamo.Region;
import com.jcabi.dynamo.Table;
import io.jare.model.Base;
import io.jare.model.Domain;
import io.jare.model.User;
import java.util.Iterator;
import java.util.Locale;
import java.util.stream.Collectors;

/**
* Dynamo Base.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
*/
public final class DyBase implements Base {

/**
* The region to work with.
*/
private final transient Region region;

/**
* Ctor.
*/
public DyBase() {
this(new Dynamo());
}

/**
* Ctor.
* @param reg Region
*/
public DyBase(final Region reg) {
this.region = reg;
}

@Override
public User user(final String name) {
return new DyUser(this.region, name);
}

@Override
public Iterator<Domain> domain(final String name) {
return this.table()
.frame()
.through(
new QueryValve()
.withSelect(Select.ALL_ATTRIBUTES)
.withLimit(1)
.withConsistentRead(true)
)
.where(
"domain",
Conditions.equalTo(name.toLowerCase(Locale.ENGLISH))
)
.stream()
.map(DyDomain::new)
.collect(Collectors.<Domain>toList())
.iterator();
}

/**
* Table to work with.
* @return Table
*/
private Table table() {
return this.region.table("domains");
}

}
70 changes: 70 additions & 0 deletions src/main/java/io/jare/dynamo/DyDomain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.dynamo;

import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Item;
import io.jare.model.Domain;
import java.io.IOException;

/**
* Dynamo domain.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
public final class DyDomain implements Domain {

/**
* The item.
*/
private final transient Item item;

/**
* Ctor.
* @param itm Item
*/
public DyDomain(final Item itm) {
this.item = itm;
}

@Override
public String owner() throws IOException {
return this.item.get("user").getS();
}

@Override
public String name() throws IOException {
return this.item.get("domain").getS();
}

@Override
public void delete() throws IOException {
this.item.frame().table().delete(
new Attributes().with("domain", this.name())
);
}

}
118 changes: 118 additions & 0 deletions src/main/java/io/jare/dynamo/DyUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.dynamo;

import com.amazonaws.services.dynamodbv2.model.Select;
import com.jcabi.aspects.Tv;
import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Conditions;
import com.jcabi.dynamo.QueryValve;
import com.jcabi.dynamo.Region;
import com.jcabi.dynamo.Table;
import io.jare.model.Domain;
import io.jare.model.User;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.stream.Collectors;

/**
* Dynamo user.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
public final class DyUser implements User {

/**
* The region to work with.
*/
private final transient Region region;

/**
* The name of him.
*/
private final transient String handle;

/**
* Ctor.
* @param reg Region
* @param name Name of him
*/
public DyUser(final Region reg, final String name) {
this.region = reg;
this.handle = name.toLowerCase(Locale.ENGLISH);
}

@Override
public Iterable<Domain> mine() {
return this.table()
.frame()
.through(
new QueryValve()
.withSelect(Select.ALL_ATTRIBUTES)
.withLimit(Tv.HUNDRED)
.withConsistentRead(false)
.withIndexName("mine")
)
.where("user", Conditions.equalTo(this.handle))
.stream()
.map(DyDomain::new)
.collect(Collectors.toList());
}

@Override
public void add(final String name) throws IOException {
synchronized (this.region) {
final Iterator<Domain> before = new DyBase(this.region)
.domain(name);
if (before.hasNext()) {
final Domain domain = before.next();
if (!domain.owner().equals(this.handle)) {
throw new IOException(
String.format(
"domain \"%s\" is occupied by @%s",
domain.name(), domain.owner()
)
);
}
}
this.table().put(
new Attributes()
.with("user", this.handle)
.with("domain", name.toLowerCase(Locale.ENGLISH))
);
}
}

/**
* Table to work with.
* @return Table
*/
private Table table() {
return this.region.table("domains");
}

}
84 changes: 84 additions & 0 deletions src/main/java/io/jare/dynamo/Dynamo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.dynamo;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.jcabi.dynamo.Credentials;
import com.jcabi.dynamo.Region;
import com.jcabi.dynamo.Table;
import com.jcabi.dynamo.retry.ReRegion;
import com.jcabi.log.Logger;
import com.jcabi.manifests.Manifests;

/**
* Dynamo.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 1.0
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class Dynamo implements Region {

/**
* Region.
*/
private final transient Region region = Dynamo.connect();

@Override
public AmazonDynamoDB aws() {
return this.region.aws();
}

@Override
public Table table(final String name) {
return this.region.table(name);
}

/**
* Connect.
* @return Region
*/
private static Region connect() {
final String key = Manifests.read("Jare-DynamoKey");
final Credentials creds = new Credentials.Simple(
key, Manifests.read("Jare-DynamoSecret")
);
final Region region;
if (key.startsWith("AAAAA")) {
final int port = Integer.parseInt(
System.getProperty("dynamo.port")
);
region = new Region.Simple(new Credentials.Direct(creds, port));
Logger.warn(Dynamo.class, "test DynamoDB at port #%d", port);
} else {
region = new Region.Prefixed(
new ReRegion(new Region.Simple(creds)),
"jare-"
);
}
Logger.info(Dynamo.class, "DynamoDB connected as %s", key);
return region;
}

}
Loading

0 comments on commit 8d24c65

Please sign in to comment.