Posts Tagged - experience-sheet

Android & Kotlin experience cheat sheet

Android

Store secrets

Go to gradle.properties and save it there as key/value pairs. Exclude this file from GIT. This make it harder for others to read your secrets from your repository or using the APK file.

We could use Android’s ProGuard to make this more secure. This doesn’t provide 100% security. The best way is to keep your keys outside the project. Store them in a backend server and retrieve them after authentication. Checkout this article and this one for more information on storing secrets in Android.

MY_KEY = "1029831283712090989087"

Open the app level build.gradle file.

android {
	defaultConfig {
		buildConfigField("String", "API_KEY", MY_KEY)
	}
	
	buildTypes {
		release {
			// this deletes unused classes and files from the APK
			minifyEnabled true
			shrinkResources true
		}
	}
}

Read More

Rust experience sheet

Relevant Post: From Java to Rust

Answers to questions I’ve spent some time looking on the Internet to find an answer to, or which come to be relevant often.

Compare between String and &str

If we’re comparing 2 Strings and Rust shows the error expected 'String', found '&str' is because we forgot to add the .trim() part to buffer.

let mut buffer = String::new();
io::stdin().lock()
  .read_line(&mut buffer)
  .expect("Couldn't read user input");

if buffer.trim() == "yes" {
  // do action
}  

Read More

MySQL user privileges

How to grant privileges for a database to a user, when both already exist.

In this case the database name will be project_node1, the user project_user and the password project_pass. All the following commands have to be executed as root or with a user with enough privileges.

-- list system users
SELECT user, host FROM mysql.user;

-- see current privileges
SHOW GRANTS FOR 'project_user'@'%';

-- delete all previous privileges (if needed)
-- REVOKE ALL PRIVILEGES ON `project_node1`.* FROM 'project_user'@'%';

-- grant new privileges and flush
GRANT ALL PRIVILEGES ON `project_node1`.* TO 'project_user'@'%';
FLUSH PRIVILEGES;

Reference(s)

https://serverfault.com/questions/115950/how-do-i-change-the-privileges-for-mysql-user-that-is-already-created

Read More

MongoDB

Config

Config. to run MongoDB

  1. Set PATH as system variable (~/.bashrc)
  2. Create folders /data/db and set rw- permissions to correct user.
  3. start mongod

Queries

Find

by element

db.getCollection('documentX').find({ _id : ObjectId("5b7e99a9149559198c5024a4") })

Read More

SQL Cheat Sheet

DDL Queries

  • Show all dbs - SHOW DATABASES;
  • Select one - USE {$db};
  • Show tables in a db - SHOW TABLES;
  • Show description of DB - DESCRIBE {$db}
Create new table
CREATE TABLE table_name (
	id numeric,
	description text);

Read More

Java experience sheet

Read files > 1 GB lazily

This reads big files (>200 MBs) sequentially and without loading the whole File in memory. This way we’re able to read text files on the Gigabyte level. This example was done reading from a remote SFTP server.

final ChannelSftp sftpClient = this.connect();
final InputStream is = sftpClient.get(file);
final InputStreamReader isReader
      = new InputStreamReader(is);  

try (final BufferedReader bffReader  
      = new BufferedReader(isReader)) {
  bffReader.lines()
        .forEach(this::doAction);
} catch(final IOException ex) {
  log.error("bla", ex);
}

Read More