Skip to content

Commit

Permalink
Update bitfields.md
Browse files Browse the repository at this point in the history
update bitfields data type page to include hugo short code - condense example and add relevant context
  • Loading branch information
sav-norem authored Oct 12, 2023
1 parent 4d345ac commit 0df18bf
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions docs/data-types/bitfields.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,27 @@ Bitfields support atomic read, write and increment operations, making them a goo

## Examples

Suppose you're keeping track of activity in an online game.
You want to maintain two crucial metrics for each player: the total amount of gold and the number of monsters slain.
Because your game is highly addictive, these counters should be at least 32 bits wide.
Suppose you're keeping track of stats for various bicycles.
You want to maintain two crucial metrics for each bike: the current price and the number of owners.
For this example, we're making the counters 32 bits wide.

You can represent these counters with one bitfield per player.

* New players start the tutorial with 1000 gold (counter in offset 0).
```
> BITFIELD player:1:stats SET u32 #0 1000
1) (integer) 0
```
* Bike 1 initially costs 1,000 (counter in offset 0) and has never had an owner. After being sold, it's now considered used and the price instantly drops to reflect its new condition, and it now has an owner (offset 1). After quite some time, the bike becomes a classic. The original owner sells it for a profit, so the price goes up and the number of owners does as well. Lastly, we'll look at the bikes current price and number of owners.

* After killing the goblin holding the prince captive, add the 50 gold earned and increment the "slain" counter (offset 1).
```
> BITFIELD player:1:stats INCRBY u32 #0 50 INCRBY u32 #1 1
1) (integer) 1050
2) (integer) 1
```

* Pay the blacksmith 999 gold to buy a legendary rusty dagger.
```
> BITFIELD player:1:stats INCRBY u32 #0 -999
1) (integer) 51
```

* Read the player's stats:
```
> BITFIELD player:1:stats GET u32 #0 GET u32 #1
1) (integer) 51
{{< clients-example bitfield_tutorial bf >}}
> BITFIELD bike:1:stats SET u32 #0 1000
1) (integer) 0
> BITFIELD bike:1:stats INCRBY u32 #0 -50 INCRBY u32 #1 1
1) (integer) 950
2) (integer) 1
```
> BITFIELD bike:1:stats INCRBY u32 #0 500 INCRBY u32 #1 1
1) (integer) 1450
2) (integer) 2
> BITFIELD bike:1:stats GET u32 #0 GET u32 #1
1) (integer) 1450
2) (integer) 2
{{< /clients-examples >}}


## Performance
Expand Down

0 comments on commit 0df18bf

Please sign in to comment.