All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.tools.gfsh.app.commands.get Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.tools.gfsh.app.commands;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import com.gemstone.gemfire.internal.tools.gfsh.app.CommandExecutable;
import com.gemstone.gemfire.internal.tools.gfsh.app.Gfsh;
import com.gemstone.gemfire.internal.tools.gfsh.app.util.ObjectUtil;
import com.gemstone.gemfire.internal.tools.gfsh.app.util.PrintUtil;

public class get implements CommandExecutable
{
	private Gfsh gfsh;
	
	public get(Gfsh gfsh)
	{
		this.gfsh = gfsh;
	}
	
	public void help()
	{
		gfsh.println("get [] | [-k ] | [-?]");
		gfsh.println("     Get value from the current region.");
		gfsh.println("        : field=val1 and field2='val1' \\");
		gfsh.println("                           and field3=to_date('', '')");
		gfsh.println("     Data formats: primitives, String, and java.util.Date");
		gfsh.println("         b|B - Byte      (e.g., 1b)");
		gfsh.println("         c|C - Character (e.g., 1c)");
		gfsh.println("         s|S - Short     (e.g., 12s)");
		gfsh.println("         i|I - Integer   (e.g., 15 or 15i)");
		gfsh.println("         l|L - Long      (e.g., 20l)");
		gfsh.println("         f|F - Float     (e.g., 15.5 or 15.5f)");
		gfsh.println("         d|D - Double    (e.g., 20.0d)");
		gfsh.println("         '' (e.g., '\\'Wow!\\'!' Hello, world')");
		gfsh.println("         to_date('', '')");
		gfsh.println("                       (e.g., to_date('04/10/2009', 'MM/dd/yyyy')");
		gfsh.println("     -k    Get values from the current region using the");
		gfsh.println("                        enumerated keys. Use 'ls -k' to get the list");
		gfsh.println("                        of enumerated keys.");
		gfsh.println("      format: num1 num2 num3-num5 ... e.g., 'get -k 1 2 4 10-20'");
		gfsh.println();
	}
	
	public void execute(String command) throws Exception
	{
		if (command.startsWith("get -?")) {
			help();
		} else if (command.startsWith("get -k")) {
			get_k(command);
		} else if (command.startsWith("get")) {
			get(command);
		}
	}
	
	private void get(String command) throws Exception
	{
		if (gfsh.getCurrentRegion() == null) {
			gfsh.println("Error: Region undefined. Use 'cd' to change region first before executing this command.");
			return;
		}

		LinkedList list = new LinkedList();
		gfsh.parseCommand(command, list);
		if (list.size() < 2) {
			gfsh.println("Error: get requires a query predicate");
		} else {
			String input = (String) list.get(1);
			Object key = null;
			Object value;
			if (input.startsWith("'")) {
				int lastIndex = -1;
				if (input.endsWith("'") == false) {
					lastIndex = input.length();
				} else {
					lastIndex = input.lastIndexOf("'");
				}
				if (lastIndex <= 1) {
					gfsh.println("Error: Invalid key. Empty string not allowed.");
					return;
				}
				key = input.subSequence(1, lastIndex); // lastIndex exclusive
			} else {
				key = ObjectUtil.getPrimitive(gfsh, input, false);
				if (key == null) {
					key = gfsh.getQueryKey(list, 1);
				}
			}
			if (key == null) {
				return;
			}
			long startTime = System.currentTimeMillis();
			value = gfsh.getCurrentRegion().get(key);
			long stopTime = System.currentTimeMillis();
			
			if (value == null) {
				gfsh.println("Key not found.");
				return;
			}
			
			HashMap keyMap = new HashMap();
			keyMap.put(1, key);
			PrintUtil.printEntries(gfsh.getCurrentRegion(), keyMap, null);
			if (gfsh.isShowTime()) {
				gfsh.println("elapsed (msec): " + (stopTime - startTime));
			}
		}
	}
	
	private void get_k(String command) throws Exception
	{
		if (gfsh.getCurrentRegion() == null) {
			gfsh.println("Error: Region undefined. Use 'cd' to change region first before executing this command.");
			return;
		}
		
		// get -k #
		LinkedList list = new LinkedList();
		gfsh.parseCommand(command, list);
		if (list.size() < 3) {
			gfsh.println("Error: get -k requires number(s)");
			return;
		}
		
		if (gfsh.getLsKeyList() == null) {
			gfsh.println("Error: No keys obtained. Execute 'ls -k' first to obtain the keys");
			return;
		}
		
		Map keyMap = gfsh.getKeyMap(list, 2);
		long startTime = System.currentTimeMillis();
		gfsh.getCurrentRegion().getAll(keyMap.values());
		long stopTime = System.currentTimeMillis();
		if (gfsh.isShowResults()) {
			PrintUtil.printEntries(gfsh.getCurrentRegion(), keyMap, null);
		} else {
			gfsh.println("Fetched: " + keyMap.size());
		}
		if (gfsh.isShowTime()) {
			gfsh.println("elapsed (msec): " + (stopTime - startTime));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy