SSDB basic instructions and usage, SSDB basic instructions
There are several reasons for using SSDB at that time. First, when designing the contest module, You need to query the Rank of the current competition multiple times because you click to query the Rank of the current competition, the amount of data queried each time is large. to efficiently obtain data, the SSDB database is used. The second aspect is to record the user's AC status. After the user logs in, the web page must display the questions and questions that the user AC has tried but failed. In order to traverse all the submit stored in the current system, the data volume is also large. Follow the previous principles and continue to use SSDB for solutions.
1: What is SSDB?
Baidu once failed to find Baidu encyclopedia. One definition is that SSDB is an open-source high-performance database server that uses Google LevelDB as the storage engine and supports T-level data. Literally, this database has high performance (/□ \)). Then, we need to use the feature of high query speed.
2: Main Characteristics of SSDB (Entry statistics (/□\), we mainly use the k-v mode, that is, the key-value ):
Supports zset, map/hash, list, and kv data structures to replace Redis
It is particularly suitable for storing a large amount of Set Data and supports a wide range of data structures: key-value, key-map, key-zset, and key-list.
Use Google LevelDB as the storage engine
Supports master-slave synchronization and multi-master synchronization.
The client supports PHP, C ++, Python, Lua, Java, Ruby, nodejs, and Go.
Memory usage is very small
3: Installation and start stop
We recommend that you install the release method in SSDB by compiling and installing the source code. We recommend that you run the release line environment in mainstream Linux releases of the release line version. log on to your server remotely using SSH, and then use the following commands to download, compile, install, and run the following commands:
# Installation
$ Sudo wget -- no-check-certificate https://github.com/ideawu/ssdb/archive/master.zip
$ Sudo unzip master
$ Cd ssdb-master
$ Sudo make
$ # Optional, install ssdb in/usr/local/ssdb
$ Sudo make install
# Start
$ Sudo./ssdb-server ssdb. conf
# Start as a daemon
$ Sudo./ssdb-server-d ssdb. conf
# Stop
$ Sudo kill 'cat./var/ssdb. Pi'
SSDB is installed in the/usr/local/ssdb logs directory by default. ssdb-server is the server program, and ssdb-cli is the command line client.
4: Configuration
The SSDB. conf file attached to ssdb is a configuration file, which can be used without modification .. The standard configuration file is as follows:
# Ssdb-server config
# MUST indent by TAB!
# Relative to path of this file, directory must exists
Work_dir =./var
Pidfile =./var/ssdb. pid
Server:
Ip: 127.0.0.1
Port: 8888
# Bind to public ip
# Ip: 0.0.0.0
# Format: allow | deny: all | ip_prefix
# Multiple allows or denys is supported
# Deny: all
# Allow: 127.0.0.1
# Allow: 192.168
Replication:
Slaveof:
# To identify a master even if it moved (ip, port changed)
# If set to empty or not defined, ip: port will be used.
# Id: svc_2
# Sync | mirror, default is sync
# Type: sync
# Ip: 127.0.0.1
# Port: 8889
Logger:
Level: info
Output: log.txt
Rotate:
Size: 1000000000
Leveldb:
# In MB
Cache size: 500
# In KB
Block_size: 32
# In MB
Write_buffer_size: 64
# In MB
Compaction_speed: 1000
# Yes | no
Compression: no
Work_dir: the working directory of ssdb-server. After startup, two directories, data and meta, are generated under this directory to save the database files of LevelDB. this directory is relative to ssdb. the relative path of conf. You can also specify the absolute path.
Server: ip and port specify the IP address and port number of the server to listen. if the ip address is 0.0.0.0, it indicates binding all IP addresses. based on security considerations, you can set the ip address to 127.0.0.1, so that only the local machine can access it. to implement more stringent network security restrictions, you must rely on the iptables of the operating system.
Replication: used to specify master-slave synchronous replication. slaveof. ip, slaveof. port indicates that this SSDB server will synchronize data from this target machine (that is, the server corresponding to this configuration file is slave ). you can refer to the preparation of ssdb_slave.conf.
Logger: Configure logging. level is the log level, which can be trace | debug | info | error. output is the name of the log file. SSDB supports log rotation. After the log file reaches a certain size, rename log.txt and create a new log.txt.
Leveldb: configure the LevelDB parameters. You generally want to modify the cache_size parameter to specify the cache size. An appropriate cache can improve read performance, but a large cache will affect write performance.
Use the ssdb that comes with the producer when you enable the consumer. when the conf file is configured, the volume log volume file generated by SSDB logs is segmented by volume. therefore, you need to write your own crontab to compress logs and regularly clean the logs.
If a system fault such as server power failure or kernel panic occurs, after the system is restarted, You need to manually delete the ssdb PID file ssdb. pid before starting the ssdb.
5. Procedure:
After the installation is complete, run sudo./ssdb-server-d ssdb. conf in the ssdb directory to start SSDB. At this time, the database is already running.
When you need to store data:
First, sort the information to be stored, for example (contestID, rank_list_proto_str). The former is the key, and the latter is the value.
Furthermore, operations are decided. Currently, the main operations involved are data storage and query, I .e. insert and get operations:
When data is inserted, ssdb is used. set (key, value). If the key is a combination, it is connected with '\ t' in the middle, for example, ssdb. set (userID + '\ t' + problemID, value ).
When querying, the main mode is the same, that is, using ssdb. get (key) to get the corresponding value. If no corresponding value exists, an empty string is returned.
Insert:
ssdb_api.SetContestRankListProto(contestID, contest_rank_list.SerializeToString())
ssdb_api.InsertUserProblemStatus(user.userID, submit.problemID, submit.status)
Query:
contest_rank_list = ssdb_api.GetContestRankListProto(contest_id)
status_ = ssdb_api.GetUserProblem(user.userID, problem.problemID)
All the above operations have been written with the operation function, that is, the function in view/ssdb_api.py.
from ssdb import SSDBimport sysimport cugbacm.proto.rank_pb2 ssdb_ip = "127.0.0.1"ssdb_port = 6666ssdb = SSDB(host=ssdb_ip, port=ssdb_port) def GetContestRankListProto(contestID): global ssdb proto_str = ssdb.get(contestID) return proto_str def SetContestRankListProto(contestID, rank_list_proto_str): global ssdb try: ssdb.set(contestID, rank_list_proto_str) except: pass def InsertUserProblemStatus(userID, problemID, status): #AC == 1 No_pass = 2 other = 0 global ssdb value = 2 if status == "Accepted": value = 1 st = str(ssdb.get(userID + '\t' + str(problemID))) if st == "1": return; else: ssdb.set(userID + '\t' + str(problemID), value)
<pre name="code" class="html">def GetUserProblem(userID, problemID): global ssdb st = ssdb.get(userID + '\t' + str(problemID)) if str(st) != "1" and str(st) != "2": return "0" else: return str(st)
After setting this. py file, you need to introduce this file into the file using SSDB, which is the host localhost.
Add the rest later (/□ \)