mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Use normalizer as stream source Renamed 'writer' service to 'normalizer' and dropped Cassandra facilities from it. Extracted the common dependencies to 'mainflux' package for easier sharing. Fixed the API docs and unified environment variables. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use docker build arguments to specify build Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove cassandra libraries Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update go-kit version to 0.6.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix manager configuration Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Refactor docker-compose Merged individual compose files and dropped external links. Remove CoAP container since it is not referenced from NginX config at the moment. Update port mapping in compose and nginx.conf. Dropped bin scripts. Updated service documentation. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Drop content-type check Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement users data access layer in PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Bump version to 0.1.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use go-kit logger everywhere (except CoAP) Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Improve factory methods naming Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement clients data access layer on PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Make tests stateless All tests are refactored to use map-based table-driven tests. No cross-tests dependencies is present anymore. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove gitignore Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nginx proxying Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Mark client-user FK explicit Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update API documentation Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update channel model Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add channel PostgreSQL repository tests Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement PostgreSQL channels DAO Replaced update queries with raw SQL. Explicitly defined M2M table due to difficulties of ensuring the referential integrity through GORM. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Expose connection endpoints Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix swagger docs and remove DB logging Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nested query remarks Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add unique indices Signed-off-by: Dejan Mijic <dejan@mainflux.com>
122 lines
3.1 KiB
Go
122 lines
3.1 KiB
Go
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
|
|
|
package codec
|
|
|
|
// All non-std package dependencies live in this file,
|
|
// so porting to different environment is easy (just update functions).
|
|
|
|
func pruneSignExt(v []byte, pos bool) (n int) {
|
|
if len(v) < 2 {
|
|
} else if pos && v[0] == 0 {
|
|
for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
|
|
}
|
|
} else if !pos && v[0] == 0xff {
|
|
for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// validate that this function is correct ...
|
|
// culled from OGRE (Object-Oriented Graphics Rendering Engine)
|
|
// function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
|
|
func halfFloatToFloatBits(yy uint16) (d uint32) {
|
|
y := uint32(yy)
|
|
s := (y >> 15) & 0x01
|
|
e := (y >> 10) & 0x1f
|
|
m := y & 0x03ff
|
|
|
|
if e == 0 {
|
|
if m == 0 { // plu or minus 0
|
|
return s << 31
|
|
}
|
|
// Denormalized number -- renormalize it
|
|
for (m & 0x00000400) == 0 {
|
|
m <<= 1
|
|
e -= 1
|
|
}
|
|
e += 1
|
|
const zz uint32 = 0x0400
|
|
m &= ^zz
|
|
} else if e == 31 {
|
|
if m == 0 { // Inf
|
|
return (s << 31) | 0x7f800000
|
|
}
|
|
return (s << 31) | 0x7f800000 | (m << 13) // NaN
|
|
}
|
|
e = e + (127 - 15)
|
|
m = m << 13
|
|
return (s << 31) | (e << 23) | m
|
|
}
|
|
|
|
// GrowCap will return a new capacity for a slice, given the following:
|
|
// - oldCap: current capacity
|
|
// - unit: in-memory size of an element
|
|
// - num: number of elements to add
|
|
func growCap(oldCap, unit, num int) (newCap int) {
|
|
// appendslice logic (if cap < 1024, *2, else *1.25):
|
|
// leads to many copy calls, especially when copying bytes.
|
|
// bytes.Buffer model (2*cap + n): much better for bytes.
|
|
// smarter way is to take the byte-size of the appended element(type) into account
|
|
|
|
// maintain 3 thresholds:
|
|
// t1: if cap <= t1, newcap = 2x
|
|
// t2: if cap <= t2, newcap = 1.75x
|
|
// t3: if cap <= t3, newcap = 1.5x
|
|
// else newcap = 1.25x
|
|
//
|
|
// t1, t2, t3 >= 1024 always.
|
|
// i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
|
|
//
|
|
// With this, appending for bytes increase by:
|
|
// 100% up to 4K
|
|
// 75% up to 8K
|
|
// 50% up to 16K
|
|
// 25% beyond that
|
|
|
|
// unit can be 0 e.g. for struct{}{}; handle that appropriately
|
|
var t1, t2, t3 int // thresholds
|
|
if unit <= 1 {
|
|
t1, t2, t3 = 4*1024, 8*1024, 16*1024
|
|
} else if unit < 16 {
|
|
t3 = 16 / unit * 1024
|
|
t1 = t3 * 1 / 4
|
|
t2 = t3 * 2 / 4
|
|
} else {
|
|
t1, t2, t3 = 1024, 1024, 1024
|
|
}
|
|
|
|
var x int // temporary variable
|
|
|
|
// x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
|
|
if oldCap <= t1 { // [0,t1]
|
|
x = 8
|
|
} else if oldCap > t3 { // (t3,infinity]
|
|
x = 5
|
|
} else if oldCap <= t2 { // (t1,t2]
|
|
x = 7
|
|
} else { // (t2,t3]
|
|
x = 6
|
|
}
|
|
newCap = x * oldCap / 4
|
|
|
|
if num > 0 {
|
|
newCap += num
|
|
}
|
|
|
|
// ensure newCap is a multiple of 64 (if it is > 64) or 16.
|
|
if newCap > 64 {
|
|
if x = newCap % 64; x != 0 {
|
|
x = newCap / 64
|
|
newCap = 64 * (x + 1)
|
|
}
|
|
} else {
|
|
if x = newCap % 16; x != 0 {
|
|
x = newCap / 16
|
|
newCap = 16 * (x + 1)
|
|
}
|
|
}
|
|
return
|
|
}
|