All Products
Search
Document Center

Configure a routing algorithm

Last Updated: Dec 11, 2019

Table-based routing

  • In DMS Enterprise, a table-based routing algorithm is defined by using a Groovy expression, which is similar to a routing algorithm configured in the application code.

Format

  • #shardKey#. That is, a complete algorithm expression is as follows: #Routing field#.

Case studies

  • Table-based routing
    • Simple modulo operations
      • Numeric modulo operation: #user_id#%100
      • Secondary modulo operation: #user_id#%10000%100
      • Numeric Java Hash modulo operation: Math.abs(#user_id#.hashCode())%100
    • String modulo operations
      • Numeric string hashing: Math.abs(#user_id#.toString().hashCode())%100
      • String hashing: Math.abs(#user_id#.hashCode())%100
      • CobarHash: Math.abs(cobarHash(#column#,start, end)).intdiv(8)
      • CobarOldHash: Math.abs(cobarOldHash(#column#, len)).intdiv(8)
  • Routing based on the database rule
    • Method of routing to the database if the names of tables are the same: 'schema_prefix_'+(#user_id#%10)+'.table_name'
    • Database and table names:
      • 'schema_prefix_'+(#user_id#%100)+'.table_name_prefix_'+(#user_id#%1000)
      • 'schema_prefix_'+lastSwapZero(String.valueOf((#user_id#%1024).intdiv(128),4)+'.table_name_prefix_'+lastSwapZero(String.valueOf((#user_id#%128)),4) The same set of physical tables are used in each physical database.
      • 'schema_prefix_'+substring(#EXTEND_ID#,16,18).toLong().intdiv(2)+'.table_name_prefix_'+substring(#EXTEND_ID#,16,18) Divide the integer formed by the sixteenth and seventh digits in the string by 2 and use the resultant value to route to the corresponding database. Use the sixteenth and seventh digits in the string to route to the corresponding table.
  • Date-based routing
    • Routing to the same table for the same day of each month: dayOfMonth(#time#)
  • Routing based on the third-to-last digit of the string
    • If the suffix of the table name increases by 10 each time, multiply the third-to-last digit by 10. If the suffix increases by 1 each time, do not multiply the third-to-last digit by 10: Integer.valueOf(substring(#ip_id#,-3,-2))*10
  • Other types of complex routing
    • Routing by using a custom function: [String func(String arg){ return arg.hashCode()%10;} 'table_name_'+func(#user_id#)+'_other_'func(#user_id#)]
  • For more information about Groovy, see Groovy.

General built-in functions

  • cobarOldHash

    • Algorithm description: old cobarHash algorithm

      ```

public static long cobarOldHash(String s, int len) { long h = 0; int sLen = s.length(); for (int i = 0; (i < len && i < sLen); i++) { h = (h << 5) - h + s.charAt(i); } return h; }

  ```
  • cobarHash

    • Algorithm description: new cobarHash algorithm

      ```

public static long cobarHash(String s, int start, int end) { if (start < 0) { start = 0; } if (end > s.length()) { end = s.length(); } long h = 0; for (int i = start; i < end; ++i) { h = (h << 5) - h + s.charAt(i); } return h; }

```
  • weekOfYear

    • Algorithm description: This algorithm calculates the number of a week in a year.

      ```

public static int weekOfYear(String dateValue) { Date date = DateTimeUtils.getSomeDate(dateValue); if(date != null) { return DateTimeUtils.getWeekOfYear(date); }

return 0;
}
```
  • dayOfYear

    • Algorithm description: This algorithm calculates the number of a day in a year.

      ```

public static int dayOfYear(String dateValue) { Date date = DateTimeUtils.getSomeDate(dateValue); if(date ! = null) { return DateTimeUtils.getDayOfYear(date); }

return 0;
}
```
  • dayOfMonth

    • Algorithm description: This algorithm calculates the number of a day in a month.

      ```

public static int dayOfMonth(String dateValue) { Date date = DateTimeUtils.getSomeDate(dateValue); if (date != null) { return DateTimeUtils.getDayOfMonth(date); } return 0; }

```
  • dayOfWeek

    • Algorithm description: This algorithm calculates the number of a day in a week.

      ```

public static int dayOfWeek(String dateValue) { Date date = DateTimeUtils.getSomeDate(dateValue); if (date ! = null) { int dayOfWeek = DateTimeUtils.getDayOfWeek(date); if (dayOfWeek==1){ dayOfWeek=7; }else { dayOfWeek=dayOfWeek-1; } return dayOfWeek; } return 0; }

```
  • substring

    • Algorithm description: This algorithm calculates the start and end positions of the captured string. The start and end positions can be negative, indicating that the string is captured from back to front.

      ```

public static String substring(String value, int start, int end) { return StringUtils.substring(value, start, end); }

```
  • substring

    • Algorithm description: This algorithm calculates the start position of the captured string.

      ```

public static String substring(String value, int start) { return StringUtils.substring(value, start); }

```
  • last4swap

    • Algorithm description: This algorithm captures the last four digits in the suffix of the string. If there are only three digits, 0 is added to the front of the three digits. The first and second digits are automatically exchanged with the third and fourth digits.

      ```

public static String last4swap(String value) { if(value.length() < 4) { value = StringUtils.leftPad(value, 4, '0'); } return StringUtils.substring(value, -2)+StringUtils.substring(value, -4, -2); }

```
  • lastSwapZero

    • Algorithm description: This algorithm captures the string of a specified length. If the captured string is not long enough, 0 is added before the corresponding digit.

      public static String lastSwapZero(String value, int length) {
         if (value.length() < length) {
             return StringUtils.leftPad(value, length, '0');
         }
         return value;
      }