Friday, October 10, 2014

Sevntu Checkstyle release 1.12.0

Official page: http://sevntu-checkstyle.github.io/sevntu.checkstyle/#1.12.0

New and noteworthy:




Increased Check's handling of redundant returns in constructors and void methods, particularly: fixed problem of handling redundant returns in try-catch-finally blocks. Done by Aleksey Nesterenko.


Now this Check puts violation on throwing anonymous exception even in case if it is defined before throwing. Done by Aleksey Nesterenko.


Now check prints The Current Line Length In all it's warnings. Done by Aleksey Nesterenko.


This Check highlights variable definition statements where diamond operator could be used.
Rationale: using diamond operator (introduced in Java 1.7) leads to shorter code
and better code readability. It is suggested by Oracle that the diamond primarily using
for variable declarations. E.g. of statements: 

Without diamond operator:

Map<String, Map<String, Integer>> someMap = new HashMap<String, Map<String, Integer>>();

With diamond operator:

Map<String, Map<String, Integer>> someMap = new HashMap<>(); 

Done by Aleksey Nesterenko.



This Check warns on propagation of inner private types to outer classes:
- Externally accessible method if it returns private inner type.
- Externally accessible field if it's type is a private inner type.
These types could be private inner classes, interfaces or enumerations.

Example: 

 class OuterClass {

  public InnerClass innerFromMain = new InnerClass(); //WARNING
  private class InnerClass { ... } 
  public InnerClass  getValue() { //WARNING
      return new InnerClass(); 
  }
   
  private interface InnerInterface { ... }
  public Set<InnerInterface> getValue() { //WARNING
      return new TreeSet<InnerInterface>;
  }
  
  private Enum Fruit {Apple, Pear}
  public Fruit getValue() { //WARNING
      return Fruit.Apple;
  }
  
  public someMethod(InnerClass innerClass) { ... }  //WARNING
  
 }

Rationale: it is possible to return private inner type or use it as the parameter of non-private method, but it is impossible to use it in other classes (besides inner classes) unless it extends or implements at least one non-private class or interface. Such situation usually happens after bulk refactoring and usually means dead/useless code.

Done by Aleksey Nesterenko.
  • Replaced existing NestedTernaryCheck with more general TernaryPerExpressionCountCheck.
New TernaryPerExpressionCountCheck restricts the number of ternary operators in expression to a specific limit.

Rationale: This Check helps to improve code readability by pointing developer on
expressions which contain more than user-defined count of ternary operators.

It points to complicated ternary expressions. Reason:
- Complicated ternary expressions are not easy to read.
- Complicated ternary expressions could lead to ambiguous result if user
does not know Java's operators priority well, e.g.:

 String str = null;
 String x = str != null ? "A" : "B" + str == null ? "C" : "D";
 System.out.println(x);

Output for code above is "D", but more obvious would be "BC".

Check has following properties:
maxTernaryPerExpressionCount - limit of ternary operators per expression
ignoreTernaryOperatorsInBraces - if true Check will ignore ternary operators
in braces (braces explicitly set priority level)
ignoreIsolatedTernaryOnLine - if true Check will ignore one line ternary operators,
if only it is places in line alone.
Options ignoreTernaryOperatorsInBraces and ignoreIsolatedTernaryOnLine can
make Check less strict, e.g.:
Using ignoreTernaryOperatorsInBraces option (value = true)
does not put violation on code below:

 callString = "{? = call " +
   (StringUtils.hasLength(catalogNameToUse) 
   ? catalogNameToUse + "." : "") +
   (StringUtils.hasLength(schemaNameToUse) 
   ? schemaNameToUse + "." : "") +
   procedureNameToUse + "(";

When using ignoreIsolatedTernaryOnLine (value = true), even without
ignoreTernaryOperatorsInBraces option Check won't warn on code below:

 int a = (d == 5) ? d : f
   +
   ((d == 6) ? g : k); 

Done by Aleksey Nesterenko.


  • Update in CustomDeclarationOrderCheck

Updated Check to treat more king of methods as setters. E.g., now Check treats 'set' method as setter, in cases like:

public void setWorkMode(String workMode) {
this.workMode = WorkMode.valueOf(workMode);
}

public void setUserCache(UserCache userCache) {
super.setUserCache(userCache);
}

public void setY(Integer y) { // setter
this.y = Integer.parseInt(y + "");
}

Done by Alexey Nesterenko.

General information:

Repository: https://github.com/sevntu-checkstyle/sevntu.checkstyle
mail-list: https://groups.google.com/forum/?hl=en#!forum/sevntu-checkstyle

Tuesday, October 7, 2014

Oracle: check real table size on disk

SELECT
   owner, table_name, TRUNC(sum(bytes)/1024/1024) Meg
FROM
(SELECT segment_name table_name, owner, bytes
 FROM dba_segments
 WHERE segment_type = 'TABLE'
 UNION ALL
 SELECT i.table_name, i.owner, s.bytes
 FROM dba_indexes i, dba_segments s
 WHERE s.segment_name = i.index_name
 AND   s.owner = i.owner
 AND   s.segment_type = 'INDEX'
 UNION ALL
 SELECT l.table_name, l.owner, s.bytes
 FROM dba_lobs l, dba_segments s
 WHERE s.segment_name = l.segment_name
 AND   s.owner = l.owner
 AND   s.segment_type = 'LOBSEGMENT'
 UNION ALL
 SELECT l.table_name, l.owner, s.bytes
 FROM dba_lobs l, dba_segments s
 WHERE s.segment_name = l.index_name
 AND   s.owner = l.owner
 AND   s.segment_type = 'LOBINDEX')
WHERE owner in ('SCHEMA_NAME')
GROUP BY table_name, owner
HAVING SUM(bytes)/1024/1024 > 10  /* Ignore really small tables */
ORDER BY SUM(bytes) desc ;

Tuesday, August 19, 2014

How to check UNDO tablespace total and occupied size

select tablespace_name,sum(bytes) total_bytes from dba_data_files where tablespace_name like '%UNDO%' group by tablespace_name;

select tablespace_name,sum(bytes) occupied_bytes from dba_segments where tablespace_name like '%UNDO%' group by tablespace_name;

Thursday, August 14, 2014

How to ssh to host and set terminal title to hostname:path_on_the_host in a single command

 1. Create file ~/ssht.sh with content:

#!/bin/bash
SETTP='MY_PROMPT="$HOSTNAME:$PWD\$ "'
SETTP="$SETTP;"'MY_TITLE="\[\e]0;$HOSTNAME:$PWD\a\]"'
SETTP="$SETTP;"'PS1="$MY_TITLE$MY_PROMPT"'
ssh -t <YOUR_NICKNAME>@$1 "export PROMPT_COMMAND='eval '\\''$SETTP'\\'; bash --login"

2. Make it executable:

chmod ugo+x ~/ssht.sh

3. add alias to this script into your .profile:

alias ssht="~/ssht.sh"

4. Use:

ssht <HOST_NAME>

Thursday, July 24, 2014

Oracle: how to remove duplicate constraints of 'CHECK' type


create or replace function get_search_condition(ownerr in varchar2, p_cons_name in varchar2) return varchar2 authid current_user
 is
   l_search_condition user_constraints.search_condition%type;
 begin
  select search_condition into l_search_condition from all_constraints where constraint_name = p_cons_name and owner = ownerr;
  return l_search_condition;
end;
/

--select 'ALTER TABLE ' || owner ||'.'|| table_name || ' DROP CONSTRAINT ' ||  regexp_replace(constraints, '^\w+; ', '') || ';' from (
  select owner, table_name, search_condition
  , listagg(constraint_name, '; ') within group (order by constraint_name) constraints
  from (
    select owner, table_name, regexp_replace(lower(get_search_condition(owner, constraint_name)), '\s+', ' ') search_condition, constraint_name
      from all_constraints where owner in (
      select owner from dba_tables where owner not in ('SYS', 'SYSTEM', 'APEX_040000', 'MDSYS', 'CTXSYS', 'FLOWS_FILES', 'OUTLN', 'XDB')
      ) and constraint_type = 'C' and search_condition IS NOT NULL
      order by table_name, constraint_name
  ) group by owner, table_name, search_condition having (count(constraint_name) > 1)
--)
;
drop function get_search_condition;

Tuesday, July 22, 2014

Remove obsolete GIT branches

I don't know an option to do this in a single command so I am doing it like below:

git checkout master
comm -23 <( git branch | grep -v "/" | grep -v "*" | sort ) <( git branch -r | awk -F '/' '{print $2}' | sort ) | awk '{print "git branch -D " $1}'
# prints 'git branch -D' commands for every obsolete branch. The next step is to apply them manually.

You could modify command to remove obsolete branches automatically (on your own risk):

comm -23 <( git branch | grep -v "/" | grep -v "*" | sort ) <( git branch -r | awk -F '/' '{print $2}' | sort ) | xargs git branch -D 

Another option to remove obsolete branches is:

git for-each-ref --format='%(refname:short) %(upstream)' refs/heads/ | awk '$2 !~/^refs\/(remotes|heads)/' | xargs git branch -D

Friday, July 4, 2014

Linux: how to kill all process instances by process name


ps -elf | grep <process_name> | grep -v grep | awk '{print $4}' | xargs kill -9

Example:

ps -elf | grep maven | grep -v grep | awk '{print $4}' | xargs kill -9 

Tuesday, May 27, 2014

Oracle: working with DBMS STATS

Useful queries:

Check when statistics was gathered:
SELECT owner, table_name, last_analyzed FROM all_tables where owner='<SCHEMA_NAME>' ORDER BY last_analyzed DESC NULLS LAST; --Tables.
SELECT owner, index_name, last_analyzed FROM all_indexes where owner='<SCHEMA_NAME>' ORDER BY last_analyzed DESC NULLS LAST; -- Indexes.

Friday, February 21, 2014

Oracle: get incoming and outgoing grants for specific user

-- List of incoming grants to specific user/schema:
select * from (
  select tpm.name privilege,
     decode(mod(oa.option$,2), 1, 'YES', 'NO') grantable,
     ue.name grantee,
     ur.name grantor,
     u.name owner,
     decode(o.TYPE#, 0, 'NEXT OBJECT', 1, 'INDEX', 2, 'TABLE', 3, 'CLUSTER',
     4, 'VIEW', 5, 'SYNONYM', 6, 'SEQUENCE',
     7, 'PROCEDURE', 8, 'FUNCTION', 9, 'PACKAGE',
     11, 'PACKAGE BODY', 12, 'TRIGGER',
     13, 'TYPE', 14, 'TYPE BODY',
     19, 'TABLE PARTITION', 20, 'INDEX PARTITION', 21, 'LOB',
     22, 'LIBRARY', 23, 'DIRECTORY', 24, 'QUEUE',
     28, 'JAVA SOURCE', 29, 'JAVA CLASS', 30, 'JAVA RESOURCE',
     32, 'INDEXTYPE', 33, 'OPERATOR',
     34, 'TABLE SUBPARTITION', 35, 'INDEX SUBPARTITION',
     40, 'LOB PARTITION', 41, 'LOB SUBPARTITION',
     42, 'MATERIALIZED VIEW',
     43, 'DIMENSION',
     44, 'CONTEXT', 46, 'RULE SET', 47, 'RESOURCE PLAN',
     66, 'JOB', 67, 'PROGRAM', 74, 'SCHEDULE',
     48, 'CONSUMER GROUP',
     51, 'SUBSCRIPTION', 52, 'LOCATION',
     55, 'XML SCHEMA', 56, 'JAVA DATA',
     57, 'EDITION', 59, 'RULE',
     62, 'EVALUATION CONTEXT',
     'UNDEFINED') object_type,
     o.name object_name, '' column_name
      from sys.objauth$ oa, sys.obj$ o, sys.user$ u, sys.user$ ur, sys.user$ ue, table_privilege_map tpm
      where oa.obj# = o.obj# and oa.grantor# = ur.user# and oa.grantee# = ue.user#
        and oa.col# is null and oa.privilege# = tpm.privilege and u.user# = o.owner#
        and o.TYPE# in (2, 4, 6, 9, 7, 8, 42, 23, 22, 13, 33, 32, 66, 67, 74, 57) and bitand (o.flags, 128) = 0
  union all --column level grants
  select tpm.name privilege, decode(mod(oa.option$,2), 1, 'YES', 'NO') grantable, ue.name grantee, ur.name grantor,
         u.name owner, decode(o.TYPE#, 2, 'TABLE', 4, 'VIEW', 42, 'MATERIALIZED VIEW') object_type, o.name object_name, c.name column_name
  from sys.objauth$ oa, sys.obj$ o, sys.user$ u, sys.user$ ur, sys.user$ ue,
       sys.col$ c, table_privilege_map tpm
  where oa.obj# = o.obj# and oa.grantor# = ur.user# and oa.grantee# = ue.user# and oa.obj# = c.obj#
    and oa.col# = c.col# and bitand(c.property, 32) = 0 and oa.col# is not null
    and oa.privilege# = tpm.privilege and u.user# = o.owner# and o.TYPE# in (2, 4, 42)
    and bitand (o.flags, 128) = 0
) where owner = '<user>';



-- List of outgoing grants from specific user/schema:
select * from (
  select tpm.name privilege,
     decode(mod(oa.option$,2), 1, 'YES', 'NO') grantable,
     ue.name grantee,
     ur.name grantor,
     u.name owner,
     decode(o.TYPE#, 0, 'NEXT OBJECT', 1, 'INDEX', 2, 'TABLE', 3, 'CLUSTER',
     4, 'VIEW', 5, 'SYNONYM', 6, 'SEQUENCE',
     7, 'PROCEDURE', 8, 'FUNCTION', 9, 'PACKAGE',
     11, 'PACKAGE BODY', 12, 'TRIGGER',
     13, 'TYPE', 14, 'TYPE BODY',
     19, 'TABLE PARTITION', 20, 'INDEX PARTITION', 21, 'LOB',
     22, 'LIBRARY', 23, 'DIRECTORY', 24, 'QUEUE',
     28, 'JAVA SOURCE', 29, 'JAVA CLASS', 30, 'JAVA RESOURCE',
     32, 'INDEXTYPE', 33, 'OPERATOR',
     34, 'TABLE SUBPARTITION', 35, 'INDEX SUBPARTITION',
     40, 'LOB PARTITION', 41, 'LOB SUBPARTITION',
     42, 'MATERIALIZED VIEW',
     43, 'DIMENSION',
     44, 'CONTEXT', 46, 'RULE SET', 47, 'RESOURCE PLAN',
     66, 'JOB', 67, 'PROGRAM', 74, 'SCHEDULE',
     48, 'CONSUMER GROUP',
     51, 'SUBSCRIPTION', 52, 'LOCATION',
     55, 'XML SCHEMA', 56, 'JAVA DATA',
     57, 'EDITION', 59, 'RULE',
     62, 'EVALUATION CONTEXT',
     'UNDEFINED') object_type,
     o.name object_name, '' column_name
      from sys.objauth$ oa, sys.obj$ o, sys.user$ u, sys.user$ ur, sys.user$ ue, table_privilege_map tpm
      where oa.obj# = o.obj# and oa.grantor# = ur.user# and oa.grantee# = ue.user#
        and oa.col# is null and oa.privilege# = tpm.privilege and u.user# = o.owner#
        and o.TYPE# in (2, 4, 6, 9, 7, 8, 42, 23, 22, 13, 33, 32, 66, 67, 74, 57) and bitand (o.flags, 128) = 0
  union all --column level grants
  select tpm.name privilege, decode(mod(oa.option$,2), 1, 'YES', 'NO') grantable, ue.name grantee, ur.name grantor,
         u.name owner, decode(o.TYPE#, 2, 'TABLE', 4, 'VIEW', 42, 'MATERIALIZED VIEW') object_type, o.name object_name, c.name column_name
  from sys.objauth$ oa, sys.obj$ o, sys.user$ u, sys.user$ ur, sys.user$ ue,
       sys.col$ c, table_privilege_map tpm
  where oa.obj# = o.obj# and oa.grantor# = ur.user# and oa.grantee# = ue.user# and oa.obj# = c.obj#
    and oa.col# = c.col# and bitand(c.property, 32) = 0 and oa.col# is not null
    and oa.privilege# = tpm.privilege and u.user# = o.owner# and o.TYPE# in (2, 4, 42)
    and bitand (o.flags, 128) = 0
) where grantee = '<user>';