When I recently browsed an article about the stream of events, I mentioned the flow of data, and I wanted to see if I could implement one on the Android side.
According to the article, each time the data change events, such as inserting, deleting or updating, as an immutable event, so that the data flow in the event, rather than the database of destructive write, that is, directly read the data aggregated results can obtain the best performance.
The event flow can do the following:
1. Get all the original events, you may need to convert them, and then load them into a large data warehouse for the analyst to use;
2. Update the full-text search index so that users can search for the latest data; 3. Update the cache so that the system can read the data from the fast cache and ensure that the data in the cache is up to date; 4. Create a new event stream by processing the event stream, and then use the latter as input to another system. Possible benefits are as follows: 1. Loose coupling: Data read and write using different database schemas, the data read through the written data conversion, the degree of coupling between different parts of the application is reduced; 2. Read and write performance: normalization (write fast) and non-normalized (read fast) controversy stems from the assumption that data is read and written using the same pattern, Database mode, read and write speed will be improved ; 3. Extensibility: Because event flow is a simple abstraction and allows developers to decompose the application into streams of producers and consumers, it is easy to parallelize and extend across machines; 4. Flexibility: The original event is simple and clear, the mode migration does not have much impact, but presenting the data to the user is much more complex, but if there is a conversion process can be implemented To the conversion of cached content, when a new user interface is required, only new logic is needed to build the new cache; 5. Error Scenario: Original eventis the same fact that if there is a problem with the system, developers can always replay the event in the same order. If this is the case, write a query or rule to match events that satisfy a particular pattern, and then perform a full-text search on the stream, register a query in advance on the stream, and send a notification when there is an event matching query. Several concepts derive from this:1. Response: The main is to provide the event stream to the user interface to use;2. Change data capture: Use the database in a way that we are familiar with, but extract any insert, update, and delete operations into a data change event stream. The initial implementation has come out, the approximate implementation is as follows:
1. Creating databases and Tables
在assets文件夹下创建database.xml文件,里面配置数据库的名字,版本号和数据库的表:
<?XML version= "1.0" encoding= "Utf-8"?> <Database> <!--Database name - <dbnamevalue= "Zwb.db"></dbname> <!--Database Version - <versionvalue= "1"></version> <!--Database Tables - <List> <Mappingclass= "Com.zwb.args.dbpratice.model.Status"></Mapping> <Mappingclass= "Com.zwb.args.dbpratice.model.User"></Mapping> </List> </Database>
然后初始化DatabaseCache:
Databasecache cache = databasecache.getinstance (this);
该操作应该是在Application中声明,因为该动作涉及到数据库和表的创建。
2. Basic use
声明一个model类,继承自BaseTable:
@Table (Table = "status") Public classStatusextendsbasetable {@ColumnPrivateString name; @ColumnPrivateString Statusid; Public voidsetName (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidSetstatusid (String id) { This. Statusid =ID; } PublicString Getstatusid () {returnStatusid; } }
其中,@Table声明的是该model对应的表的名字,@Column声明的是该字段对应的数据库中的类型。
如果该字段的类型和数据库中的类型不一致,可以通过@ColumnType来指定类型。
3. Data insertion
New Status (); Status.setname ("forwarding"); Status.setstatusid ( "insertevent"); Insertstatusevent.to (Status. class). Insert (status);
4. Data Update
New updateevent (); Updateevent.to (Status. class). WHERE ("id", "n"). Update ("name", "Hello");
5. Data query
list<status> statuslist = Cache.from (Status. Class). Where ("Statusid", "n"). Find ();
这样就是查询Status表中的statusId为01的所有记录。当然,也可以查询所有数据:
list<status> statuslist = Cache.from (Status. Class). FindAll ();
6. Data read
Databasecache cache = databasecache.getinstance (this); List<Status> statuslist = cache.readfromdb (Status. Class);
该操作应该在Application中执行,然后执行相应的数据插入:
for (Status status:statuslist) { new insertevent (); Insertevent.to (Status. class ). Insert (status);}
这样数据就会从数据库转移到事件流中。
7.数据存储
Databasecache cache = databasecache.getinstance (this); Cache.inserttodb (Status. class);
这样就会将和Status有关的数据插入到数据库中。
8.数据删除
New deleteevent ();d eleteevent.to (Status. class). WHERE ("id", "n"). Delete ();
这样就是删除id为01的数据。 如果是删除某个集合的全部数据,则是:
New Arraylist<status>(); for (int i = 0; i <; i++) { new Status (); Status.setname ("Hello"); Status.setid ("the"); Statuses.add (status); } Deleteevent.to (Status. class). DeleteAll (statuses);
如果是删除表的全部数据:
Deleteevent.to (Status. Class). DeleteAll ();
This is the current implementation, there will be time to explain the implementation of the process, the specific project address on GitHub: Https://github.com/wenjiang/EventStreamDB, interested can go up to see, by the way to a star.
Event stream operations database on Android