API Design: Use type state pattern to avoid ambiguous option flags

For example,ZADD is a command that add member with score to sorted set, and it can accept NX or XX as option. 1 2 ZADD key [NX | XX] [GT | LT] [CH] [INCR] score member [score member ...] XX: Only update elements that already exist. Don’t add new elements. NX: Only add new elements. Don’t update already existing elements. NX 與 XX 只能選一個,在 go-redis 是用這樣的 structure 來裝 argument, 但這就必須要額外的註解與檢查來告訴使用者 NX, XX 是互斥的。 1 2 3 4 5 6 7 8 type ZAddArgs struct { NX bool XX bool LT bool GT bool Ch bool Members []Z } Ref: go-redis ZAddArgs

Build Nested JSON in PostgreSQL

Original Stackoverflow thread: https://stackoverflow.com/questions/42222968/create-nested-json-from-sql-query-postgres-9-4/42226253#42226253 Suppose we have this tables: person car wheel And the relation between is: person:car = 1:N car:wheel = 1:N We need to build some nested JSON Object with SQL Query to get the summary about details of each car this person has, what would you do ? The Goal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 { "persons": [ { "person_name": "Johny", "cars": [ { "carid": 1, "type": "Toyota", "comment": "nice car", "wheels": [ { "which": "front", "serial number": 11 }, { "which": "back", "serial number": 12 } ] }, { "carid": 2, "type": "Fiat", "comment": "nice car", "wheels": [ { "which": "front", "serial number": 21 }, { "which": "back", "serial number": 22 } ] } ] }, { "person_name": "Freddy", "cars": [ { "carid": 3, "type": "Opel", "comment": "nice car", "wheels": [ { "which": "front", "serial number": 3 } ] } ] } ] } Approach 1 - Left Join 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 select json_build_object( 'persons', json_agg( json_build_object( 'person_name', p.

Use `sync.Pool` to reduce memory consumption

Identifying the problem Our service is like a excel document datastore. and we use xorm as ORM framework, Everytime we need to get data from DB, we call session.Find(&[]Author{}) with the slice of table beans, but this have a problem, Memory allocation is very high So every time lots of clients try to download excel file, the memory consumption is too high, and downloadling excel file takes too long to complete.

Optimize a PARTITION - SELECT query up to 60x faster

This post demonstrates my experience of optimizing a PARTITION - SELECT query, and how I made it up to 60x faster. Original Query and the use case Our App is a simple excel data version control system, the data is organized by project, key and data is stored in seperated table called dbKey and dbData. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 create table dbKey ( id serial , project_id int, -- keys goes here -- NOTE: key can be 1.

ChatGPT First Glance

This is my first glance of ChatGPT, and I ask her to generate a peice of code in Haskell, which can map a function to a list. The result she generated is totally correct, and can be run in playground. 1 2 3 4 5 6 7 8 9 10 11 12 addOneToEach :: [Int] -> [Int] addOneToEach xs = map (+1) xs myMap :: (a -> b) -> [a] -> [b] myMap _ [] = [] myMap f (x:xs) = f x : myMap f xs main = do let myList = [1, 2, 3, 4] let doubledList = myMap (*2) myList print doubledList -- Output: [2,4,6,8] Here’s the link to our chat: https://sharegpt.

My First Post

Hello, This is my first time trying Hugo! Inline Formula: 1 SELECT 'hello-world' FROM me Block Formula: Test IFrame