1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.client;
22  
23  import java.util.NavigableMap;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HBaseClusterTestCase;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * Test versions.
34   * Does shutdown in middle of test to prove versions work across restart.
35   */
36  public class TestGetRowVersions extends HBaseClusterTestCase {
37    private static final Log LOG = LogFactory.getLog(TestGetRowVersions.class);
38  
39    private static final String TABLE_NAME = "test";
40    private static final byte [] CONTENTS = Bytes.toBytes("contents");
41    private static final byte [] ROW = Bytes.toBytes("row");
42    private static final byte [] VALUE1 = Bytes.toBytes("value1");
43    private static final byte [] VALUE2 = Bytes.toBytes("value2");
44    private static final long TIMESTAMP1 = 100L;
45    private static final long TIMESTAMP2 = 200L;
46    private HBaseAdmin admin = null;
47    private HTable table = null;
48  
49    @Override
50    public void setUp() throws Exception {
51      super.setUp();
52      HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
53      desc.addFamily(new HColumnDescriptor(CONTENTS));
54      this.admin = new HBaseAdmin(conf);
55      this.admin.createTable(desc);
56      this.table = new HTable(conf, TABLE_NAME);
57    }
58  
59    /** @throws Exception */
60    public void testGetRowMultipleVersions() throws Exception {
61      Put put = new Put(ROW, TIMESTAMP1, null);
62      put.add(CONTENTS, CONTENTS, VALUE1);
63      this.table.put(put);
64      // Shut down and restart the HBase cluster
65      this.cluster.shutdown();
66      this.zooKeeperCluster.shutdown();
67      LOG.debug("HBase cluster shut down -- restarting");
68      this.hBaseClusterSetup();
69      // Make a new connection
70      this.table = new HTable(conf, TABLE_NAME);
71      // Overwrite previous value
72      put = new Put(ROW, TIMESTAMP2, null);
73      put.add(CONTENTS, CONTENTS, VALUE2);
74      this.table.put(put);
75      // Now verify that getRow(row, column, latest) works
76      Get get = new Get(ROW);
77      // Should get one version by default
78      Result r = table.get(get);
79      assertNotNull(r);
80      assertFalse(r.isEmpty());
81      assertTrue(r.size() == 1);
82      byte [] value = r.getValue(CONTENTS, CONTENTS);
83      assertTrue(value.length != 0);
84      assertTrue(Bytes.equals(value, VALUE2));
85      // Now check getRow with multiple versions
86      get = new Get(ROW);
87      get.setMaxVersions();
88      r = table.get(get);
89      assertTrue(r.size() == 2);
90      value = r.getValue(CONTENTS, CONTENTS);
91      assertTrue(value.length != 0);
92      assertTrue(Bytes.equals(value, VALUE2));
93      NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map =
94        r.getMap();
95      NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap =
96        map.get(CONTENTS);
97      NavigableMap<Long, byte[]> versionMap = familyMap.get(CONTENTS);
98      assertTrue(versionMap.size() == 2);
99      assertTrue(Bytes.equals(VALUE1, versionMap.get(TIMESTAMP1)));
100     assertTrue(Bytes.equals(VALUE2, versionMap.get(TIMESTAMP2)));
101   }
102 }