Saturday, March 30, 2019

JSON Serialization and Deserialization

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation.
When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.
Say, you have an object
{foo: [1, 4, 7, 10], bar: "baz"}
serializing into JSON will convert it into a string:
which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}.

DynamoDB Read/Write Capacity

Read Request Units and Write Request Units

  • One read request unit represents one strongly consistent read request, or two eventually consistent read requests, for an item up to 4 KB in size. Transactional read requests require 2 read request units to perform one read for items up to 4 KB. If you need to read an item that is larger than 4 KB, DynamoDB needs additional read request units. The total number of read request units required depends on the item size, and whether you want an eventually consistent or strongly consistent read. For example, if your item size is 8 KB, you require 2 read request units to sustain one strongly consistent read, 1 read request unit if you choose eventually consistent reads, or 4 read request units for a transnational read request.
  • One write request unit represents one write for an item up to 1 KB in size. If you need to write an item that is larger than 1 KB, DynamoDB needs to consume additional write request units. Transactional write requests require 2 write request units to perform one write for items up to 1 KB. The total number of write request units required depends on the item size. For example, if your item size is 2 KB, you require 2 write request units to sustain one write request or 4 write request units for a transnational write request. 
    Read Consistency

    Eventually Consistent Reads
    When you read data from a DynamoDB table, the response might not reflect the results of a recently completed write operation. The response might include some stale data. If you repeat your read request after a short time, the response should return the latest data.
    Strongly Consistent Reads
    When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful. A strongly consistent read might not be available if there is a network delay or outage. Consistent reads are not supported on global secondary indexes (GSI).

    DynamoDB uses eventually consistent reads, unless you specify otherwise. Read operations (such as GetItem, Query, and Scan) provide a ConsistentRead parameter. If you set this parameter to true, DynamoDB uses strongly consistent reads during the operation. 

Friday, March 15, 2019

Modulo Operator: Java

Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus or remainder operator. The % operator returns the remainder of two numbers. 

So 20 modulo 5 is 0 because 20 divided by 5 is 4 with no remainder.
    21 modulo 5 is 1 22 modulo 5 is 2 23 modulo 5 is 3 24 modulo 5 is 4 25 modulo 5 is 0
In C, C++ and Java, modulo is represented as the percent sign. So
    int a = 20 % 5 ;

The most common use case for the modulo operator is to find out if a given number is odd or even.
Modulo has a variety of uses. If you want to know if a number is an even "hundred", like 300, 400, 500 or 256700, you can test for it like this:
    if ( ( a % 100 ) == 0 )
        System.out.println( a + "exactly!");

If the outcome of the modulo operation between any number and two is equal to one, it’s an odd number:
public void whenDivisorIsOddAndModulusIs2_thenResultIs1() {
    assertThat(3 % 2).isEqualTo(1);
On the other hand, if the result is zero (i.e. there is no remainder), it’s an even number:
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0() {
    assertThat(4 % 2).isEqualTo(0);