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.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseClusterTestCase;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  /**
34   * Test versions.
35   * Does shutdown in middle of test to prove versions work across restart.
36   */
37  public class TestGetRowVersions extends HBaseClusterTestCase {
38    private static final Log LOG = LogFactory.getLog(TestGetRowVersions.class);
39  
40    private static final String TABLE_NAME = "test";
41    private static final byte [] CONTENTS = Bytes.toBytes("contents");
42    private static final byte [] ROW = Bytes.toBytes("row");
43    private static final byte [] VALUE1 = Bytes.toBytes("value1");
44    private static final byte [] VALUE2 = Bytes.toBytes("value2");
45    private static final long TIMESTAMP1 = 100L;
46    private static final long TIMESTAMP2 = 200L;
47  
48    @Override
49    public void setUp() throws Exception {
50      super.setUp();
51      HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
52      desc.addFamily(new HColumnDescriptor(CONTENTS));
53      HBaseAdmin admin = new HBaseAdmin(conf);
54      admin.createTable(desc);
55    }
56  
57    /** @throws Exception */
58    public void testGetRowMultipleVersions() throws Exception {
59      Put put = new Put(ROW, TIMESTAMP1, null);
60      put.add(CONTENTS, CONTENTS, VALUE1);
61      HTable table = new HTable(new Configuration(conf), TABLE_NAME);
62      table.put(put);
63      // Shut down and restart the HBase cluster
64      this.cluster.shutdown();
65      this.zooKeeperCluster.shutdown();
66      LOG.debug("HBase cluster shut down -- restarting");
67      this.hBaseClusterSetup();
68      // Make a new connection.  Use new Configuration instance because old one
69      // is tied to an HConnection that has since gone statle.
70      table = new HTable(new Configuration(conf), TABLE_NAME);
71      // Overwrite previous value
72      put = new Put(ROW, TIMESTAMP2, null);
73      put.add(CONTENTS, CONTENTS, VALUE2);
74      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 }